Loss of information using XBee
<p>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.</p>

<p>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:</p>

<pre><code>void loop(void)
{
char msg;
if (Serial.available()){

  msg = Serial.read();


  msg = Serial.print(msg);
}
}
</code></pre>

<p>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:</p>

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

<p>Thanks in advance.</p>

<p>EDIT: Seems like there is an overflow problem with the  <a href="https://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Manual.pdf" rel="nofollow">XBee modules</a>, 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:</p>

<pre><code>for letter in word:
   serial.write(letter)
   time.sleep(0.01)
</code></pre>

<p>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.</p>

<p>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:</p>

<ul>
<li><p>Baud rate: 57600</p></li>
<li><p>Bytesize: 8</p></li>
<li><p>Parity: None</p></li>
<li><p>Stop bits: 1</p></li>
</ul>

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

<p>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.</p>

<p><a href="http://i.stack.imgur.com/tiU8E.png" rel="nofollow"><img src="http://i.stack.imgur.com/tiU8E.png" alt="XCTU interface with the Serial options opened"></a></p>

<p>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 <a href="http://www.digi.com/wiki/developer/index.php/Setting_Serial_Adapter_Baud_Rate" rel="nofollow">seems to not be possible to change</a>.</p>

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

python arduino serial-port pyserial xbee