我试图通过串口记下温度测量设备的值。代码如下:
import time
import serial
port = serial.Serial("/dev/ttyS0", 115200, timeout=1)
RefTemp = open("RefTemp.dat", "w")
RefTemp.close()
sleep = 1/3.
try:
RefTemp = open("RefTemp.dat", "w")
while True:
port.write('s\n')
time.sleep(sleep)
localtime = time.localtime()
timeString = time.strftime("%Y.%m.%d %H:%M:%S", localtime)
newline = port.readline()
newline = newline[13:21]
RefTemp.write('%s %s \n' % (newline, timeString))
print newline
except KeyboardInterrupt:
pass
RefTemp.close()
问题是,我的输出现在是两个空行,然后测量值导致.dat文件的以下内容:
2016.05.25 13:37:03
2016.05.25 13:37:03
+022.545 2016.05.25 13:37:03
2016.05.25 13:37:04
2016.05.25 13:37:04
+022.544 2016.05.25 13:37:04