慢 Python 串口速度
Slow Python serial speed
我已将 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 可以提高速度,但不幸的是我没有找到任何相关文档。
感谢任何帮助。
来自:
Sleeping for a tenth of a second between readings is an absolute guarantee that you will get less than 10 readings per second. The occasional garbage value you're getting happens when the serial port buffer inevitably overflows and characters get lost.
我已将 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 可以提高速度,但不幸的是我没有找到任何相关文档。
感谢任何帮助。
来自
Sleeping for a tenth of a second between readings is an absolute guarantee that you will get less than 10 readings per second. The occasional garbage value you're getting happens when the serial port buffer inevitably overflows and characters get lost.