我已将Arduino UNO连接到我的raspberry pi,并希望使用Python脚本读取连接的传感器。
当我尝试从Arduino IDE读取传感器数据时,它工作得非常快,但使用Python时速度非常慢。
这是我的代码:
import serial
from subprocess import call
from time import sleep
ser = serial.Serial('/dev/ttyACM0')
ser.baudrate = 9600
a = 0
stop = False
file = open("PulseData/MasterArrayData.txt","w")
if(ser.isOpen() == False):
ser.open()
print("Start scanning")
while stop == False:
test = ser.readline()
try:
testInt = int(test)
if testInt > 100 and testInt < 800:
print test
file.write(str(testInt))
file.write("\n")
a = a+1
except ValueError:
print "Not an integer"
if(a == 400):
stop = True
sleep(0.1)
file.close()
call(["./main", "PulseData/MasterArrayData.txt"])
我已经尝试使用更高的波特率,或更短的睡眠时间,但没有成功。
我读到PyTTY可以提高速度,但遗憾的是,我没有找到任何关于这方面的文档。
我感谢你的帮助。