【发布时间】:2020-12-27 05:51:11
【问题描述】:
下面的代码应该将一个数字转换为二进制代码,然后与 arduino-nano 连接,根据哪个二进制代码,4 个灯应该亮起。只有在二进制代码为 1 时,所有灯才会亮起。在控制台中它可以工作,但我的函数没有连接到 arduino-nano。
我的 Arduino-nano 上的代码在手动输入时工作得非常好。
有人可以帮我吗?
import time
import serial
ser = serial.Serial('COM6',115200, timeout=0.5)
number = input('A decimal number please:',)
def decimalToBinary(num): # decimal into a binary code
return bin(num)
#[2:] zur entfernung von 0b
while True:
data = ser.readline() # try to get the connection to arduino nano
index = 0
while index < len(bin): # separate the binary code in individual numbers
binary = bin[index]
print(binary)
def light_up():
if index[0] == 1:
ser.write(b'u') #light up the first light
if index[1] == 1:
ser.write(b'd') # light up the second light
if index[2] == 1:
ser.write(b't') # light up the third light
if index[3] == 1:
ser.write(b'c') # light up the fourth light
light_up()
【问题讨论】:
-
while True: data = ser.readline()我对 Python 知之甚少,但这会停止吗? -
我认为这是一个无穷无尽的。但我已经尝试过不使用 while 循环,但效果不佳。这只是一个尝试。