如何在Processing到/ dev/rfcomm0中设置串行通信

gra*_*rog 5 processing serial-port bluetooth rfcomm

我正在尝试在Ubuntu 12.04和连接到Arduino的JY-MCU蓝牙串行模块之间进行串行通信.

我在/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";
}
Run Code Online (Sandbox Code Playgroud)

我可以使用putty与/ dev/rfcomm0串口进行通信,这非常有效.

然而,尽管经过多次尝试,我根本无法看到如何在Processing中创建一个以任何方式工作的串行端口.

例如 :

println(Serial.list());
Run Code Online (Sandbox Code Playgroud)

什么都不打印.

如果我执行:

String portName = "/dev/rfcomm0";
myPort = new Serial(this, portName, 9600);
println(myPort);
Run Code Online (Sandbox Code Playgroud)

我在显示器中看到了这个:

processing.serial.Serial@1712651
Run Code Online (Sandbox Code Playgroud)

但如果我再打电话:

 myPort.write('9');
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

java.lang.NullPointerException
    at processing.serial.Serial.write(Serial.java:572)
    ...
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么会失败.我一直在遵循Tom Igoe的"Make Things Talk"中的所有指示,但这并不像他说的那样......

任何帮助都会很棒!

谢谢,

短发

gra*_*rog 8

经过高低的搜索,我完成了这项工作.

关键问题是处理使用rxtx java库(RXTX-2.1-7)进行串行通信.

RXTX维基说:

"rxtx试图通过扫描/ dev检测端口是否匹配任何一组已知良好前缀的文件,例如'ttyS','ttym',以及2.2'ttyUSB'等等."

并且由于蓝牙设备被命名 rfcomm*,因此无法检测到.

诀窍是创建一个骗局rxtx的sym链接(使用尚未分配的ttyS设备):

$ sudo ln -s  /dev/rfcomm0 /dev/ttyS99
Run Code Online (Sandbox Code Playgroud)

然后,连接:

$ sudo rfcomm connect 0
 Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
 Press CTRL-C for hangup
Run Code Online (Sandbox Code Playgroud)

此时,JY-MCU上的红色LED变为稳固,处理可以检测到它:

println(Serial.list());
Run Code Online (Sandbox Code Playgroud)

输出是:

[0] "/dev/ttyACM0" 
[1] "/dev/ttyS99"
Run Code Online (Sandbox Code Playgroud)

因此,串行通信可以工作.

总而言之,以下过程将允许处理脚本通过串行端口与BlueZ linux框架中的JY-MCU设备进行通信

一次性设置:

  1. 启动JY-MCU,

  2. 使用以下命令获取其硬件地址,我的是00:12:11:19:08:54

    $ hcitool scan  
    
    Run Code Online (Sandbox Code Playgroud)
  3. 用它来创建/etc/bluetooth/rfcomm.conf文件; 你会注意到我为rfcomm设备选择了0,我们需要稍后连接:

    $ cat /etc/bluetooth/rfcomm.conf
    rfcomm0 {
        bind yes;
        device 00:12:11:19:08:54;
        channel    1;
        comment "Linvor Bluetooth Module";
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用BlueMan配对JY-MCU.

每次你想使用JY-MCU

  1. 创建sym链接:

    $ sudo ln -s  /dev/rfcomm0 /dev/ttyS99
    
    Run Code Online (Sandbox Code Playgroud)
  2. 连接到JY-MCU:

    $ sudo rfcomm connect 0
      Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
      Press CTRL-C for hangup
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您现在可以运行处理脚本并使用以下代码连接到JY-MCU:

    String portName = "/dev/ttyS99";
    myPort = new Serial(this, portName, 9600);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 运行处理脚本后,请务必在命令行中按CTRL-C断开JY-MCU.

应该这样做!Ciao,Bob


Eri*_*ens 0

我脑子里突然浮现出一些东西。由于通道 1 已被使用,我也遇到了类似的问题。如果您绑定到已在使用的通道,则可能会发生不好的事情。

sdptool browse local
Run Code Online (Sandbox Code Playgroud)

使用该命令查看您的 Ubuntu 设备上有哪些频道可用。