【发布时间】:2016-04-27 21:43:37
【问题描述】:
我有一个 XBee (S2C) 连接到我的 Mac,另一个 XBee 连接到一个 TI 微控制器 (TIVA-C129),它们相互通信 - Mac 作为协调器,TI 作为路由器。
我可以在它们之间进行通信,但在 TI 方面,我无法读取串行端口中的确切数据。
在 Mac 上,我在 python 代码下运行,该代码通过 XBee 读取传入的串行数据并写入确认。
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/tty.usbserial-A104IC2U', 9600)
ack='A'
while True:
incoming = ser.readline().strip()
if incoming != 'A':
print '%s' % incoming
ser.write('%s\n' % ack)
在 TI 方面,我有以下代码
int incomingByte = 0;
void setup()
{
Serial3.begin(9600); //UART3 has XBee connection
pinMode(LED, OUTPUT);
}
void loop()
{
Serial3.println("Sending command to the XBee");
delay(1000);
Serial3.println("I am R1");
delay(1000);
if (Serial3.available() > 0) {
// read the incoming byte from UART3
incomingByte = Serial3.read();
// say what you got, print at the usb serial console
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
当我运行它时,XBee 通信在 python 控制台中打印“我是 R1” 后停止。我确信 Serial3.available() > 0 可以正常工作,因为当我用如下所示的闪烁代码替换它时,它可以正常工作并且 XBee 通信继续正常工作。
if (Serial3.available() > 0) {
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
看来问题出在
incomingByte = Serial3.read();
从 python,我正在发送一个带有 ser.write('%s\n' % ack) 的字符串 (%s)。 Serial3.read() 是 ack 字符串的正确读取机制吗?还有什么?
仅供参考:我仅使用 TI(不涉及 python)测试了 serial.read(),方法是在控制台中编写一些东西,serial.read() 可以读取和打印它。
【问题讨论】:
标签: python serial-port uart xbee