python3 pySerial TypeError:不支持unicode字符串,请编码为字节: [英] python3 pySerial TypeError: unicode strings are not supported, please encode to bytes:

查看:2238
本文介绍了python3 pySerial TypeError:不支持unicode字符串,请编码为字节:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中,我导入了pySerial库,以便可以通过串行命令与Arduino Uno通信.
它在Python 2.7中效果很好,但是在Python 3中,我一直遇到一个错误,它说这是

In Python 3 I imported the pySerial library so I could communicate with my Arduino Uno by serial commands.
It worked very well in Python 2.7 but in Python 3 I keep running into a error it says this

TypeError:不支持unicode字符串,请编码为字节:'allon'

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

在Python 2.7中,我唯一做的不同是使用raw_input,但我不知道Python 3中正在发生什么.这是我的代码

In Python 2.7 the only thing I did differently is use raw_input but I don't know what is happening in Python 3. Here is my code

import serial, time
import tkinter
import os

def serialcmdw():
    os.system('clear')
    serialcmd = input("serial command: ")
    ser.write (serialcmd)

serialcmdw()

ser = serial.Serial()
os.system('clear')
ser.port = "/dev/cu.usbmodem4321"
ser.baudrate = 9600
ser.open()
time.sleep(1)
serialcmdw()

推荐答案

将要写入串行的数据编码,在这种情况下,将"serialcmd"编码为字节.请尝试以下操作:

Encode your data which you are writing to serial,in your case "serialcmd" to bytes.try the following :

ser.write(serialcmd.encode())

ser.write(serialcmd.encode())

这篇关于python3 pySerial TypeError:不支持unicode字符串,请编码为字节:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