7579 views|4 replies

1903

Posts

0

Resources
The OP
 

Python practical serial port assistant---6 realize serial port reception [Copy link]

 

Because I didn't debug successfully yesterday morning, I was upset and couldn't sleep well, so I got up before 2 o'clock this morning to continue debugging. Finally, I found the BUG of yesterday, which turned out to be a problem with the serial port parameter configuration. Here is the code first.
  1. #! /usr/bin/env python # -*- coding: utf-8 -*- import time import datetime import threading import binascii import platform import logging import serial import SerialDeal import serialMainUI if platform.system() == "Windows": from serial.tools import list_ports elif platform.system() == "Linux": import glob, os, re import wx import wx.xrc logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') class MainSerialUI(serialMainUI.serialFrame): def __init__(self, master): super(MainSerialUI, self).__init__() self.ser = None self.receive_count = 0 self.receive_data = "" self.list_box_serial = None def __del__(self): if platform.system() == "Linux": try: self.ser.SetStopEvent() except: pass def m_openserOnButtonClick( self, event ): try: self.port = self.m_comset.GetValue() #print ("com is %s" % self.port) self.baudrate = self.m_comboBox2.GetValue() #print ("buadrate is %s" % self.baudrate) self.parity = self.m_comboBox4.GetValue() #print ("parity is %s" % self.parity) self.databit = self.m_comboBox3.GetValue() #print ("databit is %s" % self.databit) self.stopbit = self.m_comboBox5.GetValue() #print ("stopbit is %s" % self.stopbit) self.ser = SerialDeal.serDeal(Port=self.port, BaudRate=self.baudrate, ByteSize=self.databit, Parity=self.parity, Stopbits=self.stopbit) self.ser.start() if self.ser.alive: print 'succes' self.thread_read = threading.Thread(target=self.SerRead) self.thread_read.setDaemon(True) self.thread_read.start() else: print '0' except Exception as e: #logging.error(e) print u'Failed to open serial port' def SerRead(self): try: n = self.ser.serSer.inWaiting() if n: self.receive_data += self.ser.read(n).replace(binascii.unhexlify("00"), "") if self.ser.thresholdValue <= len(self.receive_data): self.receive_count += 1 # Whether the received display is Hex if self.m_rcvBox.get() == 1: self.receive_data = self.space_b2a_hex(self.receive_data) self.m_rcvtext.insert("end", "[" + str(datetime.datetime.now()) + " - " + str(self.receive_count) + "]:\n", "green") self.m_rcvtext.insert(" end", self.receive_data + "\n") self.m_rcvtext.see("end") self.receive_data = "" except Exception as e: logging.error(e) self.receive_data = "" self.ser.stop() self.ser = None def m_clrRcvTextOnButtonClick( self, event ): self.m_rcvtext.Clear() event.Skip() def space_b2a_hex(self, data): ''' format Example of the received data string: 123 --> 31 32 33 ''' new_data_list = list() new_data = "" hex_data = binascii.b2a_hex(data) temp_data = "" for index,value in enumerate(hex_data): temp_data += value if len(temp_data) == 2: new_data_list.append(temp_data) temp_data = "" for index,value in enumerate(new_data_list): if index%25 == 0 and index != 0: new_data += "\n" new_data += value new_data += " " return new_data def m_send1butOnButtonClick( self, event ): if self.ser.alive: send_data = self.m_textCtrl5.GetValue() #try: def m_clr1butOnButtonClick( self, event ): self.m_textCtrl5.Clear() def main(): app = wx. App() MainSerialUI(app).Show(True) app.MainLoop() if __name__ == '__main__': main()
复制代码
This code, combined with the previous two codes, can run the serial port receiving. I am very excited. There is still the serial port sending that has not been debugged. I will finish it another day and upload it to everyone again. Here is a description of the BUG: There are a lot of # comments in my code, which I use to confirm which step went wrong during debugging. You can also try this debugging method. ]This content is originally created by EEWORLD forum user star_66666. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the sourceb2a_hex(data) temp_data = "" for index,value in enumerate(hex_data): temp_data += value if len(temp_data) == 2: new_data_list.append(temp_data) temp_data = "" for index,value in enumerate(new_data_list) : if index%25 == 0 and index != 0: new_data += "\n" new_data += value new_data += " " return new_data def m_send1butOnButtonClick( self, event ): if self.ser.alive: send_data = self.m_textCtrl5.GetValue() #try: def m_clr1butOnButtonClick( self, event ): self.m_textCtrl5.Clear () def main(): app = wx.App() MainSerialUI(app).Show(True) app.MainLoop() if __name__ == '__main__': main()[/code] This code, combined with the previous two codes, can run the serial port receiving. I am very excited. There is still the serial port sending that has not been debugged. I will finish it another day and upload it to everyone again. Here is a description of the BUG: There are a lot of # comments in my code, which I use to confirm which step went wrong during debugging. You can also try this debugging method. ]This content is originally created by EEWORLD forum user star_66666. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the sourceb2a_hex(data) temp_data = "" for index,value in enumerate(hex_data): temp_data += value if len(temp_data) == 2: new_data_list.append(temp_data) temp_data = "" for index,value in enumerate(new_data_list) : if index%25 == 0 and index != 0: new_data += "\n" new_data += value new_data += " " return new_data def m_send1butOnButtonClick( self, event ): if self.ser.alive: send_data = self.m_textCtrl5.GetValue() #try: def m_clr1butOnButtonClick( self, event ): self.m_textCtrl5.Clear () def main(): app = wx.App() MainSerialUI(app).Show(True) app.MainLoop() if __name__ == '__main__': main()[/code] This code, combined with the previous two codes, can run the serial port receiving. I am very excited. There is still the serial port sending that has not been debugged. I will finish it another day and upload it to everyone again. Here is a description of the BUG: There are a lot of # comments in my code, which I use to confirm which step went wrong during debugging. You can also try this debugging method. ]This content is originally created by EEWORLD forum user star_66666. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source





Latest reply

Yes, I just registered this website recently and saw your previous post. Thank you very much.  Details Published on 2018-1-24 10:40
 
 

5

Posts

0

Resources
2
 
Did you use QT to make it? Can you please send me the interface?

Comments

It's made with wxpython, and has an interface. Look at the previous post, this is a continuation of a project. Look at 3 or 4, there is an interface.  Details Published on 2018-1-24 09:21
 
 
 

1903

Posts

0

Resources
3
 
powerniu posted on 2018-1-24 08:46 Did you use QT to make it? Can you post the interface for us to see?
It is made with wxpython, and has an interface. Look at the previous post, this is a continuation of a project. Look at 3 or 4, there is an interface
 
 
 

5

Posts

0

Resources
4
 
star_66666 posted on 2018-1-24 09:21 Made with wxpython, it has an interface. Look at the previous post. This is a continuation of a project. Look at 3 or 4, which have an interface
Yes, I just registered this website recently and saw your previous post. Thank you very much.

Comments

You are welcome. I have just started learning and am working on my second project. I have not yet completed this serial port assistant. I learned it in my spare time after work. If you have time, let's discuss and learn together.  Details Published on 2018-1-24 10:42
 
 
 

1903

Posts

0

Resources
5
 
powerniu posted on 2018-1-24 10:40 Yes, I just registered this website recently and saw your previous post. Thank you very much.
You are welcome. I have just started learning and am just working on my second project. I haven’t finished this serial port assistant yet. I learned it in my spare time after work. If you have time, let’s discuss and learn together.
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list