我们在从串口获取数据时遇到问题。 我们有这个python代码,其目的是从文件中获取值然后递增它。我们试图用电机伺服将它连接到arduino,这样当它检测到增量时,伺服就会移动。
#
# fetch counter
#
import time
import urllib
import serial
# usb serial connection to arduino
ser = serial.Serial('COM3', 9600)
last_counter = open("hitcounter.txt")
for b in last_counter:
b = b.strip()
while (True):
counter = open("hitcounter.txt")
for a in counter:
a = a.strip()
delta = int(a) - int(b)
ass = delta+1
print ass
print "counter: %s, delta: %s" % (a, delta)
x=chr(ser.write(chr(ord(chr(delta)))))
print type(x)
print "%s" %(x)
y=ser.write(ass)
print type(y)
print "%s" %(y)
b = a
time.sleep(10)
答案 0 :(得分:2)
在您的代码中:
while (True):
它之后的行没有缩进,因此不是代码块的一部分。你的代码在这里只是空闲循环。 :(
假设我理解你的代码逻辑:
#
# fetch counter
#
import time
import urllib
import serial
# usb serial connection to arduino
ser = serial.Serial('COM3', 9600)
last_counter = open("hitcounter.txt")
for b in last_counter:
b = b.strip()
while (True):
counter = open("hitcounter.txt")
for a in counter:
a = a.strip()
delta = int(a) - int(b)
ass = delta+1
print ass
print "counter: %s, delta: %s" % (a, delta)
x=chr(ser.write(chr(ord(chr(delta)))))
print type(x)
print "%s" %(x)
y=ser.write(ass)
print type(y)
print "%s" %(y)
b = a
time.sleep(10)