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

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

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

问题

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

3.30x530.00x2.59x1325.0x28.70x1013.14x0.91x23.41
3.50x531.00x2.60x1327.5x28.70x1013.11x1.17x23.41
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:

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

This is the complete function:

import serial
import time
import schedule
import pandas as pd

def main_func():
    arduino = serial.Serial('COM3', 9600)
    print('Established serial connection to Arduino')
    arduino_data = arduino.readline()
    arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")

    decoded_values = str(arduino_data)
    list_values = decoded_values.split('x')

    for item in list_values:
        list_in_floats.append(float(item))

    print(f'Collected readings from Arduino: {list_in_floats}')

    arduino_data = 0
    list_in_floats.clear()
    list_values.clear()
    arduino.close()
    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:

3.30x530.00x2.59x1325.0x28.70x1013.14x0.91x23.41
3.50x531.00x2.60x1327.5x28.70x1013.11x1.17x23.41
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

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

This is the complete function:

import time
import schedule
import pandas as pd


def main_func():
    arduino = serial.Serial('COM3', 9600)
    print('Established serial connection to Arduino')
    arduino_data = arduino.readline()
    arduino_data = arduino_data[0:len(arduino_data)].decode("utf-8")

    decoded_values = str(arduino_data)
    list_values = decoded_values.split('x')

    for item in list_values:
        list_in_floats.append(float(item))

    print(f'Collected readings from Arduino: {list_in_floats}')


    arduino_data = 0
    list_in_floats.clear()
    list_values.clear()
    arduino.close()
    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

所以我尝试了不同的编码utf-8cp1252iso-8859-1latin1utf-16),结果发现这并没有解决我的问题但使用了'latin1'我发现我的某个传感器有时会提供奇怪的读数我添加了一个try循环来过滤它们现在即使发生错误我的代码也能正常运行

def main_func():
    arduino_data = arduino.readline()
    dec  = 'latin1' # Options: utf-8, cp1252, iso-8859-1, latin1, utf-16

try:
    arduino_data2 = arduino_data[0:len(arduino_data)].decode(dec)
    decoded_values = str(arduino_data2)
    list_values = decoded_values.split('x') # 数据在 x 处分隔
    timestamp = str(time.time())

    for item in list_values:
        list_in_floats.append(float(item)) # 将分隔后的数据放入列表
    list_in_floats.append(timestamp)
    df.loc[len(df.index)] = list_in_floats # 将数据存储在 DataFrame 中

    print(f'{list_in_floats}')
    # print(list_in_floats[0]) # 打印 piezo_spannung

except:
   print("发生了异常", arduino_data[0:len(arduino_data)].decode(dec))


arduino_data = 0
list_in_floats.clear()
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

def main_func():
    arduino_data = arduino.readline()
    dec  = 'latin1' # Möglichkeiten: utf-8, cp1252, iso-8859-1, latin1, utf-16

try:
    arduino_data2 = arduino_data[0:len(arduino_data)].decode(dec)
    decoded_values = str(arduino_data2)
    list_values = decoded_values.split('x') # am x werden die Daten getrennt
    timestamp = str(time.time())

    for item in list_values:
        list_in_floats.append(float(item)) # die augeteilten Daten in eine Liste setzen
    list_in_floats.append(timestamp)
    df.loc[len(df.index)] = list_in_floats # Daten in DataFrame speichern

    print(f'{list_in_floats}')
    # print(list_in_floats[0]) # druckt piezo_spannung

except:
   print("An exception occurred", arduino_data[0:len(arduino_data)].decode(dec))


arduino_data = 0
list_in_floats.clear()
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:

确定