Skip to content

Commit fe37bf8

Browse files
committed
fix serial receive flush buffer time and fix color
1 parent 50a7861 commit fe37bf8

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

COMTool/conn/conn_serial.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ def getCommboboxItems(obj):
262262
elif conf_type == "stopbits":
263263
value = float(value)
264264
self.com.__setattr__(conf_type, value)
265+
if conf_type == "baudrate":
266+
self.oneByteTime = 1 / (self.com.baudrate / (self.com.bytesize + 2 + self.com.stopbits)) # 1 byte use time
265267
elif conf_type == "flowcontrol":
266268
values = getCommboboxItems(obj)
267269
idx = 0
@@ -439,7 +441,7 @@ def receiveDataProcess(self):
439441
buffer += data
440442
continue
441443
buffer += data
442-
if buffer and (time.time() - t > 0.001): # no new data in 1ms
444+
if buffer and (time.time() - t > self.oneByteTime * 2): # no new data in next frame
443445
try:
444446
self.onReceived(buffer)
445447
except Exception as e:

COMTool/plugins/dbg.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,35 @@ def _getColorByfmt(self, fmt:bytes):
693693
return color, bg
694694

695695
def _texSplitByColor(self, text:bytes):
696+
remain = b''
696697
ignoreCodes = [rb'\x1b\[\?.*?h', rb'\x1b\[\?.*?l']
697698
text = text.replace(b"\x1b[K", b"")
698699
for code in ignoreCodes:
699700
colorFmt = re.findall(code, text)
700701
for fmt in colorFmt:
701702
text = text.replace(fmt, b"")
702703
colorFmt = re.findall(rb'\x1b\[.*?m', text)
704+
if text.endswith(b"\x1b"): # ***\x1b
705+
text = text[:-1]
706+
remain = b'\x1b'
707+
elif text.endswith(b"\x1b["): # ***\x1b[
708+
text = text[:-2]
709+
remain = b'\x1b['
710+
else: # ****\x1b[****, ****\x1b[****;****m
711+
idx = -2
712+
idx_remain = -1
713+
while 1:
714+
idx = text.find(b"\x1b[", len(text) - 10 + idx + 2) # \x1b[00;00m]
715+
if idx < 0:
716+
break
717+
remain = text[idx:]
718+
idx_remain = idx
719+
if len(remain) > 0:
720+
match = re.findall(rb'\x1b\[.*?m', remain) # ****\x1b[****;****m***
721+
if len(match) > 0: # have full color format
722+
remain = b''
723+
else:
724+
text = text[:idx_remain]
703725
plaintext = text
704726
for fmt in colorFmt:
705727
plaintext = plaintext.replace(fmt, b"")
@@ -716,25 +738,27 @@ def _texSplitByColor(self, text:bytes):
716738
colorStrs.append([self.lastColor, self.lastBg, text[p:]])
717739
else:
718740
colorStrs = [[self.lastColor, self.lastBg, text]]
719-
return plaintext, colorStrs
741+
return plaintext, colorStrs, remain
720742

721743
def getColoredText(self, data_bytes, decoding=None):
722-
plainText, coloredText = self._texSplitByColor(data_bytes)
744+
plainText, coloredText, remain = self._texSplitByColor(data_bytes)
723745
if decoding:
724746
plainText = plainText.decode(encoding=decoding, errors="ignore")
725747
decodedColoredText = []
726748
for color, bg, text in coloredText:
727749
decodedColoredText.append([color, bg, text.decode(encoding=decoding, errors="ignore")])
728750
coloredText = decodedColoredText
729-
return plainText, coloredText
751+
return plainText, coloredText, remain
730752

731753
def bytes2String(self, data : bytes, showAsHex : bool, encoding="utf-8"):
732754
isHexString = False
733755
dataColored = None
734756
if showAsHex:
735757
return True, utils.hexlify(data, ' ').decode(encoding=encoding), dataColored
736758
try:
737-
dataPlain, dataColored = self.getColoredText(data, self.configGlobal["encoding"])
759+
dataPlain, dataColore, remain = self.getColoredText(data, self.configGlobal["encoding"])
760+
if remain:
761+
dataPlain += remain.decode(encoding=self.configGlobal["encoding"], errors="ignore")
738762
except Exception:
739763
dataPlain = utils.hexlify(data, ' ').decode(encoding=encoding)
740764
isHexString = True
@@ -755,12 +779,13 @@ def receiveDataProcess(self):
755779
new_line = True
756780
logData = None
757781
buffer = b''
782+
remain = b''
758783
while(not self.receiveProgressStop):
759784
logData = None
760-
bytes = b''
761785
head = ""
762786
self.lock.acquire()
763-
buffer += b"".join(self.receivedData)
787+
new = b"".join(self.receivedData)
788+
buffer += new
764789
self.receivedData = []
765790
# timeout, add new line
766791
if time.time() - timeLastReceive> self.config["receiveAutoLindefeedTime"]:
@@ -770,8 +795,6 @@ def receiveDataProcess(self):
770795
else:
771796
head += "\n"
772797
new_line = True
773-
if bytes:
774-
timeLastReceive = time.time()
775798
data = ""
776799
# have data in buffer
777800
if len(buffer) > 0:
@@ -790,8 +813,8 @@ def receiveDataProcess(self):
790813
# show as string, and need to render color, wait for \n or until timeout to ensure color flag in buffer
791814
else:
792815
if time.time() - timeLastReceive > self.config["receiveAutoLindefeedTime"] or b'\n' in buffer:
793-
data, colorData = self.getColoredText(buffer, self.configGlobal["encoding"])
794-
buffer = b''
816+
data, colorData, remain = self.getColoredText(buffer, self.configGlobal["encoding"])
817+
buffer = remain
795818
# add time receive head
796819
# get data from buffer, now render
797820
if data:
@@ -813,6 +836,8 @@ def receiveDataProcess(self):
813836
new_line = False
814837
self.receiveUpdateSignal.emit(head, [colorData], self.configGlobal["encoding"])
815838
logData = head + data
839+
if len(new) > 0:
840+
timeLastReceive = time.time()
816841

817842
while len(self.sendRecord) > 0:
818843
self.onLog(self.sendRecord.pop())

0 commit comments

Comments
 (0)