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);
>>> 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);
相关文章推荐
- WSDLToJava Error: Rpc/encoded wsdls are not supported with CXF
- #Redis Error #MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details a
- TypeError: decoding Unicode is not supported
- WSDLToJava Error: Rpc/encoded wsdls are not supported with CXF
- TypeError: decoding Unicode is not supported
- TypeError: decoding Unicode is not supported
- MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
- MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- Flex中TypeError: Error #1089: Assignment to lists with more than one item is not supported.
- WSDLToJava Error: Rpc/encoded wsdls are not supported with CXF
- IPMI error: Authentication type NONE not supported
- Error: The INF file contains Unicode characters that could not be converted correctly to ANSI的彻底解决
- MVC project on VS2010 error : The project type is not supported by this installation
- Compiler Error Message: CS0234: The type or namespace name 'BorderDetails' does not exist in the class or namespace 'Infragistics.WebUI.Shared' (are you missing an assembly reference?)
- ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings
- "Only TrueType fonts are supported. This is not a TrueType font."问题解决
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
- SQL Server 2005 Profiler 创建 trace 失败:Only TrueType Fonts are supported. This is not a TrueType font
- Only TrueType fonts are supported. This is not a TrueType font