完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
由于昨天早晨没有调试成功,心里一直不爽,觉都睡不好,于是今早不到2点就起来继续调试,终于把昨天的BUG找到了,还是串口参数配置的问题,先上代码。
#! /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'打开串口失败' 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 # 接收显示是否为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): ''' 格式化接收到的数据字符串 示例: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() 这段代码,配合前2次的代码,就可以运行串口的接收了,很是兴奋啊,下面还有串口的发送没有调试,改天搞定了,再次上传给大家。 下面就BUG说明一下: 大家看我的代码处有好多的#注释,就是我在调试时用于确认哪一步出错的,大家也可以试试这种调试方式。 发稿与2018年1月24日早 评分
|
||
相关推荐 |
||
只有小组成员才能发言,加入小组>>
39个成员聚集在这个小组
加入小组3399 浏览 0 评论
6022 浏览 0 评论
2314 浏览 0 评论
5324 浏览 0 评论
1956 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-30 04:04 , Processed in 0.504025 second(s), Total 42, Slave 33 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号