python實現(xiàn)串口自動觸發(fā)工作的示例
最近在一個python工具中需要實現(xiàn)串口自動觸發(fā)工作的功能,之前只在winform上面實現(xiàn),今天使用python試試。這里簡單記一下:
首先用wxpython實現(xiàn)一個Button,點擊事件綁定函數(shù)OnButtonAutoStopAll
self.button_autoStopAll = wx.Button(id=wxID_FRAME1BUTTONAUTOSTARTALL, label=u'AUTO STOP ALL', name='button_autoStop', parent=self.staticBox_common, pos=wx.Point(8, 284), size=wx.Size(180, 80), style=0) self.button_autoStopAll.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Agency FB')) self.button_autoStopAll.Bind(wx.EVT_BUTTON, self.OnButtonAutoStopAll, id=wxID_FRAME1BUTTONAUTOSTARTALL)
再有ComboBox控件實現(xiàn)點擊下拉時自動加載當前串口名
self.combox = wx.ComboBox(self, -1, pos=wx.Point(10,100), size=wx.Size(100,50), style=wx.CB_READONLY) #串口combox self.combox.Bind(wx.EVT_COMBOBOX_DROPDOWN, self.evt_combox_dropdown)
下拉菜單事件函數(shù)
def evt_combox_dropdown(self, event): print 'combox%d dropdown'%self.sta_num serial_list = list(serial.tools.list_ports.comports()) if serial_list: #判斷是否為空 portName_list = [] #轉換serial handle為port name for i in range(0, len(serial_list)): portname = list(serial_list[i]) portName_list.append(str(portname[0])) print portName_list self.combox.SetItems(portName_list)
然后進入正題,這里根據(jù)DSR信號來觸發(fā)。
#串口自動觸發(fā)檢測線程 class Job(threading.Thread): ... def run(self): while self.__running.isSet(): self.__flag.wait() # 為True時立即返回, 為False時阻塞直到內部的標識位為True后返回 print "into job function" i=0 isOpen = serial_isOpen(i) if serial_list[i]!=1 and isOpen: now_dsr = serial_list[i].getDSR() if now_dsr != last_dsr[i]: last_dsr[i] = now_dsr print 'dsr level changed to %d'%now_dsr if now_dsr == True: if thread_list[i] != 1: if ~thread_list[i].is_alive(): serial_Open(0, False) #do something else: serial_Open(0, False) #do something break time.sleep(1) ...
即每當DSR信號置低時觸發(fā)工作
以上這篇python實現(xiàn)串口自動觸發(fā)工作的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python中根據(jù)時間自動創(chuàng)建文件夾的代碼實現(xiàn)
這篇文章主要介紹了Python中根據(jù)時間自動創(chuàng)建文件夾的代碼實現(xiàn),這樣的話給工作帶來極大的便利,方便桌面文件按時間存放,具體實例代碼跟隨小編一起看看吧2021-10-10Python+MediaPipe實現(xiàn)檢測人臉功能詳解
MediaPipe是用于構建多模態(tài)(例如視頻、音頻或任何時間序列數(shù)據(jù))、跨平臺(即eAndroid、IOS、web、邊緣設備)應用ML管道的框架。本文將利用MediaPipe實現(xiàn)檢測人臉功能,需要的可以參考一下2022-02-02Python連接SQLite數(shù)據(jù)庫操作實戰(zhàn)指南從入門到精通
在Python中使用SQLite進行數(shù)據(jù)庫操作時,我們將深入研究SQLite數(shù)據(jù)庫的創(chuàng)建、表格管理、數(shù)據(jù)插入、查詢、更新和刪除等關鍵主題,幫助你全面了解如何使用SQLite進行數(shù)據(jù)庫操作2023-11-11詳述numpy中的np.random.random()系列函數(shù)用法
本文主要介紹了詳述numpy中的np.random.random()系列函數(shù)用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03使用Python和scikit-learn創(chuàng)建混淆矩陣的示例詳解
這篇文章主要介紹了使用Python和scikit-learn創(chuàng)建混淆矩陣的示例詳解,該示例包括生成數(shù)據(jù)集、為數(shù)據(jù)集選擇合適的機器學習模型、構建、配置和訓練它,最后解釋結果,即混淆矩陣,需要的朋友可以參考下2022-06-06