0

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.

nad
  • 2,640
  • 11
  • 55
  • 96

1 Answers1

0

Is it possible you're getting stuck on the Serial.print() on the TI side, maybe because you have hardware handshaking enabled for that port and it's waiting on CTS/RTS?

If you comment out those lines, does the program work? You could use strategically placed digitalWrite() calls to figure out how far you get inside of loop().

Also, I see that you're printing "Sending command to the XBee" to Serial3, so you should see that on the Python side as well.

Finally, you might want to change your if to a while, so your loop function processes all of the inbound bytes before getting stuck in the two one-second delays on each pass of the loop (limiting throughput to a single character every two seconds).

tomlogic
  • 11,489
  • 3
  • 33
  • 59