python实现串口自动触发工作的示例
最近在一个python工具中需要实现串口自动触发工作的功能,之前只在winform上面实现,今天使用python试试。这里简单记一下:
首先用wxpython实现一个Button,点击事件绑定函数OnButtonAutoStopAll
self.button_autoStopAll=wx.Button(id=wxID_FRAME1BUTTONAUTOSTARTALL,label=u'AUTOSTOPALL', 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'AgencyFB')) self.button_autoStopAll.Bind(wx.EVT_BUTTON,self.OnButtonAutoStopAll, id=wxID_FRAME1BUTTONAUTOSTARTALL)
再有ComboBox控件实现点击下拉时自动加载当前串口名
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)
下拉菜单事件函数
defevt_combox_dropdown(self,event): print'combox%ddropdown'%self.sta_num serial_list=list(serial.tools.list_ports.comports()) ifserial_list:#判断是否为空 portName_list=[] #转换serialhandle为portname foriinrange(0,len(serial_list)): portname=list(serial_list[i]) portName_list.append(str(portname[0])) printportName_list self.combox.SetItems(portName_list)
然后进入正题,这里根据DSR信号来触发。
#串口自动触发检测线程 classJob(threading.Thread): ... defrun(self): whileself.__running.isSet(): self.__flag.wait()#为True时立即返回,为False时阻塞直到内部的标识位为True后返回 print"intojobfunction" i=0 isOpen=serial_isOpen(i) ifserial_list[i]!=1andisOpen: now_dsr=serial_list[i].getDSR() ifnow_dsr!=last_dsr[i]: last_dsr[i]=now_dsr print'dsrlevelchangedto%d'%now_dsr ifnow_dsr==True: ifthread_list[i]!=1: if~thread_list[i].is_alive(): serial_Open(0,False) #dosomething else: serial_Open(0,False) #dosomething break time.sleep(1) ...
即每当DSR信号置低时触发工作
以上这篇python实现串口自动触发工作的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。