Python?Serial串口的簡單數(shù)據(jù)收發(fā)方式
更新時間:2023年09月08日 08:55:50 作者:木南成長之路
這篇文章主要介紹了Python?Serial串口的簡單數(shù)據(jù)收發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
導入模塊
注意這里模塊名是pyserial
pip install pyserial
1. 打開串口
import serial
com = serial.Serial('COM3', 9600)
print com2. 發(fā)送數(shù)據(jù)
import serial
com = serial.Serial('COM3', 9600)
success_bytes = com.write('This is data for test')
print success_bytes3. 接收數(shù)據(jù)(固定長度)
import serial
com = serial.Serial('COM3', 9600)
data = com.read(10)
print data接收10個字節(jié),不接收到就一直等待接收。
4. 接收數(shù)據(jù)(某時間內(nèi)一直接收,超過則不接收)
import serial
import time
com = serial.Serial('COM3', 115200)
over_time = 30 # 設置的時間
start_time = time.time()
while True:
end_time = time.time()
if end_time - start_time < over_time:
data = com.read(com.inWaiting())
data = str(data)
if data != '':
print data5. 封裝成類
# -*- encoding=utf-8 -*-
import serial
import time
import WriteLog
class COM:
def __init__(self, port, baud):
self.port = port
self.baud = int(baud)
self.open_com = None
self.log = WriteLog.WriteLog('ITC_LOG.LOG')
self.get_data_flag = True
self.real_time_data = ''
# return real time data form com
def get_real_time_data(self):
return self.real_time_data
def clear_real_time_data(self):
self.real_time_data = ''
# set flag to receive data or not
def set_get_data_flag(self, get_data_flag):
self.get_data_flag = get_data_flag
def open(self):
try:
self.open_com = serial.Serial(self.port, self.baud)
except Exception as e:
self.log.error('Open com fail:{}/{}'.format(self.port, self.baud))
self.log.error('Exception:{}'.format(e))
def close(self):
if self.open_com is not None and self.open_com.isOpen:
self.open_com.close()
def send_data(self, data):
if self.open_com is None:
self.open()
success_bytes = self.open_com.write(data.encode('UTF-8'))
return success_bytes
def get_data(self, over_time=30):
all_data = ''
if self.open_com is None:
self.open()
start_time = time.time()
while True:
end_time = time.time()
if end_time - start_time < over_time and self.get_data_flag:
data = self.open_com.read(self.open_com.inWaiting())
# data = self.open_com.read() # read 1 size
data = str(data)
if data != '':
self.log.info('Get data is:{}'.format(data))
all_data = all_data + data
print data
self.real_time_data = all_data
else:
self.set_get_data_flag(True)
break
return all_data
if __name__ == '__main__':
pass
com = COM('com3', 9600)
com.open()
print com.send_data('data')
com.get_data(50)
com.close()總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python中串口通信庫pyserial基礎知識
- Python讀取串口數(shù)據(jù)的實現(xiàn)方法
- python讀取串口數(shù)據(jù)有幾種方法
- python serial串口通信示例詳解
- 玩轉串口通信:利用pyserial庫,Python打開無限可能
- Python通過串口實現(xiàn)收發(fā)文件
- Python串口通信的接收與發(fā)送的實現(xiàn)
- python實現(xiàn)串口通信的示例代碼
- 使用Python玩轉串口(基于pySerial問題)
- python串口讀取數(shù)據(jù)的實例
- 使用 Python 列出串口的實現(xiàn)方法
- Python中串口操作的實現(xiàn)示例
相關文章
python簡單實現(xiàn)整數(shù)反轉的畫解算法
這篇文章主要介紹了python簡單實現(xiàn)整數(shù)反轉采用了一個有趣的畫解算法,通過示例的題目描述來對問題分析進行方案的解決,有需要的朋友可以參考下2021-08-08
Windows安裝Anaconda并且配置國內(nèi)鏡像的詳細教程
我們在學習 Python 的時候需要不同的 Python 版本,關系到電腦環(huán)境變量配置換來換去很是麻煩,所以這個時候我們需要一個虛擬的 Python 環(huán)境變量,這篇文章主要介紹了Windows安裝Anaconda并且配置國內(nèi)鏡像教程,需要的朋友可以參考下2023-01-01
Python數(shù)據(jù)庫安裝及MySQL?Connector應用教程
這篇文章主要為大家介紹了Python數(shù)據(jù)庫安裝及MySQL Connector應用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
關于Python 實現(xiàn)tuple和list的轉換問題
這篇文章主要介紹了Python 實現(xiàn)tuple和list的轉換,文中介紹了list(列表)和tuple(元組)共同點和區(qū)別,結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-05-05

