0

I am using an Arduino Uno R3 to send instructions to a serial monitor in a Python program with Serial.println. But when I send a specific line, I get a a UnicodeDecodeError.

The line in the Arduino code that causes this error is:

Serial.println("Press '1,2,3,4,5' to run motor at 10%, 20%, 30%, 40%, 50% for 2 seconds");

My Python code to read the serial data is:

def get_serial_data(self):
        while 1:
            try:
                serial_data = self.serial_connection.readline().decode('Ascii')
                filtered_serial_data = serial_data.strip('\r\n')
                self.serial_monitor.insert(END, filtered_serial_data)
            except TypeError:
                pass

and is called from a thread:

thread1 = threading.Thread(target=self.get_serial_data)
        thread1.daemon = True
        thread1.start()

But I get the following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x85 in position 36: ordinal not in range(128)

Can anyone help why this is happening or how to fix, I saw a stackoverflow post to use a try, except block to get it to ignore this line, but I want to print it out, and I want to understand why I get this error.

1
  • 1
    As a newcomer to the Arduino community, it would be good to know why this and my answer got downvoted. It was a genuine issue that took me a while to solve, and wasn't obvious from the docs. Without any reasoning for the downvotes makes this community come across as unfriendly
    – stanley
    Commented Aug 2, 2023 at 2:01

1 Answer 1

-1

I solved the issue. It was a memory issue not a decoding issue. Because the dynamic memory was mostly used up, it affected the encoding of the data. Reducing the amount of memory my code was using resolved the issue.

I also discovered, that Serial.println("some text") uses a lot of dynamic memory. So by reducing the amount of text I was encoding helped, but also by wrapping the the text in F(), like this:

Serial.println(F("some text"))

Which stores the string in flash memory not sram. Heres a helpful link on the F function:

https://arduinogetstarted.com/faq/what-is-serial.println-f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.