'How do I fix the UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 35: invalid start byte

I'm trying to write and test a code that sends a three digit value as strings to an Arduino board and turns on the LEDs based on the received value. The code worked for quite a few seconds, until I received this error message:

enter image description here

Here is my python code:

import serial
import time
serialmon = serial.Serial(port='COM3', baudrate=9600, timeout=.1)

i = 0
while True:
if(i % 2 == 0):
    val = "000"
elif (i % 3 == 0):
    val = "100"
elif (i % 5 == 0):
    val = "010"
elif (i % 7 == 0):
    val = "001"
elif (i % 11 == 0):
    val = "011"
elif (i % 13 == 0):
    val = "110"
else:
    val = "111"
serialmon.flush()
valser = str(val).encode().strip()
print ("Python value sent: ")
print (valser)
serialmon.write(valser)
time.sleep(1)
msg = serialmon.read(serialmon.inWaiting()) # read all characters in buffer

print ("Message from arduino: ")
print (msg.decode('utf-8'))
i = i+1

Here is the Arduino Code.

int setPoint = 55;
String readString;

void setup(){
  Serial.begin(9600);  // initialize serial communications at 9600 bps
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop()
{
  // serial read section
  while (Serial.available()){
    delay(30);  //delay to allow buffer to fill 
    if (Serial.available() >0){
      char c = Serial.read();  //reads the char value of 
      readString += c; 
    }
  }

  if (readString.length() >0){
    Serial.print("Arduino received: ");  
    readString = readString.substring(readString.length() - 3, readString.length());
    Serial.println(readString); 
    for (int i = 0; i <3; i++){
      if(readString[i] == '1'){
        digitalWrite(5 + i, HIGH);
      }
      else{
        digitalWrite(5 + i, LOW);
      }
    }
  }
  Serial.flush();
} 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source