Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 765
gratefulfrog Avatar asked Mar 17 '13 18:03

gratefulfrog


1 Answers

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:

  1. power up the JY-MCU,

  2. use the following command to get its hardware address, mine is 00:12:11:19:08:54

    $ hcitool scan  
    
  3. 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";
    }
    
  4. use BlueMan to pair the JY-MCU.

Every time you want to use the JY-MCU

  1. create the sym link:

    $ sudo ln -s  /dev/rfcomm0 /dev/ttyS99
    
  2. 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
    
  3. 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);
    
  4. 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

like image 101
gratefulfrog Avatar answered Oct 13 '22 18:10

gratefulfrog