Search code examples
pythonarduinoserial-portpyserialxbee

Loss of information using XBee


I am trying to establish communications via serial port between a PC with Ubuntu 14.04LTS and my RoMeo Arduino Board (Atmega 328). The used serial interface are 2 Xbee modules, one at PC and the other at the board.

Firstly, I am trying to develop a simple program to send messages to the board and receive them back. The code I use for the Arduino board is the following:

void loop(void)
{
char msg;
if (Serial.available()){

  msg = Serial.read();


  msg = Serial.print(msg);
}
}

When I send a unique character, the PC receives it back correctly. However, the problem I am facing is that for longer strings, the following characters are misspelled, as I obtain back strange hex numbers, as follows:

>>> import serial
>>> ser = serial.Serial(port='/dev/ttyUSB0', baudrate=57600, timeout=0.1)
>>> ser.write('H')
>>> ser.read(1)
'H'
>>> ser.write('Hello')
>>> ser.read(5)
'H\x8b\xbd'

Thanks in advance.

EDIT: Seems like there is an overflow problem with the XBee modules, but I can not figure it out completely: The problem is solved if I wait 0.01 seconds or more between sent characters, which is a huge amount of time. Namely, the code I use now for sending a word is:

for letter in word:
   serial.write(letter)
   time.sleep(0.01)

However, this waiting time is only needed when sending data from the PC to the Arduino. When the communication goes the other way (Arduino sends data to PC), I do not need to sleep and bytes are correctly sent all together at 57600 bauds.


Solution

  • The reason why the PC could not send more than 1 character to the Arduino board was that there was an XBee module configured with different port parameters than both the other module and the pyserial instance. In this case, the communication was established in Python with the following main parameters:

    • Baud rate: 57600

    • Bytesize: 8

    • Parity: None

    • Stop bits: 1

    If one of this parameters is different in one of the XBee modules, the communication will be faulty (like this case) or even impossible.

    To check the XBee configuration, the Digi XCTU application can be used: With the RF modules connected to the PC, open the program and read their configuration. Once opened, it has to be made sure that the 'Serial interfacing' options are equal to the previously listed.

    XCTU interface with the Serial options opened

    At the image, it is shown the menu where these options can be changed. Note that the Stop bits and the Bytesize can not be configured. The first parameter was not adjustable until the XB24-ZB versions, and the last one seems to not be possible to change.

    In the case of this question, the wrong parameter was the parity, as it was set to space parity in one of the modules, instead of no parity. Thus, the first byte was sent correctly, but after it the data was not coherent. After changing this parameter, the connection run OK.