Arduino计数器

问题描述:

我们在从串口获取数据时遇到了问题。 我们有这个python代码,它的目的是从文件中获取一个值然后递增它。我们试图用电机伺服将它连接到arduino,以便当它检测到一个增量时,伺服就会移动。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) 
+1

压痕线是不正确的。 – joaquin 2013-03-26 20:34:11

在你的代码:代码块的

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)