pySerial 解码函数返回错误 ‘utf-8 编解码器无法解码字节 0xff …’

huangapple go评论112阅读模式
英文:

pySerial decode function returns error 'utf-8 codec can't decode byte 0xff ...'

问题

I set up an Arduino to give this kind of input:

  1. 3.30x530.00x2.59x1325.0x28.70x1013.14x0.91x23.41
  2. 3.50x531.00x2.60x1327.5x28.70x1013.11x1.17x23.41
  3. 3.60x531.00x2.60x1327.5x28.70x1013.14x0.89x23.41

I want the numbers to be sent to Python to access and process them. It works for one or two lines until this error appears: 'utf-8' codec can't decode byte 0xff in position 1: invalid start byte

The problem is related to this line:

  1. arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")

This is the complete function:

  1. import serial
  2. import time
  3. import schedule
  4. import pandas as pd
  5. def main_func():
  6. arduino = serial.Serial('COM3', 9600)
  7. print('Established serial connection to Arduino')
  8. arduino_data = arduino.readline()
  9. arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")
  10. decoded_values = str(arduino_data)
  11. list_values = decoded_values.split('x')
  12. for item in list_values:
  13. list_in_floats.append(float(item))
  14. print(f'Collected readings from Arduino: {list_in_floats}')
  15. arduino_data = 0
  16. list_in_floats.clear()
  17. list_values.clear()
  18. arduino.close()
  19. print('Connection closed')

I tried changing the Arduino code to give a different data type. Neither float nor int worked, so I started looking into the documentation and the Python script, but I found nothing so far. What is strange is that it works a couple of times and then suddenly doesn't.

英文:

I set up an Arduino to give this kind of input:

  1. 3.30x530.00x2.59x1325.0x28.70x1013.14x0.91x23.41
  2. 3.50x531.00x2.60x1327.5x28.70x1013.11x1.17x23.41
  3. 3.60x531.00x2.60x1327.5x28.70x1013.14x0.89x23.41

I want the numbers to be send to Python to acess and process them. It works for one or two lines until this error appears
'utf-8' codec can't decode byte 0xff in position 1: invalid start byte

The problem is related to this line

  1. arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")

This is the complete function:

  1. import time
  2. import schedule
  3. import pandas as pd
  4. def main_func():
  5. arduino = serial.Serial('COM3', 9600)
  6. print('Established serial connection to Arduino')
  7. arduino_data = arduino.readline()
  8. arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")
  9. decoded_values = str(arduino_data)
  10. list_values = decoded_values.split('x')
  11. for item in list_values:
  12. list_in_floats.append(float(item))
  13. print(f'Collected readings from Arduino: {list_in_floats}')
  14. arduino_data = 0
  15. list_in_floats.clear()
  16. list_values.clear()
  17. arduino.close()
  18. print('Connection closed')

I tried changing the arduino code to give a different data type. neither float nor int worked so I started looking into the documentation and the pyhton script but i found nothing so far. what is strange is that it works a couple of times and den suddendly doesnt.

答案1

得分: 0

  1. 所以我尝试了不同的编码utf-8cp1252iso-8859-1latin1utf-16),结果发现这并没有解决我的问题但使用了'latin1'我发现我的某个传感器有时会提供奇怪的读数我添加了一个try循环来过滤它们现在即使发生错误我的代码也能正常运行
  2. def main_func():
  3. arduino_data = arduino.readline()
  4. dec = 'latin1' # Options: utf-8, cp1252, iso-8859-1, latin1, utf-16
  5. try:
  6. arduino_data2 = arduino_data[0:len(arduino_data)].decode(dec)
  7. decoded_values = str(arduino_data2)
  8. list_values = decoded_values.split('x') # 数据在 x 处分隔
  9. timestamp = str(time.time())
  10. for item in list_values:
  11. list_in_floats.append(float(item)) # 将分隔后的数据放入列表
  12. list_in_floats.append(timestamp)
  13. df.loc[len(df.index)] = list_in_floats # 将数据存储在 DataFrame 中
  14. print(f'{list_in_floats}')
  15. # print(list_in_floats[0]) # 打印 piezo_spannung
  16. except:
  17. print("发生了异常", arduino_data[0:len(arduino_data)].decode(dec))
  18. arduino_data = 0
  19. list_in_floats.clear()
  20. list_values.clear()
英文:

So I tried different encodings (utf-8, cp1252, iso-8859-1, latin1, utf-16) and it turned out that this did not solve my problem but using 'latin1' helped me find out that one of my sensors gave weird redings sometimes. I added a try-loop to filter them and now my code is running even if some errors occur

  1. def main_func():
  2. arduino_data = arduino.readline()
  3. dec = 'latin1' # Möglichkeiten: utf-8, cp1252, iso-8859-1, latin1, utf-16
  4. try:
  5. arduino_data2 = arduino_data[0:len(arduino_data)].decode(dec)
  6. decoded_values = str(arduino_data2)
  7. list_values = decoded_values.split('x') # am x werden die Daten getrennt
  8. timestamp = str(time.time())
  9. for item in list_values:
  10. list_in_floats.append(float(item)) # die augeteilten Daten in eine Liste setzen
  11. list_in_floats.append(timestamp)
  12. df.loc[len(df.index)] = list_in_floats # Daten in DataFrame speichern
  13. print(f'{list_in_floats}')
  14. # print(list_in_floats[0]) # druckt piezo_spannung
  15. except:
  16. print("An exception occurred", arduino_data[0:len(arduino_data)].decode(dec))
  17. arduino_data = 0
  18. list_in_floats.clear()
  19. list_values.clear()

huangapple
  • 本文由 发表于 2023年6月13日 04:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定