2

I use the pyserial library to get data from an Arduino. I use the fallowing code:

import serial
import JSON

ser = serial.Serial('/dev/ttyUSB0', 9600)
while True:
    print(ser.readline())

When I run the program I get: b'{"x":"-1","y":"0"}\r\n' but I expect {"x":"-1","y":"0"}.

I know that I can remove \r\n by using stipe() method on ser,redline(). I also noticed that b' is from bytes.

When I try load string to json by command data = json.load(ser.readline().strip()) I get an error AttributeError: 'bytes' object has no attribute 'read'

How can I parse the string to json format?

trojek
  • 3,078
  • 2
  • 29
  • 52

1 Answers1

2

using your_line.decode('utf-8') to decode your line

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125