From 9f91cd63a0390277d672f71403d4ca0bb13b0a1e Mon Sep 17 00:00:00 2001 From: vc4 <287920192@qq.com> Date: Tue, 26 Nov 2024 20:03:38 +0800 Subject: [PATCH] bacup codes --- scomm.py | 21 ++++++++++++++------- usercfg.json | 32 ++++++++++++++++---------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/scomm.py b/scomm.py index 116482d..142186a 100755 --- a/scomm.py +++ b/scomm.py @@ -1,6 +1,6 @@ #! /usr/bin/env python3 -import os,sys,json +import os,sys,json,string import serial import serial.tools.list_ports import tkinter @@ -19,6 +19,8 @@ def strnow(): return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] def tohex(b): return ' '.join(['%02X'%x for x in b]) +def human_string(data, is_hex=False, encoding=False): + return is_hex and tohex(data) or data.decode(encoding,'ignore') def uint16(b): return b[0]*256+b[1] @@ -102,7 +104,7 @@ class UIproc(): _i0,fg='end','black' if cate == 'send' and self.ckbtn_sendshow.var.get(): text += MARK('> ') - text += self.ckbtn_shex.var.get() and tohex(data) or data.decode(encoding, 'ignore') + text += human_string(data, self.ckbtn_shex.var.get(), encoding) _i0 = self.text_recv.index('end') self.text_recv.insert('end', '\n%s'%text.translate({0:'<00>'})) self.lastRecvTicks = 0 @@ -113,13 +115,13 @@ class UIproc(): self.lastCursor = self.text_recv.index('end') self.lastRecvData = data text += MARK('< ') - text += self.ckbtn_rhex.var.get() and tohex(data) or data.decode(encoding, 'ignore') + text += human_string(data, self.ckbtn_rhex.var.get(), encoding) #self.text_recv.insert('end', '\n'+text) else: data = self.lastRecvData + data self.lastRecvData = data text += MARK('< ') - text += self.ckbtn_rhex.var.get() and tohex(data) or data.decode(encoding, 'ignore') + text += human_string(data, self.ckbtn_rhex.var.get(), encoding) _i0 = self.lastCursor #self.text_recv.insert(_i0, '\n'+text) for cb in self.root.unpack.values(): @@ -128,7 +130,7 @@ class UIproc(): except: pass self.text_recv.delete(_i0,'end') _i0 = self.text_recv.index('end') - self.text_recv.insert(_i0, '\n%s'%text.translate({0:'<00>'})) + self.text_recv.insert(_i0, '\n%s'%str(text)) self.lastRecvTicks = ts fg='blue' _i1 = self.text_recv.index('end') @@ -315,7 +317,12 @@ class SerComm(): self.ui.log('%s: openClose trace' % (self.com.port)) def clearWin(self): - self.ui.clear_recvtext() + if self.com.is_open: + self.com.is_open = False + self.ui.clear_recvtext() + self.com.is_open = True + else: + self.ui.clear_recvtext() def saveFile(self): self.ui.save_recvtext() @@ -457,7 +464,7 @@ if __name__ == '__main__': root.entry('entry-split').set(root.usercfg.get('split','99ms')) root.entry('entry-cycle').set(root.usercfg.get('cycle','1024ms')) root.entry('entry-baud').set(root.usercfg.get('baud','9600')) - root.entry('entry-encoding').set(root.usercfg.get('encoding','gbk')) + root.entry('entry-encoding').set(root.usercfg.get('encoding','utf8')) root.entry('entry-uservar').set(root.usercfg.get('uservar','')) root.entry('entry-sendText', key='', cmd=lambda x:comm.sendData()).set('') root.button('btn-scan', cmd=lambda:comm.detectSerialPort()) diff --git a/usercfg.json b/usercfg.json index efdc6a5..d078af3 100644 --- a/usercfg.json +++ b/usercfg.json @@ -2,18 +2,18 @@ "baud": "9600", "btn-data01": { "hex": 1, - "title": "DOWN", - "value": "55 AA 0A FC 01 10 01 01 10 02 01 00 05 EE EE" + "title": "\u88ab\u52a8\u6a21\u5f0f", + "value": " 0A 00 35 00 02 04 02 06 01 44 18" }, "btn-data02": { "hex": 1, - "title": "UP", - "value": "55 AA 0A FC 01 10 01 01 10 02 01 00 04 EE EE" + "title": "\u5de1\u68c0\u6a21\u5f0f", + "value": "0A 00 35 01 02 04 02 06 01 6F 1C" }, "btn-data03": { "hex": 1, - "title": "reboot", - "value": "55 AA 0A FC 01 10 01 01 10 04 01 01 FF EE EE" + "title": "F3\u5de1\u68c0", + "value": "5A A5 07 A2 A4 10 10 00 01 F3 EE EE" }, "btn-data04": { "hex": 0, @@ -26,19 +26,19 @@ "value": "{\"dtr\":1,\"rts\":1,\"hex\":\"18FF\"}" }, "btn-data06": { - "hex": 0, - "title": "random", - "value": "'%s'%__import__('random').random()" + "hex": 1, + "title": "F1\u91cd\u542f", + "value": "5A A5 07 FE A1 10 10 00 01 F1 EE EE\n" }, "btn-data07": { "hex": 1, - "title": "wake", - "value": "55 AA 0A FC 01 10 01 01 10 04 01 03 00 EE EE\n" + "title": "\u5f00\u9501F3-0022", + "value": "5A A5 14 A2 A4 10 20 00 0E 12 0C 5A A5 07 00 16 10 10 04 01 03 FF EE FF EE" }, "btn-data08": { "hex": 1, - "title": "page1", - "value": "55 AA 0A FC 01 10 01 01 10 04 01 02 01 EE EE\n" + "title": "R:2000+18", + "value": "5A A5 06 FE A1 03 20 00 18 EE EE" }, "btn-unpack01": { "title": "HEX print", @@ -61,7 +61,7 @@ "value": "len(data)>=25 and data[2]==0x14 and '\\n\\K=%s\\nKuser=%s\\nad0fix=%s\\nmlevel=%s\\nfofactor=%s\\nKntc=%s\\n' % (\nuint16(data[9:]),\nuint16(data[11:]),\nuint16(data[13:]),\nuint16(data[15:]),\nuint16(data[17:]),\nuint16(data[19:]))\n\n" }, "cycle": "100", - "encoding": "gbk", - "split": "100", - "uservar": "HELLO" + "encoding": "utf8", + "split": "50", + "uservar": "2,32200,0,0" } \ No newline at end of file