python3 pySerial TypeError: unicode strings are not supported, please encode to bytes:

匿名 (未验证) 提交于 2019-12-03 03:00:02

问题:

In python 3 i imported the pySerial library so i could comunicate 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 strings are not supported, please encode to bytes: 'allon' in python 2.7 the only thing i did diferently is use raw_input but i dont 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() 

回答1:

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

ser.write(serialcmd.encode())



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!