[解决]问题在于我使用的USB-TTL PL2303芯片将XBee模块与Pi接口。它制造了这个问题。它的驱动程序没有得到RPi2的正确支持。
我正试图从我的覆盆子Pi2上的python脚本通过一个连接到它的XBee模块将一个字符串(可能是一个数字)发送到一个Arduino Uno。发送的数据在Arduino端被曲解了。当我使用X-CTU上的终端并通过它发送字符串时,它会正确地显示在ArduinoIDE的串行监视器上。
下面是我正在使用的Python代码

import time
import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.isOpen()
x= '4'
ser.write(bytes(x, "ascii")) #writing as bytes
time.sleep(2)
ser.close()

这是我使用的Arduino代码
#include <SoftwareSerial.h>


SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
// Open serial communications
Serial.begin(9600);


// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}

void loop() // run over and over
{
 int x;
 if (mySerial.available())
  {

    x = char(mySerial.read()) - '0';
   //reading data value from Software Serial port
   //converting ASCII to int
   //and storing it as x
   Serial.print(x);

   }

最佳答案

我猜,如果您已经将类型转换为char,则不需要减去“0”,因为Serial.print()会将该值解释为一个ascii代码并打印出相应的字符。所以试试,char(Serial.read())并打印出来。

07-24 09:45
查看更多