This commit is contained in:
yumoqing 2022-11-10 00:50:12 +08:00
parent 6894c7fb3a
commit 1b3325f2e6
2 changed files with 18 additions and 5 deletions

View File

@ -28,7 +28,10 @@ class UdpComm:
def run(self): def run(self):
sock = self.udpSerSock sock = self.udpSerSock
while self.run_flg: while self.run_flg:
in_s, out_s, exc_s = select([sock], [sock], []) outs = []
if len(self.buffer) > 0:
outs = [sock]
in_s, out_s, exc_s = select([sock], outs, [], 0.1)
if sock in in_s: if sock in in_s:
b, addr = sock.recvfrom(BUFSIZE) b, addr = sock.recvfrom(BUFSIZE)
t = b[0] t = b[0]
@ -36,17 +39,27 @@ class UdpComm:
if t == 'b': if t == 'b':
self.callback(b, addr) self.callback(b, addr)
else: else:
try:
txt = b.decode('utf-8') txt = b.decode('utf-8')
d = json.loads(txt) d = json.loads(txt)
self.callback(d, addr) self.callback(d, addr)
except Exception as e:
print('except:',e)
print_exc()
print(t, b)
break
if sock in out_s: if sock in out_s:
while len(self.buffer) > 0: while len(self.buffer) > 0:
d,addr = self.buffer.pop(0) d,addr = self.buffer.pop(0)
sock.sendto(d, addr) sock.sendto(d, addr)
time.sleep(0.1)
self.run_flg = False
self.udpSerSock.close() self.udpSerSock.close()
def stop(self): def stop(self):
self.run_flg = False self.run_flg = False
self.udpSerSock.close()
self.thread.join()
def broadcast(self, data): def broadcast(self, data):
broadcast_host = '.'.join(self.host.split('.')[:-1]) + '.255' broadcast_host = '.'.join(self.host.split('.')[:-1]) + '.255'

View File

@ -1 +1 @@
__version__ = '5.1.24' __version__ = '5.1.25'