Getting wrong values from serial.read() connected to a Raspberry Pi I set up a serial connection and sending integers from a Raspberry Pi to an Arduino.

This is the Python code in my Raspberry Pi:

import serial
ser = serial.Serial('/dev/ttyACM2', 9600)
ser.write(str(x_value));


And this is the command line output for x_value:


  -38 -38 -38 -40 - 38 - 39 - 40 - 12 18 26 25 49...


They are x coordinate values I generated and they range from -100 to 100. And here's the Arduino code for receiving serial data:

int ByteReceived;

void loop() {
  while(1) {
    if (Serial.available() &gt; 0) {
      ByteReceived = (Serial.read());
      Serial.print(ByteReceived);
      Serial.print("\n");   
    }
  }


On the serial monitor I'm receiving these numbers:


  46 45 51 48 45 53 52 45 49 52 45 52 55 55....


They are numbers between 40-60 and no way proportional to the values I sent from serial.write.

I'm really confused right now. Can someone help me to figure out what caused this problem?

Thank you so much.
ser.write(str(x_value));


Writes the string representation of the integer x_value, which is a sequence of ASCII characters.

ByteReceived = (Serial.read());
Serial.print(ByteReceived);


Reads a character (byte) and prints to the value (again as a sequence of characters).

45 51 48 45 53 52 45 49 52 45 52 55 55....


45 is ASCII character value for the character '-', 51 is the character '3', 48 is '0' and so on.

Cheers!