python3编码错误,编码错误,在python3中

I have a somewhat similar question here that I can't solve. In another instance of my code, I face similar encoding errors. Do assist!

My code:

port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10, bytesize=8)

f_w = open('/home/ryan/python_serial_output.txt','r+')

f_o = open('/home/ryan/python_serial_parse.txt','w')

port.send_break()

sys_reply = port.read(100000)

sys_reply_str = sys_reply.decode('utf-8')

print(sys_reply_str)

sys_reply_str_haha = sys_reply_str.replace("\r","")

sys_reply_str_haha = sys_reply_str_haha.replace("\n","")

i = list(sys_reply_str_haha)

if str(i[-1]) == '>':

ip = 'CR1'

ip_en = ip.encode('utf-8')

port.write(ip_en)

read_syscheck = port.read(100000)

read_syscheck_str = read_syscheck.decode('utf-8')

print(read_syscheck_str)

Yes, it's inefficient, but I'm writing it step by step to avoid errors as a start.

With this code, this is the result I get.

myname@Toshiba:~$ python3 serial_test.py

Explorer (c) 2009

All rights reserved.

Firmware Version: 34.11

>

Traceback (most recent call last):

File "serial_test.py", line 25, in

read_syscheck_str = read_syscheck.decode('utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 7: invalid continuation byte

It seems I am encoding 'CR1' wrongly, that's why the error has been prompted. CR1 is supposed to reset my sensor to factory default settings, and meant to reply a confirmation of that.

Thank you very much in advance for any assistance.

解决方案

Wrong encoding method.

Correct one was 'cp437'

Unsure why UTF-8 worked after the break, but not after writing a command

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值