【发布时间】:2022-08-18 02:28:54
【问题描述】:
我正在使用 RedBoard QWIIC 读取传感器数据。程序输出如下数据:
408 10 45.47 98760.30 23.33 413 19.17
400 7 45.45 98758.38 23.33 414 19.17
415 16 45.45 98757.56 23.33 414 19.17
405 3 45.45 98758.38 23.33 414 19.17
然而,当我运行我的 Python 程序时,.txt 文件看起来像这个:
07/21/2022 14:12:49 400 0 45.42 98763.58 23.34 406
19.17
07/21/2022 14:12:52 400 0 45.45 98759.20 23.34 406
19.18
07/21/2022 14:12:55 400 0 45.48 98764.69 23.34 405
19.18
有问题的 Python 程序:
import serial
import time
serialPort_1 = \'COM3\'
baud_rate = 9600
write_to_file_path = \"test 1 7-21-22.txt\"
output_file = open(write_to_file_path, \"w+\")
ser1 = serial.Serial(serialPort_1, baud_rate, timeout=4)
while 1:
line1 = ser1.readline()
line1 = line1.decode(\"utf-8\")
print(time.strftime(\"%m/%d/%Y %H:%M:%S\") + \' \' + line1)
output_file.write(time.strftime(\"%m/%d/%Y %H:%M:%S\")+\' \'+line1)
time.sleep(0.00001)
如何让程序停止在这最后两个值之间缩进?我已经尝试从在传感器输出后打印 \"\\t\" 字符改为打印几个空格。
-
这里没有什么可以做到这一点。你怎么看文件?你有没有做过一个 hexdump 来看看它到底是什么样子的?我的猜测是文件很好。如果你在一个原始系统上,也许你的终端线长度设置为 80,所以 tty 包有助于打破这条线。顺便说一句,你的 time.sleep 很傻。最小睡眠值约为 0.015,具体取决于操作系统。调度程序不会比这更频繁地运行。由于
readline阻塞(有超时),所以根本不需要睡觉。 -
我正在直接打开 .txt 文件。我不知道如何进行 hexdump。我确实尝试查看每行的字符长度,最大长度为 51 个字符。
-
\"直接打开 .txt 文件\"——在什么地方?使用
cat还是使用编辑器?如果您使用的是 Linux,请执行hexdump -C xxx.txt。 -
我在 Windows 上,我只是单击 .txt 文件,它会在记事本中打开。
标签: python deserialization pyserial