Typeerror:unicode strings are not supported, please encode to bytes: ' Hu '

Source: Internet
Author: User


1. Error description






>>> 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. Error reason





You need to add the transmitted string to the encode () method, and you need to encode the string






3. Solutions






>>> n=t.write (' 00000200=00000200 '. Encode ());
>>> print (T.PORTSTR);
COM3
>>> print (n);
>>> s=t.read (n);
print (s);




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.