How to setup serial communication in Processing to /dev/rfcomm0 - bluetooth

I am trying to perform serial communication on between Ubuntu 12.04 and a JY-MCU bluetooth serial module connected to an Arduino.
I have created this configuration in /etc/bluetooth/rfcomm.conf
rfcomm0 {
# # Automatically bind the device at startup
bind yes;
#
# # Bluetooth address of the device
device 00:12:11:19:08:54
# # RFCOMM channel for the connection
channel 1;
# # Description of the connection
comment "Linvor Bluetooth Module";
}
I can use putty to communicate with the /dev/rfcomm0 serial port and this works perfectly.
However, despite many attempts I simply cannot see how to create a serial port in Processing that works in any way.
For example :
println(Serial.list());
prints nothing at all.
If I execute:
String portName = "/dev/rfcomm0";
myPort = new Serial(this, portName, 9600);
println(myPort);
I see this in the monitor:
processing.serial.Serial#1712651
But if I then call:
myPort.write('9');
I get an exception:
java.lang.NullPointerException
at processing.serial.Serial.write(Serial.java:572)
...
I can't understand why this fails. I have been following all the instructions from Tom Igoe's "Making Things Talk", but this just does not work the way he says...
Any help would b great!
Thanks,
Bob

Aftert searching high and low, I have made this work.
The key issue is that processing uses the rxtx java library (RXTX-2.1-7) for serial communications.
The RXTX wiki says:
"rxtx tries to detect ports on by scanning /dev for files matching any
of a set of known-good prefixes, such as 'ttyS', 'ttym', and since 2.2
'ttyUSB' and so on. "
And since the bluetooth device is named rfcomm* it cannot be detected.
The trick is to create a sym link to fool rxtx (use a ttyS device that is not yet assigned):
$ sudo ln -s /dev/rfcomm0 /dev/ttyS99
Then, connect:
$ sudo rfcomm connect 0
Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
Press CTRL-C for hangup
At this point the red led on the JY-MCU becomes solid and processing can detect it:
println(Serial.list());
output is:
[0] "/dev/ttyACM0"
[1] "/dev/ttyS99"
So, serial communication can work.
To summarize, the following process will allow a processing script to communicate via a serial port with a JY-MCU device in a BlueZ linux framework
One time setup:
power up the JY-MCU,
use the following command to get its hardware address, mine is 00:12:11:19:08:54
$ hcitool scan
use that to create the /etc/bluetooth/rfcomm.conf file; you'll note that I chose 0 for the rfcomm device , we need that for connection later:
$ cat /etc/bluetooth/rfcomm.conf
rfcomm0 {
bind yes;
device 00:12:11:19:08:54;
channel 1;
comment "Linvor Bluetooth Module";
}
use BlueMan to pair the JY-MCU.
Every time you want to use the JY-MCU
create the sym link:
$ sudo ln -s /dev/rfcomm0 /dev/ttyS99
connect to the JY-MCU:
$ sudo rfcomm connect 0
Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
Press CTRL-C for hangup
you can now run a processing script and connect to the JY-MCU with
the code:
String portName = "/dev/ttyS99";
myPort = new Serial(this, portName, 9600);
after running the processing script, be sure to CTRL-C at the
command line to disconnect the JY-MCU.
That should do it!
Ciao,
Bob

Just something popped up in my mind.
I had similar problems that were caused due to channel 1 is already used. If you bind to a channel which already is in use, bad things may happen.
sdptool browse local
Use that command to see which channels are available on your Ubuntu device.

Related

Serial Ports Linux vs Windows

