串口小工具KKCOM-Python

    xiaoxiao2021-03-25  133

    main UI :

    # encoding: UTF-8 ''' Created on 2017-03-06 @author: Harbor ''' import wx from serial import Serial from wx.lib.pubsub import pub import MySerialThread class MainUI(wx.Frame): def ON_btn_openCOM_Clicked(self,evt): print "ON_btn_openCOM_Clicked===>" print self.COM_NO.GetValue() print self.BandRate.GetValue() print self.DataBit.GetValue() print self.StopBit.GetValue() print self.ParityBit.GetValue() print self.FlowControl.GetValue() #======================================================================= # 1.判断串口是否开启 # 2.获取串口相关参数 波特率 数据位 停止位 校验位 流控制 # 3.开启串口 # 4.设置按钮状态 #======================================================================= if not self.ser.isOpen(): try: self.ser.timeout = 1 self.ser.xonxoff = 0 self.ser.port = self.COM_NO.GetValue() self.ser.baudrate = int(self.BandRate.GetValue()) self.ser.bytesize = int(self.DataBit.GetValue()) self.ser.stopbits = int(self.StopBit.GetValue()) self.ser.parity = self.ParityBit.GetValue()[0] self.ser.open() except Exception , e: print 'COMM Open Fail!!',e else: self.btn_openCOM.SetLabel(u"关闭串口") print "=====>串口已开启" else: self.ser.close() # while self.Ser.isOpen(): pass self.btn_openCOM.SetLabel(u"打开串口") print "=====>串口已关闭" def OnDisplayListenner(self,log): self.display_log.AppendText(log) def ON_btn_clearCOM_Clicked(self,log): self.display_log.Clear() def ON_btn_closeLog_Clicked(self,log): if self.ser.isOpen(): self.ser.write("###") def ON_btn_openLog_Clicked(self,log): if self.ser.isOpen(): self.ser.write("@@@") def ON_btn_RESET_Clicked(self,log): if self.ser.isOpen(): self.ser.write("RST") def OnCloseWindow(self,evt): self.serialThread.stop() self.Destroy() print "OnCloseWindow=====>" def __init__(self): ''' Constructor ''' wx.Frame.__init__(self,None,title =u"串口Serial COMM v1.0",size = wx.Size(800,600)) # 显示日志区域 垂直放置 self.display_log = wx.TextCtrl(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,style=wx.TE_RICH|wx.TE_MULTILINE) #监听文本显示 pub.subscribe(self.OnDisplayListenner, "update") inner1 = wx.BoxSizer(wx.HORIZONTAL) inner1.Add(self.display_log,1,wx.EXPAND|wx.ALL,5) #串口号 开启关闭按钮 #--------------------------------------- self.com_Lable = wx.StaticText(self,wx.ID_ANY,u"串口号:",wx.DefaultPosition,wx.DefaultSize,0) COM_NO_choices = [u"COM3",u"COM4",u"COM5",u"COM6"] self.COM_NO = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,COM_NO_choices,wx.CB_READONLY) self.COM_NO.SetSelection(0) #设置默认为COM3 self.btn_openCOM = wx.Button(self,wx.ID_ANY,u"打开串口",wx.DefaultPosition,wx.DefaultSize,0) self.btn_clearCOM = wx.Button(self,wx.ID_ANY,u"清除日志",wx.DefaultPosition,wx.DefaultSize,0) self.btn_CloseLog = wx.Button(self,wx.ID_ANY,u"关闭日志",wx.DefaultPosition,wx.DefaultSize,0) self.btn_OpenLog = wx.Button(self,wx.ID_ANY,u"开启日志",wx.DefaultPosition,wx.DefaultSize,0) self.btn_RESET = wx.Button(self,wx.ID_ANY,u"重启车机",wx.DefaultPosition,wx.DefaultSize,0) inner2 = wx.BoxSizer(wx.HORIZONTAL) inner2.Add(self.com_Lable,0,wx.ALL,5) inner2.Add(self.COM_NO,0,wx.ALL,5) inner2.Add(self.btn_openCOM,0,wx.ALL,5) inner2.Add(self.btn_clearCOM,0,wx.ALL,5) inner2.Add(self.btn_CloseLog,0,wx.ALL,5) inner2.Add(self.btn_OpenLog,0,wx.ALL,5) inner2.Add(self.btn_RESET,0,wx.ALL,5) #--------------------------------------- #波特率 #--------------------------------------- self.BandRate_Lable = wx.StaticText(self,wx.ID_ANY,u"波特率:",wx.DefaultPosition,wx.DefaultSize,0) BandRate_choices = [u"115200",u"128000",u"256000"] self.BandRate = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,BandRate_choices,wx.CB_READONLY) self.BandRate.SetSelection(0) #设置默认为115200 inner3 = wx.BoxSizer(wx.HORIZONTAL) inner3.Add(self.BandRate_Lable,0,wx.ALL,5) inner3.Add(self.BandRate,0,wx.ALL,5) #--------------------------------------- #数据位DataBit #--------------------------------------- self.DataBit_Lable = wx.StaticText(self,wx.ID_ANY,u"数据位:",wx.DefaultPosition,wx.DefaultSize,0) DataBit_choices = [u"5",u"6",u"7",u"8"] self.DataBit = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,DataBit_choices,wx.CB_READONLY) self.DataBit.SetSelection(3) #设置默认为8 inner4 = wx.BoxSizer(wx.HORIZONTAL) inner4.Add(self.DataBit_Lable,0,wx.ALL,5) inner4.Add(self.DataBit,0,wx.ALL,5) #--------------------------------------- #停止位StopBit #-------------------------------------------- self.StopBit_Lable = wx.StaticText(self,wx.ID_ANY,u"停止位:",wx.DefaultPosition,wx.DefaultSize,0) StopBit_choices = [u"1",u"1.5",u"2"] self.StopBit = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,StopBit_choices,wx.CB_READONLY) self.StopBit.SetSelection(0) #设置默认为1 inner5 = wx.BoxSizer(wx.HORIZONTAL) inner5.Add(self.StopBit_Lable,0,wx.ALL,5) inner5.Add(self.StopBit,0,wx.ALL,5) #-------------------------------------------- #校验位ParityBit #-------------------------------------------- self.ParityBit_Lable = wx.StaticText(self,wx.ID_ANY,u"校验位:",wx.DefaultPosition,wx.DefaultSize,0) ParityBit_choices = [u"None",u"Odd",u"Even",u"Mark",u"Space"] self.ParityBit = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,ParityBit_choices,wx.CB_READONLY) self.ParityBit.SetSelection(0) #设置默认为None inner6 = wx.BoxSizer(wx.HORIZONTAL) inner6.Add(self.ParityBit_Lable,0,wx.ALL,5) inner6.Add(self.ParityBit,0,wx.ALL,5) #-------------------------------------------- #流控制FlowControl #-------------------------------------------- self.FlowControl_Lable = wx.StaticText(self,wx.ID_ANY,u"流控制:",wx.DefaultPosition,wx.DefaultSize,0) FlowControl_choices = [u"None",u"Hardware",u"Software",u"Custom"] self.FlowControl = wx.ComboBox(self,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.DefaultSize,FlowControl_choices,wx.CB_READONLY) self.FlowControl.SetSelection(0) #设置默认为None inner7 = wx.BoxSizer(wx.HORIZONTAL) inner7.Add(self.FlowControl_Lable,0,wx.ALL,5) inner7.Add(self.FlowControl,0,wx.ALL,5) #-------------------------------------------- # 大容器 outer = wx.BoxSizer(wx.VERTICAL) outer.Add(inner1,1,wx.EXPAND,5) outer.Add(inner2,0,wx.ALL,5) outer.Add(inner3,0,wx.ALL,5) outer.Add(inner4,0,wx.ALL,5) outer.Add(inner5,0,wx.ALL,5) outer.Add(inner6,0,wx.ALL,5) outer.Add(inner7,0,wx.ALL,5) self.SetSizer(outer) #绑定按钮事件 self.btn_openCOM.Bind(wx.EVT_BUTTON, self.ON_btn_openCOM_Clicked,self.btn_openCOM) self.btn_clearCOM.Bind(wx.EVT_BUTTON, self.ON_btn_clearCOM_Clicked,self.btn_clearCOM) self.btn_CloseLog.Bind(wx.EVT_BUTTON, self.ON_btn_closeLog_Clicked,self.btn_CloseLog) self.btn_OpenLog.Bind(wx.EVT_BUTTON, self.ON_btn_openLog_Clicked,self.btn_OpenLog) self.btn_RESET.Bind(wx.EVT_BUTTON, self.ON_btn_RESET_Clicked,self.btn_RESET) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) #初始化串口 self.ser = Serial() #执行线程 self.serialThread = MySerialThread.SerialThread(self.ser) print self.serialThread.getName() if __name__ == '__main__': app = wx.App() frm = MainUI() frm.Show() app.MainLoop() MySerialThread:

    # encoding: UTF-8 ''' Created on 2017-03-08 @author: Harbor ''' import wx import time import threading from wx.lib.pubsub import pub import MyTool class SerialThread(threading.Thread): def __init__(self, ser): ''' Constructor ''' super(SerialThread,self).__init__() self.rec_Num = 0 self.ser = ser self.quit = threading.Event() self.quit.clear() self.start() def run(self): while True: if self.ser.isOpen() and self.ser.inWaiting(): if self.quit.is_set(): break print "****************" + MyTool.getCurrentTime()+ "********************" # print self.ser.isOpen() self.rec_Num += self.ser.inWaiting() print "---rec_Num----%d--" % self.rec_Num com_log = self.ser.read(self.ser.inWaiting()) # print com_log wx.CallAfter(pub.sendMessage,'update',log = com_log) # time.sleep(1) def stop(self): self.quit.set() MyTool:

    # encoding: UTF-8 ''' Created on 2017-03-09 上午10:31:32 @author: Harbor ''' #=============================================================================== # 小工具 #=============================================================================== import time def getCurrentTime(): return time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime(time.time()))

    转载请注明原文地址: https://ju.6miu.com/read-11133.html

    最新回复(0)