zoukankan      html  css  js  c++  java
  • TypeError: unicode strings are not supported, please encode to bytes: 'hu'

    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:PythonPython36libsite-packagespyserial-3.3-py3.6.eggserialserialwin32.py", line 308, in write
        data = to_bytes(data)
      File "D:PythonPython36libsite-packagespyserial-3.3-py3.6.eggserialserialutil.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:PythonPython36libsite-packagespyserial-3.3-py3.6.eggserialserialwin32.py", line 308, in write
        data = to_bytes(data)
      File "D:PythonPython36libsite-packagespyserial-3.3-py3.6.eggserialserialutil.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);

  • 相关阅读:
    了解xss
    kail Windows10 远程连接解决办法
    信息收集二级域名或ip收集
    mpvue引用Echarts不渲染问题
    IIS状态码
    apicloud的tapmode用法
    JavaScript交换两个变量值的七种解决方案
    IIS express 服务的开启, 支持多个iis express
    asp.net解决跨域访问问题
    Flex 布局
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313874.html
Copyright © 2011-2022 走看看