简体   繁体   English

在 Linux 中通过蓝牙访问来自 Neurosky Mindset 的串行数据

[英]Accessing serial data from Neurosky Mindset over bluetooth in linux

I am trying to access the serial data stream being transmitted from the Neurosky Mindset over bluetooth.我正在尝试访问通过蓝牙从 Neurosky Mindset 传输的串行数据流。 It is a step in using ThinkGear Communications Protocol mentioned here:这是使用这里提到的 ThinkGear 通信协议的一个步骤:

http://developer.neurosky.com/docs/doku.php?id=which_api_is_right_for_me http://developer.neurosky.com/docs/doku.php?id=which_api_is_right_for_me

There are a couple programs that should do this for me (Puzzlebox, Mindstream, etc.), I've either had apparently unsolvable dependency issues or they don't do exactly what I want.有几个程序应该为我做这件事(Puzzlebox、Mindstream 等),我要么有明显无法解决的依赖问题,要么他们没有完全按照我的意愿去做。

I am able to pair the Mindset with my machine using blueman (specifically blueman-manager) I am able to connect the serial port to /dev/rfcomm0.我能够使用 blueman(特别是 blueman-manager)将 Mindset 与我的机器配对我能够将串行端口连接到 /dev/rfcomm0。 What I am first wondering is: how do I read from this data stream.我首先想知道的是:我如何从这个数据流中读取。 If I run sdptool records on the device, I get the following output:如果我在设备上运行 sdptool 记录,我会得到以下输出:

Service Name: Dev B Service RecHandle: 0x10005 Service Class ID List: "Serial Port" (0x1101) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 3 Language Base Attr List: code_ISO639: 0x656e encoding: 0x6a base_offset: 0x100服务名称:Dev B 服务 RecHandle:0x10005 服务类 ID 列表:“串行端口”(0x1101) 协议描述符列表:“L2CAP”(0x0100)“RFCOMM”(0x0003) 通道:3 语言基础属性列表:code_ISO639:0x656e 编码: 0x6a 基偏移量:0x100

