如何使用python 3从arduino获取模拟值?

问题描述 投票:1回答:1

[我正在尝试连接一个电位计,该电位计返回0到1023之间的整数值。我正在尝试通过蓝牙将该数据传输到python。

确实正在传输数据,因为在旋转电位计时确实会在屏幕上获取值。但是它没有显示为整数,而是显示为:b'\xff'

我实际上知道0是b'\x00',而1023是b'\xff',我不知道那是什么意思。有人可以提供修复程序,以便打印0到1023之间的数字吗?

import bluetooth

print ("Searching for devices...")
print ("")
nearby_devices = bluetooth.discover_devices ()
num = 0
print ("Select your device by entering its coresponding number...")
for i in nearby_devices:
    num += 1
    print (num, ": ", bluetooth.lookup_name (i))

selection = int (input ("> ")) - 1
print ("You have selected", bluetooth.lookup_name (nearby_devices[selection]))
bd_addr = nearby_devices[selection]

port = 1

sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))

while True:

    data = sock.recv(1)
    print (data)

谢谢!

python arduino bluetooth
1个回答
0
投票

它以似乎是十六进制格式发送数字:

而不是用0、1、2、3、4、5、6、7、8、9、10来计数:

0、1、2、3、4、5、6、7、8、9,A,B,C,D,E,F,10

现在以这种格式,A表示数字10,B表示数字11,依此类推。 F是数字15。现在10并不像在十进制系统中那样表示“ 1 * 10 + 0 * 1”,而是表示“ 1 * 16 + 0 * 1”。因此hexa-10 = deci-16。

但是请注意FF不会not给出1023。相反,它给出255。对于更大的数字,您需要接收更多的叮咬。您确定已阅读所有相关数据吗?

现在已经解决了,实际上数据是作为bytes发送的,您必须将它们转换回int:Convert bytes to int?

© www.soinside.com 2019 - 2024. All rights reserved.