UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfd in position 0: invalid start byte

Can someone help me with this on how to fix it? thank you

Python Code in Pycharm:
from cvzone.SerialModule import SerialObject

arduino = SerialObject(“COM5”)

while True:
myData = arduino.getData()
print(myData[0])

Run Result:
Traceback (most recent call last):
File “C:\Users\ajrju\Desktop\Arduino OpenCV Codes\PycharmPythonOpenCV Try\Inputs\Potentiometer.py”, line 6, in
myData = arduino.getData()
File “C:\Users\ajrju\Desktop\Arduino OpenCV Codes\PycharmPythonOpenCV Try.venv\Lib\site-packages\cvzone\SerialModule.py”, line 68, in getData
data = data.decode(“utf-8”)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xfd in position 0: invalid start byte

Hello @jayr_10 and welcome to the forums.

You can make your code more readable whe you post by following this advice: About the Python Help category. Same goes for stack dumps (error messages).

I’m afraid the answer is a bit dull. The author of the library you are using, cvzone assumes that the data coming in through the serial port is text in UTF-8 encoding, but it isn’t. It goes on to split the text up in a particular way.

I can tell from your line numbers that you cannot be using exactly this version.

You don’t say what’s at the other end of the serial link. In order to see what it is actually sending you should read it as binary data, using the pyserial library directly. However, cvzone evidently assumes there is a device and some software at the other end that will send data in a particular format. Is here something you are supposed to run on the Arduino that sends data in the way cvzone expects?

1 Like