打開設(shè)備管理器,雙擊端口設(shè)備,點擊端口設(shè)置,這里面的參數(shù)需要與下面代碼中的參數(shù)保持一致。
版本一
import serial
import time
serialport = serial.Serial()
serialport.port = 'COM3'
serialport.baudrate = 115200
serialport.bytesize = 8
serialport.parity = serial.PARITY_NONE
serialport.stopbits = 1
serialport.timeout = 0.001
serialport.close()
if not serialport.is_open:
serialport.open()
time.sleep(0.05) #時間設(shè)置參考串口傳輸速率
num = serialport.inWaiting()
while num == 0:
time.sleep(0.05) #時間設(shè)置參考串口傳輸速率
num = serialport.inWaiting()
if num > 0:
data = serialport.read(num)
# bytes轉(zhuǎn)str
print(str(data, 'UTF-8'))
注意事項:
1、設(shè)置匹配的波特率;
2、設(shè)置對應的串口號;
版本二
以下是一段Python代碼,可以讀取 Windows 電腦上串口的數(shù)據(jù)并將其保存到一個文本文件中:文章來源:http://www.zghlxwxcb.cn/news/detail-673480.html
import serial
# 串口配置
port = "COM1" # 更改為你要讀取的串口號
baud_rate = 115200 # 波特率,根據(jù)實際情況進行調(diào)整
# 打開串口
ser = serial.Serial(port, baud_rate)
# 打開文件
file_path = "data.txt" # 更改為保存數(shù)據(jù)的文件路徑
file = open(file_path, "w")
# 讀取和保存數(shù)據(jù)
while True:
if ser.in_waiting > 0:
data = ser.readline().decode().strip() # 讀取并解碼數(shù)據(jù)
file.write(data + "\n") # 寫入文件
file.flush() # 刷新文件緩沖區(qū)
print(data) # 可選,打印讀取到的數(shù)據(jù)
# 關(guān)閉文件和串口
file.close()
ser.close()
請注意,上述代碼中的串口號和波特率需要根據(jù)你實際連接的設(shè)備進行修改。同時,代碼中使用了一個無限循環(huán)來實時讀取串口數(shù)據(jù),你可以根據(jù)需要進行適當?shù)男薷?。此外,如果需要在保存?shù)據(jù)時進行其他處理,你可以根據(jù)具體需求進行相應的更改。文章來源地址http://www.zghlxwxcb.cn/news/detail-673480.html
版本三、讀取二進制數(shù)據(jù)流
import serial
# 設(shè)置串口參數(shù)
ser = serial.Serial('COM1', 9600, timeout=1) # 根據(jù)實際情況修改串口名稱和波特率
while True:
# 讀取串口數(shù)據(jù)
data = ser.read()
if data:
# 將字節(jié)數(shù)據(jù)轉(zhuǎn)換為字符串并打印
print(data.decode('utf-8')) # 根據(jù)實際使用的編碼進行解碼
版本4? 實現(xiàn)rtcm32數(shù)據(jù)串口接受保存:
import datetime
import time
import serial
import os
from FileUtil import FileUtil
port = '/dev/ttyUSBnet'
btl = 115200
current_path = os.path.dirname(os.path.abspath(__file__))
current_time = "B" + datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + ".rtcm"
fileutil = FileUtil(path=current_path, filename=current_time)
ser = serial.Serial(port, btl)
print(ser)
time_stamp = time.time()
cnt = 0
while True:
if ser.in_waiting:
data = ser.read()
fileutil.writeByte(data)
cnt = cnt + 1
if cnt % 100 == 0:
print(end=f'\r{"time :" +str(time.time()-time_stamp)}')
import os.path
class FileUtil:
@staticmethod
def strSplit(datas, sep):
"""
:param datas: 一維數(shù)據(jù)
:param sep: 分隔符
:return: 二維數(shù)組
"""
res = []
for data in datas:
res.append(str(data).split(sep))
return res
def __init__(self, path: str, filename: str):
if not path.endswith("/"):
path = path.__add__("/")
print("寫入文件目錄" + path + filename)
# 目錄不存在則創(chuàng)建目錄
if not os.path.exists(path):
os.makedirs(path)
self.filename = path + filename
def write(self, content):
if not os.path.exists(self.filename):
with open(self.filename, "w") as f:
f.write(content)
else:
with open(self.filename, "a") as f:
f.write(content)
def writeByte(self, content):
with open(self.filename, "ab") as f:
f.write(content)
到了這里,關(guān)于Python — 獲取電腦串口數(shù)據(jù)并保存到txt文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!