您的位置:首页 > 其它

TypeError: unicode strings are not supported, please encode to bytes: 'hu'

2017-05-10 11:26 2659 查看
1、错误描述
>>> t=serial.Serial("COM3",4800);
>>> n=t.write('00000200=0000020');
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
n=t.write('00000200=0000020');
File "D:\Python\Python36\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialwin32.py", line 308, in write
data = to_bytes(data)
File "D:\Python\Python36\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: '00000200=0000020'
>>> n=t.write('hu');
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
n=t.write('hu');
File "D:\Python\Python36\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialwin32.py", line 308, in write
data = to_bytes(data)
File "D:\Python\Python36\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: 'hu'
>>>
2、错误原因
     需要将传输的字符串添加encode()方法,需要对字符串进行编码

3、解决办法
>>> n=t.write('00000200=00000200'.encode());
>>> print(t.portstr);
COM3
>>> print(n);
17
>>> s=t.read(n);
print(s);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