I have an XBee (S2C) connected to my Mac and another XBee connected to a TI microcontroller (TIVA-C129) communicating with each other - Mac as a coordinator and TI as a router.
I can communicate between them, but on the TI side, I can't read the exact data that is coming in the serial port.
On the Mac, I am running below python code that reads the incoming serial data through XBee and writes an acknowledgment.
#!/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)
On the TI side, I have below code
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); } }
When I run this, XBee communication stops after printing "I am R1" in the python console. I am sure Serial3.available() > 0 is working as when I replace it with a blink code like below, it works and XBee communication keeps working on.
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 }
So looks like the problem is in
incomingByte = Serial3.read();
From python, I am sending a string (%s) with ser.write('%s\n' % ack). Is Serial3.read() the right read mechanism for the ack string? Anything else?
FYI: I tested the serial.read() only with TI (no python involved) by writing something in the console and serial.read() can read and print it.