Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Output Value: b'[value]\r\n' - Python Serial Read

I'm reading a value through Python from the serial port of a sensor of Arduino.

My code(Python):

arduino = serial.Serial(2, 9600, timeout=1)
print("Message from arduino: ") 
while True:
   msg = arduino.readline()
   print(msg)

I don't know why the output result is something like b'[sensor-value]\r\n'. So, I get something like b'758\r\n' b'534\r\n' b'845\r\n' etc (regarding to sensor change value).

How I convert this?

like image 969
Anderson Anizio Avatar asked Jul 21 '15 02:07

Anderson Anizio


1 Answers

You need to decode it.

print(msg.decode('utf-8'))

Please check Lexical Analysis on Python 3 documentation to see what string prefixes means

like image 51
changhwan Avatar answered Oct 01 '22 11:10

changhwan