I am having issues with my pyserial program on Ubuntu 16.
I am trying to send a break command to a hardware device using pyserial. I wrote a python script that:
Sets the port/baud/parity/bytesize/...
Opens the port
Sends a break command
Reads the return message from the device.
I run the script on my Ubuntu 16.04 machine and I get zero response, it just hangs or eventually timeouts. I copy the same script to my Windows machine, change the port (from /dev/ttyUSB0 to COM#) and my script works perfectly, gets a response from the device immediately.
When I run the script on Ubunutu I have to give permissions to the port (sudo chmod 666 /dev/ttyUSB), or I get permission denied errors when opening the port. Not sure if this matters.
Does anyone have any insight on what might be going on? I know Windows and Linux handle serial/com ports differently but I am a newbie to both Linux and serial so not sure if I am missing something.
I am using this USB to Serial cable (http://www.ugreen.com.cn/product-681-en.html) and I had to install some drivers. I connected the serial read/write pins and tested to make sure data is going through (which it is) so I know that works.
import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0' # or COM12 on windows
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.bytesize = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
ser.open()
ser.send_break(duration=0.9)
print(ser.read(10))
First off, you need to have proper permissions for accessing serial ports. It can be done by including your user into the group dialout:
sudo usermod -aG dialout <user>
You need to restart the system to complete it.
To avoid of some information to remain in buffers, you may need to clear read and write buffers before serial port operations:
pyserial 3.0:
ser.reset_input_buffer()
ser.reset_output_buffer()
pyserial 2.7 or earlier:
ser.flushInput()
ser.flushOutput()
Don't forget to close a port after all operations being done. Hope this helps.
First forget about your app and focus on the port troubleshooting, use putty in serial mode for sending a few terminal chars using this port. Use a jumper for conecting DB9 pin 3 to pin 2(rx and tx) an validate that you receive an echo of each character you type.

How to check serial connection between 2 Linux PCs

I need to connect 2 linux PCs via serial port. I used serial connector cable (null modem cable) to connect 2 linux PCs. But now how can I establish the connection I mean how can I test whether they are connected or not. Can anybody Please let me know the procedure.. I have tried screen command and when i run
screen /dev/ttyS0
This command is just opening new screen and i cant do anything on that new screen.
Am using Fedora-20 with kernel version 3.* in one PC and another one with Fedora-27 kernel version is 4.13
Hi finally i found solution to my problem Using this_link.
at both ends tried below steps:
1) yum install minicom
2) Then I configured minicom using minicom -s then using down arrow selected serial port setup
then pressed A from keyborad to change that serial device from
/dev/modem
to
/dev/ttyS0
press enter and chose save as dfl option then exit from minicom
After these configuration steps executed the commands suggested by #Niall Cosgrove
at one end executed cat /dev/ttyS0
at other end executed echo hello > /dev/ttyS0
finally i got hello message from one pc to other end using serial port:)

Problems with serial communicaiton on Raspberry Pi 3

I am trying to establish communication between a Raspberry Pi3 and a digital Sensor that has a RS-232 connection. I have bought a hat that also has a RS-232 port on it and can be connected to the rx and tx pins on the Pi. The Pi has raspbian installed and I am using Python 3 to write the code.
In the tutorials I read it says that i have to configure the Pi first to allow serial communication. I have done following steps:
Step 1:
sudo raspi-config -> Interfacing Options -> Serial -> [login shell accesible over serial? -> no] -> [serial port hardware enabled? -> yes]
Step 2:
sudo nano /boot/cmdline.txt
change line to:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Step 3:
sudo nano /boot/config.txt
add lines:
dtoverlay=pi3-miniuart-bt
enable_uart=1
force_turbo=1
Step 4:
sudo apt-get install python-serial
After this setup i wrote a very short program in Python 3 to test it:
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write(bytes(5, 'UTF8'))
The number 5 tells the sensor to change the displayed unit. The code runs without an error message but I dont see the unit of the sensor change. If I connect the RS232 to my laptop with a serial to usb converter and use Putty to monitor the serial port, I also do not see anyting being send.
It would be great if someone can tell me where my problem is. But I would also be happy about any input on how I can find a solution myself.
Many thanks in advance,
Florian
I think Raspberry Pi 3 uses a different device for the serial port. Try
ser = serial.Serial('/dev/ttyS0', 9600)

gatttool and hcitool on Linux

