【发布时间】:2019-02-24 16:10:42
【问题描述】:
我有 ArduinoUNO,我使用 Python3。我想根据命令打开和关闭单个 LED。但我得到了错误。
开启 TypeError:不支持 unicode 字符串,请编码为字节:'H' 离开 TypeError: 不支持 unicode 字符串,请编码为字节:'L'
我做错了什么? 这是我的代码:
import serial
import time
arduino = serial.Serial("COM3", 9600)
def onOffFunction():
command = input("Type in something (on/off/bye): ");
if command == "on":
print ("The LED is ON")
time.sleep(1)
arduino.write('H')
onOffFunction()
elif command == "off":
print ("The LED is OFF")
time.sleep(1)
arduino.write('L')
onOffFunction()
elif command == "bye":
print ("Bye Bye!")
time.sleep(1)
arduino.close()
else:
print ("Sorry.. Try typing something else.")
onOffFunction()
time.sleep(2)
onOffFunction()
【问题讨论】: