3

[SOLVED] the problem was with the USB-TTL PL2303 chip I was using to interface the XBee module with the Pi. It was creating the problem. It's drivers were not properly supported by the RPi2.

I am trying to send a string (possibly a number) from a python script on my Raspberry Pi2 through a XBee module connected to it, to an Arduino Uno. The data sent is being misinterpreted at the Arduino end. When I use the terminal on X-CTU and send strings through that it shows up correctly on the serial monitor of Arduino IDE.

Here is the Python Code I am using

import time
import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.isOpen()
x= '4'
ser.write(bytes(x, "ascii")) #writing as bytes
time.sleep(2)
ser.close()

Here is the Arduino code I used

#include <SoftwareSerial.h>


SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
// Open serial communications
Serial.begin(9600);


// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}

void loop() // run over and over
{ 
 int x;
 if (mySerial.available())
  {

    x = char(mySerial.read()) - '0'; 
   //reading data value from Software Serial port 
   //converting ASCII to int
   //and storing it as x
   Serial.print(x);

   }
John Pheonix
  • 31
  • 1
  • 4

2 Answers2

0

My guess, if you are already typecasting to char, you don't need to subtract '0', because then Serial.print() will interpret that value as an ascii code and print the corrosponding character. So just try, char(Serial.read()) and print it.

Aditya Shinde
  • 315
  • 1
  • 12
  • I need the string value as an integer to switch a relay switch on for that much number of minutes, that is why I subtract it from '0', – John Pheonix Dec 05 '15 at 15:49
0

Well, you know the code on the receiving end is working correctly because you can test it by sending data with the X-CTU terminal. How are you sending from X-CTU? Just typing the number 4 in the window, or sending it as a hex value (0x04)?

What happens when you have the Python script send to the X-CTU terminal? What do you see? What if you just dump the value of the byte read on the Arduino side before doing any conversions to it? Compare what X-CTU sends to what Python sends.

Instead of using bytes() to convert your Python string, you could just assign x = b'4' and see what that does.

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • When I use the X-CTU to receive data from the python script it shows up correctly on the X-CTU terminal. And when i send data from the X-CTU terminal to the Arduino, it shows up correctly on the Serial monitor. The problem arises when I use python script from Pi and Arduino together. The data sent from Pi is being received as gibberish at the arduino end. I just type the number directly onto the the Console log. The arduino code works correctly when used with the X-CTU. I cant use the b prefix notation as the string i am sending is supposed to be a variable. – John Pheonix Dec 06 '15 at 06:32
  • There must be some difference between how the Python's data appears in X-CTU, and how you're sending from X-CTU to the Arduino. Can you have the Arduino dump the hex values it's receiving, before any conversion, to compare what you get from Python versus what you get from X-CTU? – tomlogic Dec 07 '15 at 19:00
  • I did as you said by changing Serial.print(x) to Serial.print(x, HEX) and it seems that the data being sent by python code is 32 bits as displayed on the serial monitor and data being sent by X-CTU is 8 bits. Python code data is displayed as a sequence of 4 digit hex values while data from X-CTU is displayed as a 2 digit hex value. – John Pheonix Dec 10 '15 at 06:17
  • That'd be 16 bits (4 hexits). What are the actual hex values you're seeing? – tomlogic Dec 11 '15 at 08:17
  • When i send string '0' from python script i am getting FFFFFFFE on serial monitor of Arduino. Then after that if I send '1', im getting value FFFFFFA63AFFFFFFFF on serial monitor. Totally strange values. I am pulling my hair out here. – John Pheonix Dec 17 '15 at 15:55
  • Sounds like the wrong baud rate. The baud rate of each host should match the `ATBD` setting of its local XBee. – tomlogic Dec 17 '15 at 20:09
  • I didn't touch the baud rate of the xbee modules while configuring and they return code 3 when 'ATBD' command is issued. – John Pheonix Dec 20 '15 at 05:20