I'm trying to connect to a device using gatttool on Linux. I run hcitool lescan to get the device MAC address, and then gatttool -i hci1 -b <macaddr> -t random -I. Then I type connect. I see [CON] as expected, but the problem is that it disappears automatically after about 1s, with no error messages. I have been searching for hours, but I don't see why that is so?
Note: I have run hcitool lecc before running gatttool one time just to try it, but it seems to me that since then, gatttool connection is not working properly anymore (unexpected disconnection as explained above). Is it because I have used hcitool lecc? If yes, is there a way to "undo" it?
Not sure if this helps you because I'm seeing this behavior on my Raspberry Pi (an up-to-date wheezy version) and bluez 5.18 (released April 2014.)
I can connect to my BLE peripheral from the command line after doing an lecc without any problems. I don't think you need to reset after an lecc. Regardless, if you do want to reset your BT dongle because it is in some funky state, look at this excellent step-by-step guide to reset the BT dongle. I have followed these steps with consistent results.
I can connect and query say primary but more often than not, the connection drops before I can manually type in primary. See the results below. If I use the up/down arrows after connecting to quickly scroll to primary, I am able to see primary services offered by this peripheral without any problems.
~ $ sudo gatttool -I -i hci0 -m 48 -b 20:CD:39:A8:11:AF
[20:CD:39:A8:11:AF][LE]> connect
Attempting to connect to 20:CD:39:A8:11:AF
Connection successful
[20:CD:39:A8:11:AF][LE]> primary
Command Failed: Disconnected

Linux command line howto accept pairing for bluetooth device without pin

Is there a way to pair a device in linux without requiring a pin(for testing purposes so I need it to be done w/out human interaction, assuming you have root access)?
bluez-simple-agent seems to require a pin except with some simple devices such as mice.
Entering a PIN is actually an outdated method of pairing, now called Legacy Pairing. Secure Simple Pairing Mode is available in Bluetooth v2.1 and later, which comprises most modern Bluetooth devices. SSPMode authentication is handled by the Bluetooth protocol stack and thus works without user interaction.
Here is how one might go about connecting to a device:
# hciconfig hci0 sspmode 1
# hciconfig hci0 sspmode
hci0: Type: BR/EDR Bus: USB
BD Address: AA:BB:CC:DD:EE:FF ACL MTU: 1021:8 SCO MTU: 64:1
Simple Pairing mode: Enabled
# hciconfig hci0 piscan
# sdptool add SP
# hcitool scan
00:11:22:33:44:55 My_Device
# rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &
Connected /dev/rfcomm0 to 00:11:22:33:44:55 on channel 1
Press CTRL-C for hangup
This would establish a serial connection to the device.
follow steps (CentOs):
bluetoothctl
devices
scan on
pair 34:88:5D:51:5A:95 (34:88:5D:51:5A:95 is my device code,replace it with yours)
trust 34:88:5D:51:5A:95
connect 34:88:5D:51:5A:95
If you want more details
https://www.youtube.com/watch?v=CB1E4Ir3AV4
Try setting security to none in /etc/bluetooth/hcid.conf
http://linux.die.net/man/5/hcid.conf
This will probably only work for HCI devices (mouse, keyboard, spaceball, etc.). If you have a different kind of device, there's probably a different but similar setting to change.
This worked like a charm for me, of-course it requires super-user privileges :-)
# hcitool cc <target-bdaddr>; hcitool auth <target-bdaddr>
To get <target-bdaddr> you may issue below command:
$ hcitool scan
Note: Exclude # & $ as they are command line prompts.
Courtesy
For Ubuntu 14.04 and Android try:
hcitool scan #get hardware address
sudo bluetooth-agent PIN HARDWARE-ADDRESS
PIN dialog pops up on Android device. Enter same PIN.
Note: sudo apt-get install bluez-utils might be necessary.
Note2: If PIN dialog does not appear, try pairing from Android first (will fail because of wrong PIN). Then try again as described above.
~ $ hciconfig noauth
This should do the trick (I'm using bluez 5.23 and there's no more simple-egent and blue-utils). However, I'm trying to look for a way to make changes hciconfig permanent because after power out and then power on, authentication is needed again. So far, the changes in hciconfig still stays the same when you reboot it. it reverts back only when power out. If anybody has found a way to make hciconfig permanent, do let me know!
~ $ hciconfig noauth
It worked for me in "Linux mx 4.19"
The exact steps are:
1) open a terminal - run: "hciconfig noauth"
2) use the blueman-manager gui to pair the device (in my case it was a keyboard)
3) from the blueman-manager choose "connect to HID"
step(3) is normally asking for a password - the "hciconfig noauth" makes step(3) passwordless

Resources