receiving dat from Pi serial USB port

Hello, I am sending a servo position dat from the Pi to an Arduino. The on the Pi side-the sender:

#!/usr/bin/python3

from time import sleep
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # Establish the connection on a specific port

while True:
    deg=180
    deg_b=deg.to_bytes(1, 'little')

    ser.write(deg_b) #send the degree in one byte to Aruino
    print (ser.readline()) # Read the newest output from the Arduino
    sleep(.1) # Delay for one tenth of a second

On the receiver-Arduino

void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
char inByte = ' ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
  
if(inByte) my_blink(12);
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}//if

}//loop

void my_blink(int pin) {
  digitalWrite(pin, HIGH);
  delay(100);
  digitalWrite(pin, LOW);
  delay(100);
}

This send deg=180 from Pi to Arduino in one single byte, Arduino reads it the send it back to the Pi, I get b'\xb4\r\n', but how about the char inByte = ' '; ? What is the format in inByte = ' ';? how do I convert this inByte back to an integer 180 ? Thanks.

0xB4 is 180 in decimal, so it's the same value. Try to save it into a variable and check the value. If you want to communicate with direct byte values, don't use println() method but the print() or better write() method.

If you want to communicate with direct byte values, don't use println() method but the print() or better write() method.

print() will still convert the value to a string prior to sending. The only function to use to send binary data is write().

Thank you for the replies. I tried to use Serial.print(inByte) and Serial.write(inByte), neither of them output anything from the sketch to my python. :(

All I need to do is having a servo degree in the python script, then send to the Arduino sketch, use a switch statement,

switch(inByte) {
   case :
     pos =inByte;
   break;
.
.
.
.
   default:
   break;
}

How can I find out what value the Pi send to the Arduino?

print() will still convert the value to a string prior to sending. The only function to use to send binary data is write().

This is not correct. If the argument to the print() method is a character, the character's value is directly transfered using the write() method. But the detour over the print() method is not necessary and might lead to future errors.

I tried to use Serial.print(inByte) and Serial.write(inByte), neither of them output anything from the sketch to my python.

Try using ser.read() instead of ser.readline() in your python script. And don't use print to put a byte value out as it will be made "printable" by converting it to hex format. If you have problem programming the Pi in python, go to a Pi forum. The Arduino code does what you told us you expect it to do.

pylon: This is not correct. If the argument to the print() method is a character, the character's value is directly transfered using the write() method. But the detour over the print() method is not necessary and might lead to future errors.

Try using ser.read() instead of ser.readline() in your python script. And don't use print to put a byte value out as it will be made "printable" by converting it to hex format. If you have problem programming the Pi in python, go to a Pi forum. The Arduino code does what you told us you expect it to do.

pylon: This is not correct. If the argument to the print() method is a character, the character's value is directly transfered using the write() method. But the detour over the print() method is not necessary and might lead to future errors.

Try using ser.read() instead of ser.readline() in your python script. And don't use print to put a byte value out as it will be made "printable" by converting it to hex format. If you have problem programming the Pi in python, go to a Pi forum. The Arduino code does what you told us you expect it to do.

pylon: This is not correct. If the argument to the print() method is a character, the character's value is directly transfered using the write() method. But the detour over the print() method is not necessary and might lead to future errors.

Try using ser.read() instead of ser.readline() in your python script. And don't use print to put a byte value out as it will be made "printable" by converting it to hex format. If you have problem programming the Pi in python, go to a Pi forum. The Arduino code does what you told us you expect it to do.

Thanks for the reply. "If you have problem programming the Pi in python, go to a Pi forum." LOL, I did go to Pi forum. I went to one Pi forum, I got the same reply "If you have problem programming the Arduino, go to a Arduino forum. Reminded me that when I call Comcast, they send me to my router maker, when call the router make, they bounce me back to Comcast. Haha. Is there an ArduiPi forum to fill the gap? :p

Anyhow, as far as I concern, the int 180 is encoded in a single byte \xb4 in python presentation. I guess it is in 0x4b in C presentation. in python there is a utility function int.from_bytes(,,,) that converts a single byte back into int. Is there a similar function on Arduino?

Is there a similar function on Arduino?

There is a one line command to do it.

byte msb;
byte lsb;

int theInt = (int)msb << 8 + lsb;

The only question is which of the two bytes you read is msb and which is lsb.

But, I thought you were sending one byte in and one byte out, so msb should be 0 and lsb should be the byte you send/receive.

It is not clear why the nibbles seem to be reversed in the lsb.

Have a look at this Python - Arduino demo and at Serial Input Basics

Debugging is much easier if you send data in human readable form. I would only send binary data as a last resort if the extra speed is essential.

...R