英文:
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-8、cp1252、iso-8859-1、latin1、utf-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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论