我正在运行一个Arduino程序,该程序将“串行波特”设置为19200。我想使用PySerial库提取串行数据。但是,只有在波特率处于115200时,PySerial才可以工作。

这是我的Arduino setup()的样子:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  XBee.begin(19200);// was 9600
}


这是设置为19200时PySerial代码的外观:

with serial.Serial('COM19', 19200) as ser:
    x = ser.read(8)          # 6 works for reading in 2 variables
    print(x)


输出:


  b'\ xf3 \ xea \ xf6 \ xea \ xea \ xf8'
  
  b'\ xf8 \ xf6 \ xf3 \ xfc \ xfc \ xfc'


这是设置为115200时PySerial代码的外观:

with serial.Serial('COM19', 115200) as ser:
    x = ser.read(8)          # 6 works for reading in 2 variables
    print(x)


输出:


  b'70 \ r \ n72'
  
  b'72 \ r \ n70'


有谁知道为什么它仅适用于115200,而不适用于19200?或者我可以怎么做才能将该数据类型转换为十进制数字?

谢谢

最佳答案

波特率取决于设备。它实质上决定了设备之间的数据速率。您需要检查XBee型号的数据表以确定兼容的波特率。通常,通常使用9600和115200,因为使用的非标准波特率因产品而异。

关于python - 为什么PySerial脚本使用115200波特而不是19200波特?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55486069/

10-16 04:37