Commits

gfcheng committed b48b468

发送数据功能实现

Comments (0)

Files changed (1)

         wx.Frame.__init__(self, None, wx.ID_ANY, u"cgf's com shell",
             wx.DefaultPosition, wx.Size(500, 400), wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
 
-        self.threads = []
-        self.serial_ports = []
-        self.cur_port_number = ""
-        self.cur_port_com = ""
-        self.find_no_port = True
+        self.TX_count = 0   #发送计数
+        self.RX_count = 0   #接收计数
         self.is_linking_to_port = False
         # 循环扫描可用COM端
         #for i in range(256):
 
         # 绑定事件
         self.Bind(wx.EVT_BUTTON, self.OnClear, self.clear_button)
-        #self.Bind(wx.EVT_BUTTON, self.OnSend, self.send_button)
+        self.Bind(wx.EVT_BUTTON, self.OnSend, self.send_button)
         self.Bind(wx.EVT_BUTTON, self.OnOpen_Port, self.open_port_button)
         self.Bind(wx.EVT_BUTTON, self.OnClose_Port, self.close_port_button)
         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
         self.option_stopbits.SetSelection(0)
         self.option_parity.SetSelection(0)
 
-
-        self.option_newline = 2
-        #print self.serial_port_dict
     def List_To_Dict(self, m_list):
             n_list = []
             for i in range(len(m_list)):
 
         #wx.MessageBox(u'当前串口号被占用,或其它错误','ERROR', wx.OK | wx.ICON_EXCLAMATION)
 
+    def OnSend(self, event):
+        if not self.is_linking_to_port:
+            return
+        msg = self.send_text.GetValue()
+        if not msg:
+            return
+        try:
+            msg = msg.encode('gb18030')
+        except Exception,e:
+            print e
+        else:
+            self.serial.write(msg)
+            print "Send: %s" %msg
+            self.TX_count += len(msg)
+            self.SetStatusText(u"S:%d R:%d" %(self.TX_count,self.RX_count),2)
+
     def OnOpen_Port(self, event):
         try:
             self.serial.port =  self.option_port.GetSelection()
                 self.is_linking_to_port = True
                 self.SetStatusText(u"已连接%s  [%s-%s-%s-%s]" % (self.serial_port_dict[self.option_port.GetSelection()],
                     self.serial.baudrate, self.serial.bytesize,self.serial.stopbits, self.serial.parity),1)
-                self.SetStatusText(u"",2)
+                self.SetStatusText(u"S:0 R:0", 2)
                 self.task = Recv_Thread(self,self.serial)
                 self.task.start()
 
         text = text.replace('\r', '\n')
         if self.display_as_hex.IsChecked():
             text = binascii.b2a_hex(text)
-            print text
-            text = text.replace('0a', '\n')
-            print 
+            print "Hex:%s" %text
+            text = text.replace('0a', '\n') #还原16进制的\n
         self.recv_text.AppendText(text)
+        self.RX_count += len(text)
+        self.SetStatusText(u"S:%d R:%d" %(self.TX_count,self.RX_count),2)
+
 
-        
 
 class Recv_Thread(threading.Thread):
     def __init__(self, window, ser):
                 n = self.serial.inWaiting() #检查是否还有未接收的字符
                 if n:
                     text = text + self.serial.read(n)
+                print "Recv:%s" %text
                 wx.CallAfter(self.window.Update_Recv_Text, text)
-            #time.sleep(0.5)
         print "stop"
 
 if __name__ == '__main__':