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);

  • 相关阅读:
    golang获取URL
    Golang读取HTML中Table数据到二维数组
    Golang的GUI开发包fyne基本教程
    C#搭建安川机器人上位机
    程序计数器
    SpringBoot定时任务详解
    mysql 5.7安装
    springboot 配置多数据源
    mysql 查询某一天数据
    java如何获取当前日期和时间
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313874.html
Copyright © 2011-2022 走看看