(I get more than that, but that is the service I'm trying to access.) (我得到的不止这些,但那是我想要访问的服务。)

I guess my main questions are: If cat /dev/rfcomm0 displays nothing does that mean that there is no data being transmitted?我想我的主要问题是:如果 cat /dev/rfcomm0 不显示任何内容,这是否意味着没有数据正在传输? Is the problem that it isn't being displayed because it is raw?是因为它是原始的而不显示的问题吗? Is there a command that will guarantee to give me any serial data if it is being transmitted?是否有命令可以保证在传输时给我任何串行数据? Is there a bluetooth standard command that starts SPP transmission (I've searched for documentation for bluez, but that hasn't been too fruitful)?是否有启动 SPP 传输的蓝牙标准命令(我已经搜索了 bluez 的文档,但效果不佳)?

I have run everything as both root and regular so that doesn't seem to fix it.我已经以 root 和常规身份运行了所有内容,因此似乎无法解决它。

Any help would be greatly appreciated.任何帮助将不胜感激。

OK, it took a bit of playing around to get the MindSet connected reliably.好的,为了让 MindSet 可靠地连接,需要花点时间。

The command-line bluetooth tools are more or less useless.命令行蓝牙工具或多或少没用。 The only repeatable way to read off the Mindset (once paired via BlueMan) was to do this:读取 Mindset 的唯一可重复方法(一旦通过 BlueMan 配对)是这样做:

  bash$ sudo /etc/init.d/bluetooth stop
  bash$ sudo /etc/init.d/bluetooth start
  bash$ sudo rfcomm connect /dev/rfcomm0 00:13:##:##:##:## 1
  bash$ ./mindset_capture.rb

...but this reaches EOF before a valid ThinkGear packet has been received. ...但这在收到有效的 ThinkGear 数据包之前就达到了 EOF。

Instead, pair using Blueman.相反,使用 Blueman 配对。

Setup:设置:

  1. bash$ blueman-manager & bash$ blueman-manager &
  2. enable pairing on MindSet在 MindSet 上启用配对
  3. Click Search in BlueMan在 BlueMan 中单击搜索
  4. Select MindSet device选择 MindSet 设备
  5. Click on Pair点击配对
  6. Enter PIN (0000)输入密码 (0000)

Connect:连接:

  1. bash$ blueman-manager & bash$ blueman-manager &
  2. Right-click on MindSet and select "Connect To: Dev B" (serial connector icon)右键单击 MindSet 并选择“连接到:Dev B”(串行连接器图标)
  3. Run capture utility (eg bash$ ./mindset_capture.rb)运行捕获实用程序(例如 bash$ ./mindset_capture.rb)
  4. Right-click on MindSet and select "Disconnect: Dev B"右键单击 MindSet 并选择“断开连接:Dev B”
  5. Quit Blueman退出蓝人
  6. Restart bluetooth to release rfcomm0 (thanks, BlueMan!)重新启动蓝牙以释放 rfcomm0(感谢 BlueMan!)

    bash$ sudo /etc/init.d/bluetooth stop; bash$ sudo /etc/init.d/bluetooth 停止; sudo /etc/init.d/bluetooth start须藤 /etc/init.d/bluetooth 启动

Wouldn't it be better to have a completely command-line driven solution?拥有一个完全由命令行驱动的解决方案不是更好吗? Yes, yes it would.是的,是的,它会。 Unfortunately, the Bluetooth stack on Linux seems to be in a state where the GUI tools and the command line tools do not cooperate -- and the command-line tools do not support pairing (* UPDATE - see comments).不幸的是,Linux 上的蓝牙堆栈似乎处于 GUI 工具和命令行工具不合作的状态——并且命令行工具不支持配对(* 更新 - 见评论)。

Reading from the device is straightforward.从设备读取很简单。 It continuously transmits bytes, so read from it until you encounter two SYNC bytes (0xAA), which mark the beginning of a packet.它连续传输字节,因此从中读取直到遇到两个 SYNC 字节 (0xAA),这标志着数据包的开始。 The structure of the packet is described in mindset_communications_protocol.pdf , which is supplied in the MDT.数据包的结构在Mindset_communications_protocol.pdf 中进行了描述,该文件在 MDT 中提供。

Ruby code for reading from the MindSet:从 MindSet 中读取的 Ruby 代码:

  require 'rubygems'      # gem install serialport
  require 'serialport' 
  conn = SerialPort.new "/dev/rfcomm0", 57600
  while true
    next if conn.readbyte != 0xAA
    next if conn.readbyte != 0xAA
    while true
      c = conn.readbyte
      break if c == 0xAA
      puts "%02X" % c
    end
  end 

It's not perfect as it does no real parsing, but that's how bytes are read from the device.它并不完美,因为它没有真正的解析,但这就是从设备读取字节的方式。

After a lot of trouble I found out that the chipset in many of the tiny new bluetooth dongles (including both the one that comes with the headset and another that I already had) cannot handle being plugged into a USB hub and must be plugged directly into the raspberry pi - even though it does show up in a list of devices.经过很多麻烦,我发现许多新的微型蓝牙加密狗(包括耳机附带的和我已经拥有的另一个)中的芯片组无法处理插入 USB 集线器,必须直接插入树莓派 - 即使它确实出现在设备列表中。 Once I moved it from the hub bluez-simple-agent worked beautifully and without fuss.一旦我将它从集线器中移出,bluez-simple-agent 就运行得很漂亮而且没有大惊小怪。

I have the old NeuroSky Mindwave and BrainLink_Lite neurogadgets and here I used BrainLink.我有旧的 NeuroSky Mindwave 和 BrainLink_Lite 神经小工具,在这里我使用了 BrainLink。 Neurosky and Brainlink work in the same way using the same protocol cause BrainLink used NeuroSky chip ( TGAM ). Neurosky 和 ​​Brainlink 使用相同的协议以相同的方式工作,因为 BrainLink 使用了 NeuroSky 芯片 ( TGAM )。 I solved that problem follow the next road:我按照下一条路解决了这个问题:

first I get:首先我得到:

$ dmesg | tail -n 50
....
[243709.823581] Bluetooth: hci0: BCM: chip id 63
[243709.839507] Bluetooth: hci0: ShisiusStation
[243709.840576] Bluetooth: hci0: BCM20702A1 (001.002.014) build 0000
[243709.840606] bluetooth hci0: Direct firmware load for brcm/BCM20702A1-13d3-3404.hcd failed with error -2
[243709.840608] Bluetooth: hci0: BCM: Patch brcm/BCM20702A1-13d3-3404.hcd not found
...

after connection via blueman-manager I get in dmesg output :通过blueman-manager连接后,我进入dmesg 输出

[254803.943803] Bluetooth: TIOCGSERIAL is not supported
[254804.299425] Bluetooth: bad checksum in packet
[254817.417375] Bluetooth: TIOCGSERIAL is not supported
[254817.417498] Bluetooth: TIOCGSERIAL is not supported
[254823.416967] Bluetooth: TIOCGSERIAL is not supported
[254865.049149] ERROR @wl_inform_single_bss : cfg80211_inform_bss_frame error

Thats OK!没关系! By the way I can't connect to BrainLink for about 1-2 secs via顺便说一句,我无法通过以下方式连接到 BrainLink 大约 1-2 秒

  bash$ sudo rfcomm connect /dev/rfcomm0 8C:DE:52:44:AB:23 1

Also I patched my Bluetooth device error (Direct firmware load for brcm/BCM20702A1-13d3-3404.hcd failed with error -2) following that doc link (without descibed module recompiling)我还修补了我的蓝牙设备错误(brcm/BCM20702A1-13d3-3404.hcd 的直接固件加载失败,错误 -2)跟随该文档链接(没有描述模块重新编译)

wget https://www.dropbox.com/s/krg2ulkxdrqxham/bluetooth-hogfan.tar.gz
tar -zxvf bluetooth-hogfan.tar.gz
cd bluetooth
sudo cp fw-13d3_3404.hcd /lib/firmware/brcm
sudo cp fw-13d3_3404.hcd /lib/firmware/brcm/BCM20702A1-13d3-3404.hcd
sudo modprobe -r btusb
sudo modprobe btusb

In the result I could connect BT using channel 6 (not 1,2,3,4,5, ...).结果我可以使用通道 6(不是 1、2、3、4、5,...)连接 BT。 I don't know why.我不知道为什么。

# in real rfcomm0 became rfcomm59 by the end of experimintations.
sudo rfcomm connect rfcomm0 8C:DE:52:44:AB:23 6
Connected /dev/rfcomm0 to 8C:DE:52:44:AB:23 on channel 6
Press CTRL-C for hangup

In another shell:在另一个外壳中:

$: sudo cat /dev/rfcomm0 | hexdump -C
...
000f7f80  00 7b 02 aa aa 04 80 02  00 2c 51 aa aa 04 80 02  |.{.......,Q.....|
000f7f90  ff f9 85 aa aa 04 80 02  ff f9 85 aa aa 04 80 02  |................|
000f7fa0  00 44 39 aa aa 04 80 02  00 5b 22 aa aa 04 80 02  |.D9......[".....|
000f7fb0  00 31 4c aa aa 04 80 02  00 07 76 aa aa 04 80 02  |.1L.......v.....|
000f7fc0  ff ba c4 aa aa 04 80 02  ff 7e 00 aa aa 04 80 02  |.........~......|
000f7fd0  ff b3 cb aa aa 04 80 02  ff f9 85 aa aa 04 80 02  |................|
000f7fe0  00 1b 62 aa aa 04 80 02  00 45 38 aa aa 04 80 02  |..b......E8.....|
000f7ff0  00 36 47 aa aa 04 80 02  00 27 56 aa aa 04 80 02  |.6G......'V.....|
000f8000  00 66 17 aa aa 04 80 02  00 c5 b8 aa aa 04 80 02  |.f..............|
000f8010  00 e6 97 aa aa 04 80 02  00 91 ec aa aa 04 80 02  |................|
000f8020  ff f3 8b aa aa 04 80 02  ff d3 ab aa aa 04 80 02  |................|
000f8030  00 28 55 aa aa 04 80 02  00 36 47 aa aa 04 80 02  |.(U......6G.....|
...

Details of data meaning are described here , here and here . 此处此处此处描述了数据含义的详细信息。 Sometimes it works fine for the long time and sometimes neuro-serial-stream brings EOF so it's useful to read /dev/rfcomm0 since some time after disconnect.有时它可以长时间正常工作,有时神经串行流会带来EOF,因此在断开连接一段时间后读取 /dev/rfcomm0 很有用。 In case of connection to channel 3 I got:如果连接到通道 3,我得到:

sudo rfcomm connect rfcomm0 8C:DE:52:44:AB:23 3
Connected /dev/rfcomm43 to 8C:DE:52:44:AB:23 on channel 3
Press CTRL-C for hangup
# since 10 secs it is disconnect....
Disconnected

Output is:输出是:

$ sudo cat /dev/rfcomm43 | hexdump -C
00000000  55 04 00 38 00 00 c4 55  04 00 38 00 00 c4 55 04  |U..8...U..8...U.|
00000010  00 38 00 00 c4 55 04 00  38 00 00 c4 55 04 00 38  |.8...U..8...U..8|
...
000000a0  c4 55 04 00 38 00 00 c4  55 04 00 38 00 00 c4     |.U..8...U..8...|
000000af
# (and disconnect)

By the bye, I try to run NeuroPy example and got attention measure only one time:).再见,我尝试运行NeuroPy 示例并且只获得了一次注意力度量:)。 Also I have to do next:另外我必须做下一步:

sudo chmod 777 /dev/rfcomm0
sudo python neuropyexample.py

BrainLink was deleted from blueman paired devices during the all described experiments.在所有描述的实验中,BrainLink 从 blueman 配对设备中删除。 Also I tested that during TuSion via-Art neuroapp development process.我也在TuSion via-Art 神经应用程序开发过程中测试了这一点 Here is Facebook community group .这是 Facebook社区组 My last phrase:我的最后一句话:

# Ubuntu 16.04 
$ uname -a
Linux SlavikBoss 4.4.0-81-generic #104-Ubuntu SMP Wed Jun 14 08:17:06 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

I solved this problem with reference of the answer by Jo Ja.我参考 Jo Ja 的回答解决了这个问题。 The detailed process is below:详细流程如下:

  1. hcitool scan ---find your the adress of your brainlink. hcitool scan ---找到你的brainlink的地址。 if it dosen't work, try it again, or use the bluetooh on your computer here is the image of bluetooth in ubuntu如果它不起作用,请再试一次,或者在您的计算机上使用蓝牙 这是 ubuntu 中的蓝牙图像

    $ hcitool scan Scanning ... B0:C0:90:B3:ED:7A DESKTOP-JA7BUAI 0D:00:18:A1:54:DD BrainLink_Lite

  2. sudo rfcomm connect /dev/rfcomm0 0D:00:18:A1:54:DD 2 -----the last number 2 means connect your brainlink on channel 2, this may different with computers, I gusess the reason is the frequency range of Bluetooth is from 2401mhz to 2479mhz, Each channel accounts for 1MHz, 79 channels in total. sudo rfcomm connect /dev/rfcomm0 0D:00:18:A1:54:DD 2 -----最后一个数字2表示将你的brainlink连接到通道2,这可能与计算机不同,我猜是频率范围的原因蓝牙从2401mhz到2479mhz,每个通道占1MHz,共79个通道。 So you should have a try which channel is work in your computer.因此,您应该尝试一下您的计算机中的哪个频道正在工作。

    $ sudo rfcomm connect /dev/rfcomm0 0D:00:18:A1:54:DD 2 Connected /dev/rfcomm0 to 0D:00:18:A1:54:DD on channel 2 Press CTRL-C for hangup

    /dev/rfcomm1 or /dev/rfcomm2 or other number is ok, then rfcommX will appear in the path of '/dev/', and disappear when you disconnect. /dev/rfcomm1 或 /dev/rfcomm2 或其他数字都可以,然后 rfcommX 将出现在“/dev/”路径中,断开连接时消失。

    use the command $ rfcomm -i --device , it can be seen that your brainlink has been connected to 0C:DD:24:16:ED:69(your local bluetooth adress)使用命令$ rfcomm -i --device ,可以看到你的brainlink已经连接到了0C:DD:24:16:ED:69(你本地的蓝牙地址)

    $ rfcomm -i --device rfcomm0: 0C:DD:24:16:ED:69 -> 0D:00:18:A1:54:DD channel 2 connected [reuse-dlc release-on-hup tty-attached]

  3. In another shell:在另一个外壳中:

    sudo cat /dev/rfcomm0 | hexdump -C 00000000 7a aa aa 04 80 02 00 6c aa aa 04 80 02 00 21 5c |z......l......!\\| 00000010 aa aa 04 80 02 00 31 4c aa aa 04 80 02 00 35 48 |......1L......5H| 00000020 aa aa 04 80 02 00 6a aa aa 04 80 02 ff ef 8f aa |......j.........| 00000030 aa 04 80 02 00 06 77 aa aa 04 80 02 00 34 49 aa |......w......4I.| 00000040 aa 04 80 02 63 aa aa 04 80 02 00 0a 70 aa aa 04 |....c.......p...| 00000050 80 02 00 17 66 aa aa 04 80 02 00 16 67 aa aa 04 |....f.......g...| 00000060 80 02 00 24 59 aa aa 04 80 02 00 34 49 aa aa 04 |...$Y......4I...| 00000070 80 02 00 3b 42 aa aa 04 80 02 00 31 4c aa aa 04 |...;B......1L...| 00000080 80 02 00 6a aa aa 04 80 02 00 01 7c aa aa 04 80 |...j.......|....| 00000090 02 00 6a aa aa 04 80 02 00 20 5d aa aa 04 80 02 |..j...... ].....| 000000a0 00 04 79 aa aa 04 80 02 ff f5 89 aa aa 04 80 02 |..y.............|

    if the output is like below, it means that in step2 you donot select the right channel, you should try the right channel in your computer:如果输出如下所示,则说明在step2中您没有选择正确的通道,您应该在计算机中尝试正确的通道:

    $ sudo cat /dev/rfcomm0 | hexdump -C 00000000 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 02 00 |.U.....U.....U..| 00000010 ee 10 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 |...U.....U.....U| 00000020 02 00 ee 10 ff 55 02 00 ee 10 ff 55 02 00 ee 10 |.....U.....U....| 00000030 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 02 00 |.U.....U.....U..| 00000040 ee 10 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 |...U.....U.....U| 00000050 02 00 ee 10 ff 55 02 00 ee 10 ff 55 02 00 ee 10 |.....U.....U....| 00000060 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 02 00 |.U.....U.....U..| 00000070 ee 10 ff 55 02 00 ee 10 ff 55 02 00 ee 10 ff 55 |...U.....U.....U|

  4. run NeuroPy example and got attention.运行NeuroPy 示例并引起注意。 The NeuroPy sample is work in python2, you should change it make it works in python3 NeuroPy 示例在 python2 中工作,您应该更改它使其在 python3 中工作

    $ sudo python neuropyexample.py

    Don't forget run in sudo mode, and stop the $ sudo cat /dev/rfcomm0 | hexdump -C不要忘记以 sudo 模式运行,并停止$ sudo cat /dev/rfcomm0 | hexdump -C $ sudo cat /dev/rfcomm0 | hexdump -C terminal with ctrl+c in advance. $ sudo cat /dev/rfcomm0 | hexdump -C终端提前 ctrl+c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM