-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathComToTcpServer.py
187 lines (159 loc) · 5.66 KB
/
ComToTcpServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# Script for piping a local serial connection to a TCP port.
#
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
import time
import sys
import win32pipe
import win32file
import pywintypes
import socket
import threading
import queue
import argparse
import serial
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--logfile", type=str,
help="File path to log traffic")
parser.add_argument("-p", "--port", type=int, default=5555,
help="The TCP port to listen for connections.")
parser.add_argument("-n", "--pipe", type=str,
help="The named pipe to connect to connect to.")
parser.add_argument("-c", "--comport", type=str,
help="The number of the COM device to connect to. E.G 'COM5'")
parser.add_argument("-b", "--baudrate", type=int, default=115200,
help="The baudrate of the serial port.")
parser.add_argument("-v", "--verbose", action="store_true",
help="Enabled verbose prints.")
args = parser.parse_args()
# Global queues used between threads.
out_queue = queue.Queue()
in_queue = queue.Queue()
logfile = None
inout_last = None
def socket_thread():
# Set up the socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', args.port))
sock.listen()
while True:
print("Waiting for socket...")
conn, addr = sock.accept()
print(f"Socket Connected - {addr}")
# clear the out queue before starting a connection
while not out_queue.empty():
out_queue.get()
# use a short timeout to move on if no data is ready.
conn.settimeout(0.01)
# process the queue.
connected = True
while connected:
try:
while not out_queue.empty():
conn.sendall(out_queue.get())
data = conn.recv(4096)
except socket.timeout:
data = None
except Exception as e:
print("Socket disconnected.")
connected = False
continue
if data is not None and len(data) != 0:
in_queue.put(data)
def write_log(inout, data):
global inout_last
global logfile
if logfile is None:
return
if inout != inout_last:
logfile.flush()
if inout:
logfile.write("\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
else:
logfile.write("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
inout_last = inout
string = data.decode("ascii")
logfile.write(string)
def listen_named_pipe():
print("Waiting for pipe...")
quit = False
while not quit:
try:
handle = win32file.CreateFile(
args.pipe,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
print("Pipe connected.")
while True:
if win32file.GetFileSize(handle) > 0:
hr, data = win32file.ReadFile(handle, 4096)
if (hr != 0):
print(f"Error reading: {hr}")
continue
write_log(False, data)
if args.verbose:
print(f"OUT: {data}")
out_queue.put(data)
while not in_queue.empty():
data = in_queue.get()
write_log(True, data)
if args.verbose:
print(f"IN: {data}")
win32file.WriteFile(handle, data, None)
except pywintypes.error as e:
if e.args[0] == 2:
if args.verbose:
print("No pipe yet, waiting...")
time.sleep(1)
elif e.args[0] == 109:
print("broken pipe")
quit = True
def listen_com_port():
print("Opening com port...")
serial_port = serial.Serial(args.comport, args.baudrate, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS,
timeout=0.1)
print(f"Opened {args.comport}.")
while True:
if serial_port.in_waiting > 0:
data = serial_port.read(size=serial_port.in_waiting)
write_log(False, data)
if args.verbose:
print(f"OUT: {data}")
out_queue.put(data)
while not in_queue.empty():
data = in_queue.get()
write_log(True, data)
if args.verbose:
print(f"IN: {data}")
serial_port.write(data)
def main():
if args.pipe is None and args.comport is None:
print("Must specify a serial connection!")
return
global logfile
if args.logfile is not None:
logfile = open(args.logfile, 'w')
logfile.write(f"arguments: {args} \n\n")
# Create the thread for the TCP port.
port_thread = threading.Thread(target=socket_thread)
port_thread.daemon = True
port_thread.start()
try:
if args.pipe is not None:
listen_named_pipe()
if args.comport is not None:
listen_com_port()
else:
raise Exception("No serial port to connect to!")
except:
if logfile is not None:
logfile.close()
main()