串口 助手 过程 python
本帖最后由 lee_st 于 2018-1-23 05:23 编辑
天终于有时间继续调试,可是在调试过程中添加运行代码时出现问题,希望哪位高人指点一下,谢谢。先上代码。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import ti me
import datetime
import threading
import binascii
import platform
import logging
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=None):
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()
self.baudrate = self.m_comboBox2.GetValue()
self.parity = self.m_comboBox3.GetValue()
self.databit = self.m_comboBox4.GetValue()
self.stopbit = self.m_comboBox5.GetValue()
self.ser = SerialDeal.serDeal(Port=self.port,
BaudRate=self.baudrate,
ByteSize=self.databit,
Parity=self.parity,
Stopbits=self.stopbit)
self.ser.open()
if self.ser.alive:
self.thread_read = threading.Thread(target=self.SerRead)
self.thread_read.setDaemon(True)
self.thread_read.start()
except Exception as e:
logging.error(e)
event.Skip()
def SerialRead(self):
try:
n = self.ser.inWaiting()
if n:
self.receive_data += self.ser.read(n).replace(binascii.unhexlify("00"), "")
if self.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 main():
App = wx.PySimpleApp()
MainSerialUI().Show(True)
App.MainLoop()
if __name__ == '__main__':
main()
总是提示错误见下图
编译不能通过,所以编写的代码不能时时check,很是郁闷,百度查了,也是无果,求助于各位大神,谢谢!
发稿与2018年1月23日
更多回帖