Skip to content

Commit 7c5da3f

Browse files
authored
ComToTcpServer: Switch to new flag for formatted output (#6)
ComToTcpServer: Switch to new flag for formatted output The original intention of the verbose flag was to enable debugger the debugger traffic, but an emerged use case of the flag is to monitor device logging while not broken into the debugger. This commit removes the verbose option and adds an show option that cleanly outputs the traffic and a debug option intended for debug prints.
1 parent 523bd29 commit 7c5da3f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

Scripts/ComToTcpServer.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
help="The number of the COM device to connect to. E.G 'COM5'")
2828
parser.add_argument("-b", "--baudrate", type=int, default=115200,
2929
help="The baudrate of the serial port.")
30-
parser.add_argument("-v", "--verbose", action="store_true",
31-
help="Enabled verbose prints.")
30+
parser.add_argument("-s", "--show", action="store_true",
31+
help="Shows the traffic on the console.")
32+
parser.add_argument("-d", "--debug", action="store_true",
33+
help="Enables debug printing.")
3234
args = parser.parse_args()
3335

3436
# Global queues used between threads.
@@ -115,20 +117,23 @@ def listen_named_pipe():
115117
continue
116118

117119
write_log(False, data)
118-
if args.verbose:
119-
print(f"OUT: {data}")
120+
if args.show:
121+
text = data.decode('ascii', errors='replace')
122+
print(f"{text}")
120123
out_queue.put(data)
121124

122125
while not in_queue.empty():
123126
data = in_queue.get()
124127
write_log(True, data)
125-
if args.verbose:
126-
print(f"IN: {data}")
128+
if args.show:
129+
text = data.decode('ascii', errors='replace')
130+
# prints with red color
131+
print("\033[31m" + text + "\033[0m")
127132
win32file.WriteFile(handle, data, None)
128133

129134
except pywintypes.error as e:
130135
if e.args[0] == 2:
131-
if args.verbose:
136+
if args.debug:
132137
print("No pipe yet, waiting...")
133138
time.sleep(1)
134139
elif e.args[0] == 109:
@@ -146,15 +151,18 @@ def listen_com_port():
146151
if serial_port.in_waiting > 0:
147152
data = serial_port.read(size=serial_port.in_waiting)
148153
write_log(False, data)
149-
if args.verbose:
150-
print(f"OUT: {data}")
154+
if args.show:
155+
text = data.decode('ascii', errors='replace')
156+
print(f"{text}")
151157
out_queue.put(data)
152158

153159
while not in_queue.empty():
154160
data = in_queue.get()
155161
write_log(True, data)
156-
if args.verbose:
157-
print(f"IN: {data}")
162+
if args.show:
163+
text = data.decode('ascii', errors='replace')
164+
# prints with red color
165+
print("\033[31m" + text + "\033[0m")
158166
serial_port.write(data)
159167

160168
def main():

0 commit comments

Comments
 (0)