幽灵资源网 Design By www.bzswh.com

最近在一个python工具中需要实现串口自动触发工作的功能,之前只在winform上面实现,今天使用python试试。这里简单记一下:

首先用wxpython实现一个Button,点击事件绑定函数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控件实现点击下拉时自动加载当前串口名

 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)

下拉菜单事件函数

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)

然后进入正题,这里根据DSR信号来触发。

#串口自动触发检测线程
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信号置低时触发工作

以上这篇python实现串口自动触发工作的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,串口,自动触发

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。