简体   繁体   English

在 Raspberry Pi 和计算机之间使用带有 Python 套接字库的蓝牙串行端口配置文件发送数据

[英]Sending data using Bluetooth Serial Port Profile with Python socket library between Raspberry Pi and computer

I have been able to connect a sensor via Bluetooth to my Raspberry Pi and receive its information with the Serial Port Profile using the Python Socket library with this program:我已经能够通过蓝牙将传感器连接到我的 Raspberry Pi,并使用带有此程序的 Python Socket 库通过串行端口配置文件接收其信息:

import socket

sensorMACAddress = 'XX:XX:XX:XX:XX:XX'

port = 1

s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((sensorMACAddress,port))

s.send(bytes('#ob', 'UTF-8'))  # Tells the sensor to send the data in bytes

while(1):

   data = s.recv(12)


   while len(data) < 12:

       data = data + s.recv(12)


   Y = struct.unpack('<f', data[0:4])[0]
   P = struct.unpack('<f', data[4:8])[0]
   R = struct.unpack('<f', data[8:12])[0]

   YPR = str(['S3', int(Y), int(P), int(R)])

   print(YPR)
   print('\n')

(The sensor sends the data automatically once connected.) (传感器一旦连接就会自动发送数据。)

Now I want to send the information of this sensor to my computer, so I just updated the prior program by creating a new socket object for the connection between the Raspberry and the computer to send the information with the send() function:现在我想把这个传感器的信息发送到我的电脑上,所以我刚刚更新了之前的程序,创建了一个新的套接字 object 用于连接树莓派和电脑,用 send() function 发送信息:

# This goes outside the loop
computerMACAddress = 'XX:XX:XX:XX:XX:XX'
s_comp = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s_comp.connect((computerMACAddress,port))


# This goes inside the loop
s_comp.send(YPR.encode())

Apparently this program is sending information, and it does connect via Bluetooth to my computer (or my phone) but my computer (Windows) doesn't seem to receive it, and neither does my phone (Android).显然这个程序正在发送信息,它确实通过蓝牙连接到我的电脑(或我的手机),但我的电脑(Windows)似乎没有收到它,我的手机(Android)也没有。

Following this steps ( https://scribles.net/setting-up-bluetooth-serial-port-profile-on-raspberry-pi/ ) I was able to send and receive information between my Raspberry Pi and my computer via Bluetooth using their respective keyboards, using the Bluetooth Serial Port Profile, so the problem seems to be on the sockets and not on the Bluetooth Serial Port Profile connection.按照这些步骤( https://scribles.net/setting-up-bluetooth-serial-port-profile-on-raspberry-pi/ )我能够使用他们的蓝牙在我的Raspberry Pi和我的计算机之间发送和接收信息相应的键盘,使用蓝牙串行端口配置文件,所以问题似乎出在 sockets 上,而不是蓝牙串行端口配置文件连接上。 The following simple program didn't work either:以下简单程序也不起作用:

import socket

computerMACAddress = 'XX:XX:XX:XX:XX:XX'

port = 1

s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((computerMACAddress,port))


data = 'Hello world!'

while 1:
   s.send(bytes(data, 'UTF-8'))
   print(data)

On this example ( http://blog.kevindoran.co/bluetooth-programming-with-python-3/ ) I saw that they create a client program to send the information and a server program to receive it, but I didn't have to do this to receive information from the sensor to the Raspberry, so I don't know why I would need to do it with the computer.在这个例子中( http://blog.kevindoran.co/bluetooth-programming-with-python-3/ )我看到他们创建了一个客户端程序来发送信息和一个服务器程序来接收它,但我没有必须这样做才能从传感器接收信息到 Raspberry,所以我不知道为什么我需要用计算机来做。 In any case, I tried but I couldn't make it work.无论如何,我尝试过,但我无法让它发挥作用。 Apparently, sockets don't work very well in Windows, but with my Android phone I couldn't receive information either, even though I could establish a Bluetooth connection.显然,sockets 在 Windows 中不能很好地工作,但是使用我的 Android 手机我也无法接收信息,即使我可以建立蓝牙连接。

So, in summary:所以,总结一下:

  1. Why did the send() and recv() functions work with the sensor but not with the computer or phone?为什么 send() 和 recv() 函数可以在传感器上工作,但不能在电脑或手机上工作?
  2. If I need a server program, why didn't I need it to receive information from the sensor, and how can I make it work?如果我需要一个服务器程序,为什么我不需要它来接收来自传感器的信息,我怎样才能让它工作?
  3. If that's not the problem, what is it?如果这不是问题,那是什么?

Thank you.谢谢你。

It looks like there is a Bluetooth Serial Port Profile (SPP) server running on the sensor and so the SPP client on the Raspberry Pi connects and exchanges information successfully.看起来传感器上运行着蓝牙串行端口配置文件 (SPP) 服务器,因此 Raspberry Pi 上的 SPP 客户端成功连接并交换信息。

Python sockets for Bluetooth SPP on Windows was only introduced in Python 3.9 so you will need a recent version of Python on your Windows computer. Python sockets for Bluetooth SPP on Windows was only introduced in Python 3.9 so you will need a recent version of Python on your Windows computer.

However, if it has connected, then maybe you need to go hunting for the serial port it has connected it to.但是,如果它已连接,那么您可能需要 go 寻找它已连接到的串行端口。 Maybe the following will help: https://www.instructables.com/Raspberry-Pi-Bluetooth-to-PuTTY-on-Windows-10/也许以下内容会有所帮助: https://www.instructables.com/Raspberry-Pi-Bluetooth-to-PuTTY-on-Windows-10/

However a more typical setup would be that the Raspberry Pi would be a bridge between the sensor and the network.然而,更典型的设置是 Raspberry Pi 将成为传感器和网络之间的桥梁。 This means that the value from the sensor goes to the RPi by Bluetooth.这意味着来自传感器的值通过蓝牙传输到 RPi。 Then the RPi makes the sensor data available over WiFi/internet via a website/socket running on the RPi.然后,RPi 通过在 RPi 上运行的网站/套接字通过 WiFi/互联网提供传感器数据。

As a side note, you could be a little more efficient with your unpacking of the data.作为旁注,您可以更高效地解包数据。 For example:例如:

>>> data = b'\x11\x11\x11\x11\x22\x22\x22\x22\x33\x33\x33\x33'
>>> import struct
>>> Y, P, R = struct.unpack('<fff', data)
>>> print(f"['S3', {Y}, {P}, {R}]\n")
['S3', 1.1443742118159064e-28, 2.1973163753312686e-18, 4.17232506322307e-08]

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

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