[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);
}