之前的一些东西:
所有这些都基于wxTerminal.py see the table at the bottom of the article
(Pyserial微型端口和WxPython GUI的组合)
使用: Python:2.7.14。 WxPython:4.0.0b2
我的问题是我有一个从我的设备读取串行数据的线程, 并尝试使用事件更新GUI:
class TerminalFrame(wx.Frame):
....
....
def ComPortThread(self):
"""\
Thread that handles the incoming traffic. Does the basic input
transformation (newlines) and generates an SerialRxEvent
"""
while self.alive.isSet():
b = self.serial.read(self.serial.in_waiting or 1)
if b:
# newline transformation
if self.settings.newline == NEWLINE_CR:
b = b.replace(b'\r', b'\n')
elif self.settings.newline == NEWLINE_LF:
pass
elif self.settings.newline == NEWLINE_CRLF:
b = b.replace(b'\r\n', b'\n')
event = SerialRxEvent(self.GetId(), b)
**ERROR!** >>> self.GetEventHandler().AddPendingEvent(event)
我收到错误:
File "C:/Users/DIMA/Desktop/pyserial-master/pyserial-master/examples/wxTerminal.py", line 349, in ComPortThread
self.GetEventHandler().AddPendingEvent(event)
wxAssertionError: C++ assertion "event" failed at ..\..\src\common\event.cpp(1246) in wxEvtHandler::QueueEvent(): NULL event can't be posted
缺少什么?
SERIALRX = wx.NewEventType()
# bind to serial data receive events
EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
class SerialRxEvent(wx.PyCommandEvent):
eventType = SERIALRX
def __init__(self, windowID, data):
wx.PyCommandEvent.__init__(self, self.eventType, windowID)
self.data = data
def Clone(self):
self.__class__(self.GetId(), self.data)
答案 0 :(得分:1)
这是一个(Linux)线程事件示例,它与python 2.7.12(wx 3.0)和python 3.5.2(wx 4.0)一起使用。它使用的是Tcp插槽而不是串口,但我确信你可以从中取出骨头。
用以下方法测试:
echo 'my data' | nc -q 1 127.0.0.1 5005
echo 'new data' | nc -q 1 127.0.0.1 5005
echo 'Quit' | nc -q 1 127.0.0.1 5005
import wx
import wx.lib.newevent
import socket
import threading
tcp_event, EVT_TCP_EVENT = wx.lib.newevent.NewEvent()
class MyMain(wx.Frame):
def __init__(self, *args, **kwds):
self.frame = wx.Frame.__init__(self, *args, **kwds)
self.SetTitle("Threaded Port Listener")
self.panel = wx.Panel(self)
self.data = wx.StaticText(self.panel,-1,"Nothing yet",pos=(10,10))
# Create a listening socket for external requests
tcp_port = 5005
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
print("Error on Socket")
# force re-use of the socket if it is in time-out mode after being closed
# other wise we can get bind errors after closing and attempting to start again
# within a minute or so
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self.sock.bind(("127.0.0.1", 5005))
except IOError as msg:
print("Error on Socket Bind 5005")
pass
try:
self.sock.listen((1))
except:
print("Error on Socket listen")
self.Bind(wx.EVT_CLOSE, self.OnExit)
self.Bind(EVT_TCP_EVENT, self.OnTcpThreadEvent)
#Start the thread
self.tcp = TcpThread(self,self.sock)
self.Show()
def OnTcpThreadEvent(self, event):
data = event.data.strip()
print ("data received",data)
self.data.SetLabel(data)
if data == "Quit":
self.OnExit(None)
def OnExit(self,event):
try:
self.tcp.stop() # Shutdown the tcp listener
self.tcp.join(0.1) # Wait 1/10 second for it to finish then give up
except Exception as e:
print (e)
pass
self.sock.close()
self.Destroy()
# The tcp thread is started as a daemon because this allows us to make the socket a blocking socket
# The advantage is that it does nothing until a request comes in.
# The disadvantage is that the sock.accept cannot be interrupted which makes closing a problem as it will wait
# With the tcp thread as a daemon we can perform a self.tcp.join(timeout) which allows the program to close and leaves
# the still running thread to be cleaned up by the system garbage collecter
class TcpThread(threading.Thread):
def __init__(self, tcp_target, sock):
threading.Thread.__init__(self)
self.sock = sock
self.tcp_target = tcp_target
self.stopthread = False
self.setDaemon(True)
self.start() # start the thread
def run(self):
while self.stopthread == False:
print ("listening")
try:
conn, addr = self.sock.accept()
except socket.timeout:
continue
except socket.error as e:
msg="tcp accept error",str(e)
print (msg)
break
try:
data = conn.recv(32).decode('UTF-8')
except socket.timeout:
continue
except IOError as e:
msg ="Error on socket receive "+str(e)
print (msg)
continue
evt = tcp_event(data=data,target=conn)
wx.PostEvent(self.tcp_target,evt)
self.sock.close()
def stop(self):
self.stopthread = True
if __name__ == "__main__":
myapp = wx.App()
MyMain(None)
myapp.MainLoop()
答案 1 :(得分:1)
这也是困扰我的事情。
由于某种原因,永远不会在wxPython classic中调用Clone
方法(尝试插入raise
)。而pyserial作者似乎错了。根据{{3}} Clone
方法返回新活动!
使用Phoenix(通过pip安装获得的)时,将调用Clone
方法。因为您返回None
,AddPendingEvent
会抱怨。
编辑事件类中的Clone方法(wxTerminal中的SerialRxEvent),使其正确返回:
def Clone(self):
# raise # uncomment this to show that this will not get called in classic
# instead of this
# self.__class__(self.GetId(), self.data)
# do this
return self.__class__(self.GetId(), self.data)
答案 2 :(得分:0)
我只是将wxPython 3.0.2(insted 4.0.0b2)安装到示例wxTerminal.py工作