我目前正在构建一台使用Arduino Mega2560作为其主控制器的机器. Arduino连接到串行,获取命令,执行它并每隔1ms吐出一堆测量数据.我有一个运行
Python的Raspberry Pi,为用户提供了一个很好的GUI来发送命令,并以可读的形式呈现数据.
我面临的问题是:Arduino能够每毫秒吐出15个字节的数据(因此只有15kbyte / s),但我正在运行的代码每10毫秒只能处理大约15个字节,所以1.5kB / s .
当我运行cat / dev / ttyACM0> somefile,我很高兴看到所有数据点.
我有以下精简的Python代码
# Reset Arduino by starting serial
microprocBusy = True
serialPort = serial.Serial("/dev/ttyACM0", baudrate=460800, timeout=0)
time.sleep(0.22);
serialPort.setDTR(False);
time.sleep(0.22);
serialPort.setDTR(True);
time.sleep(0.10);
logfile = open(logfilenamePrefix + "_" + datetime.datetime.now().isoformat() + '.txt', 'a')
# Bootloader has some timeout, we need to wait for that
serialPort.flushInput()
while(serialPort.inWaiting() == 0):
time.sleep(0.05)
# Wait for welcome message
time.sleep(0.1)
logfile.write(serialPort.readline().decode('ascii'))
logfile.flush()
# Send command
serialPort.write((command + '\n').encode('ascii'))
# Now, receive data
while(True):
incomingData = serialPort.readline().decode('ascii')
logfile.write(incomingData)
logfile.flush()
if(incomingData[:5] == "FATAL" or incomingData[:6] == "HALTED" or incomingData[:5] == "RESET"):
break;
elif(incomingData[:6] == "RESULT"):
resultData = incomingData;
logfile.flush()
当我运行它时,第一个~350个数据点进入,然后我看到一些受损数据并错过了大约2000个数据点,之后我看到另外350个数据点.在此过程中CPU使用率为100%
出了什么问题? PySerial的优化程度是否很差,或者我错过了我的代码中是否存在错误?我可以运行cat / dev / ttyACM0>来自Python的somefile然后读取该文件,但这不是一个很好的解决方案,是吗?
非常感谢 :)