使用读取文件并写入串行端口的代码实时写入文件-Python

问题描述

当前有一个接受.txt文件并将命令提交给序列号的代码。然后,它从串行端口读取答复并将其写入硬编码的.txt文件。问题是它无法实时保存,因此如果出于任何原因需要停止代码,则无法收集当前数据,并且文本文件为空白。我对缓冲和诸如此类的东西不太熟悉,并尝试了“ outputFile = open(“ ./ outputFile.txt”,“ a”,0)“,但是这给了我错误”不能具有未缓冲的文本I / O ”在python 3中?”所以我不确定该怎么办。如果您想弄乱它,这是常规布局:

with open(test_file) as file_test:
    Lines = file_test.readlines()
    for line in Lines:
        #send_str is the command to send to the serial port
        send_str = line
        file_result.write(line + "\n")   #<--- if I were to cancel out after this it wouldn't be saved(*)                   
        ser.write(send_str.encode('utf-8'))
        time.sleep(send_pause)
        reply_str = ser.readline().decode('utf-8').strip()
        file_result.write("reply:" + reply_str + "\n")   #<---(*)
        file_result.write('\n')   #<---(*)

任何东西都可以帮助您

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)