I want to run a python program that receives the data through serial communication and saves it to a file, but after receiving the data this error shows up. Any idea? (code runs ok in Windows but in Raspbian OS I have this error)(the data is in JSON format)
error:
Traceback (most recent call last): File "/home/pi/Desktop/ReadFromPort.py", line 11, in line = ser.readline().decode("utf-8") UnicodeDecoderError: 'utf-8' codec can't decode byte 0xbd in position 2: invalid start byte
Python code:
from serial import *
from serial.tools import list_ports
import os
ser = Serial('/dev/ttyS0',115200, timeout = 1)
with open("output1.txt","w",encoding="utf-8") as file:
while True:
line = ser.readline().decode("utf-8")
if(line != ""):
#print(line)
file.write(line)
file.flush()
os.fsync(file)
.decode("utf-8")
?