0

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() > 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.

3
  • 2
    You're receiving a different type than you're transmitting. Dec 2, 2017 at 23:25
  • 2
    Learn the meaning of ASCII...
    – Majenko
    Dec 2, 2017 at 23:29
  • 46 45 51 48 45 53 52 45 49 52 45 52 55 55.... . - 3 0 - 5 4 - 1 4 - 4 7 7...
    – dda
    Dec 3, 2017 at 8:00

1 Answer 1

3
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.