Skip to main content
deleted 178 characters in body
Source Link
stanley
  • 107
  • 3

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");

It appears to be the number '20' in this line that is causing this error, because when I delete everything from '20%...' onward leaving only the first half of the line, it works.

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.

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");

It appears to be the number '20' in this line that is causing this error, because when I delete everything from '20%...' onward leaving only the first half of the line, it works.

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.

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.

Source Link
stanley
  • 107
  • 3

How to fix UnicodeDecodeError when using Serial.println

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");

It appears to be the number '20' in this line that is causing this error, because when I delete everything from '20%...' onward leaving only the first half of the line, it works.

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.