python data format to C data format

Hello, I am writing a python code to write data to the serial port for Anduino to receive.

Pi side

pos_i=50 #degree in the range of 0-180

pos_b=pos_i.to_bytes(1, 'little') # squeeze the degree value into one single byte

while True:
	ser.write(pos_b) #write the single byte to serial

Duino side

void loop() {
  if (!Serial.available()>0)
    byte inByte =;
    int pos = int(inByte); //inByte;

Here is the question, in python byte’s format is in 180 is \xb4 but in c the format is 0xb4, o when Arduino receives the byte fro python, does it auto magically convert to the c format? Is there a utility function to convert python format to c format? Thanks

You're talking about two different ways to WRITE the same number. What gets sent over from one to the other is always in binary. But it is the same number. I don't understand what you think you need to convert.

This Python - Arduino demo may help.

Also Serial Input Basics - simple reliable ways to receive data.

I strongly recommend sending data in human-readable form as it makes debugging very much easier. If you really need to send numbers in their binary format then the Python pack function can be used.


  if (!Serial.available()>0)
    byte inByte =;

If nothing is available, read it.

Seems strange to me.

On On Raspbian Lite (jessie) only Python 2 was installed, I wanted to use Python3 with Pyserial, so some things to install first...

sudo apt install python3 python3-pip
pip3 install pyserial

I generally like to send textual messages to things, so perhaps sending the ASCII code for 'a' is a good starting place.

import serial, struct, time
ser = serial.Serial('/dev/ttyUSB0',38400, timeout=3)
time.sleep(3) # wait for the bootloader to run
if ser.in_waiting:
    junk = ser.readline().strip() # clean any junk from the buffer
    print("junk: " + junk[-10:].decode("utf-8")) 
a = ord('a') # send the code for 'a'
# Python 3.6 strings are utf-8, they seem to need encode to ascii for Pyserial
echo = ser.readline().strip()
print("debug: " + echo[-10:].decode("utf-8"))

On the Arduino side, the UART will receive the ASCII value and it can then be compared (e.g. inByte == 'a')

Arduinos (GCC) compiler knows the ASCII codes but I have had some pain getting Python to send them.

I have an example of using that 'a' form Pyserial to stop a blinking ATmega328p, unfortunately, it is not an Arduino program it is using the raw toolchain for AVR on Raspbian.