3

I am trying toread potential meter data from Arduino using python, with the program on Arduino as follows :

#include <cvzone.h>SerialData serialData;int sendVals[2];void setup() {  serialData.begin(9600);}void loop() {  int potVal = analogRead(A0);  sendVals[0]= potVal;  serialData.Send(sendVals);}

the program on the arduino side is running fine

and program in python as follows

from cvzone.SerialModule import SerialObjectarduino = SerialObject("COM7")while True:   data = arduino.getData()   print(data[0])

but I get this error:

Traceback (most recent call last):    data = arduino.getData()  File "C:...\site-packages\cvzone\SerialModule.py", line 68, in getData    data = data.decode("utf-8")UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf7 in position 0: invalid start byte

How to solve it?

Greenonline's user avatar
Greenonline
3,1527 gold badges37 silver badges49 bronze badges
askedDec 6, 2021 at 10:38
M. Al lail Qadrillah's user avatar
2
  • the new USB connection resets the Mega and it spends some time in bootloader waiting for upload.CommentedDec 6, 2021 at 13:32
  • Maybe add some delay on the Arduino after initializing the serialData instartup() before it starts sending data in theloop().CommentedDec 6, 2021 at 14:45

2 Answers2

2

I would embed the call to.getData() into atry block and handle this specific exception withexcept UnicodeDecodeError. There can always be a transmission error, especially when the script is started.

However, I would do it differently:

void loop() {  Serial.println(analogRead(A0));}

...and on the python side useserial.readline() from pyserial instead.But I don't know cvzone and you might have a reason to use this instead of the commonly used pyserial.

answeredDec 6, 2021 at 11:31
Sim Son's user avatar
0
0

There seems that theremay be an issue with the implementation ofgetData() inSerialModule.py.

Fromline 62

    def getData(self):        """        :param numOfVals: number of vals to retrieve        :return: list of data received        """        data = self.ser.readline()        data = data.decode("utf-8")        data = data.split('#')        dataList = []        [dataList.append(d) for d in data]        return dataList[:-1]

The block of code containing thereadLine() should probably be with in atry...except block, like so:

    def getData(self):        """        :param numOfVals: number of vals to retrieve        :return: list of data received        """        try:            data = self.ser.readline()            data = data.decode("utf-8")            data = data.split('#')            dataList = []            [dataList.append(d) for d in data]            return dataList[:-1]        except Exception as e:            print(e)            self.ser.close

An issue has been raised on Github:SerialModule.py - readLine() should be within try block. #49

answeredSep 9, 2022 at 7:47
Greenonline's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.