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

查看:68
本文介绍了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

类型错误:不支持 unicode 字符串,请编码为字节:'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())

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

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