筆記:python讀取串口數據並保到本地txt文件

效果

在這裏插入圖片描述
在這裏插入圖片描述

代碼

import time
import serial

ser = serial.Serial(
port=‘COM3’,
baudrate=9600,
parity=serial.PARITY_ODD, # 校驗位
stopbits=serial.STOPBITS_TWO, # 停止位
bytesize=serial.SEVENBITS # 數據位
)
data = ‘’

while True:
data = ser.readline()
t = time.time()
ct = time.ctime(t)
print(ct, ‘:’)
print(data)

f = open('D:/test.txt', 'a')
f.writelines(ct)
f.writelines(':\n')
f.writelines(data.decode('utf-8'))
f.close()

參考鏈接:https://blog.csdn.net/u012611644/article/details/79125234?utm_source=blogxgwz0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章