-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathat_intercept_programmer.py
executable file
·115 lines (86 loc) · 3.32 KB
/
at_intercept_programmer.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
#!/usr/bin/env python3
#
# Connects programming software to serial port device (radio) via network.
#
# Programming software <-> virtual null modem cable <-> at_intercept_programmer.py <-> LAN <-> at_intercept_radio.py <-> Radio
# COM18 COM26 /dev/ttyACM0
#
# The data stream is sent via network to a script (at_intercept_radio.py) where
# the usb device is connected via USB. On the radio side the datastream can be exported
# for further investigation.
#
# This script connects to a virtual com port COM26 which is connected via a virtual
# null modem cable to the virtual com port COM18 which is used by the programming software.
# This virtual ports and cable can be provided by the COM0COM tool.
#
# Linux users can use
# socat -d -d pty,raw,echo=0,b4000000 pty,raw,echo=0,b4000000
# for emulating a virtual null modem cable.
import serial
import time
import socket
import sys
import select
# config
servername = '192.168.1.2' # ip or hostname of server
serverport = 4242
comport = 'COM26' # connected to COM18 with com0com. use COM18 in CPS
# parameters?
if len(sys.argv) == 3:
servername = sys.argv[1]
comport = sys.argv[2]
elif len(sys.argv) == 2:
servername = sys.argv[1]
elif len(sys.argv) >3:
print("Usage: " + sys.argv[0] + ' servername [comport]')
exit()
# open serial port
serialPort = None
try:
print("Opening comport " + comport)
serialPort = serial.Serial(port = comport, baudrate=4000000, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE) # 115200 921600 4000000
#serialPort.setblocking(False)
except:
print('ERR: Could not open port ' + comport)
print("Usage: " + sys.argv[0] + ' servername [comport]')
exit()
# make tcp connection
print("Connecting to " + servername + ":" + str(serverport) )
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((servername, serverport))
sock.setblocking(False)
# wait for data
try:
while 1:
readers, _, _ = select.select([sock], [], [], 0.1) # timeout 0.1s / serialPort is not supported on windows :-(
for reader in readers:
if reader is sock:
## copy data from remote radio to serial port
data_from_network = sock.recv(1024)
for line in data_from_network.split(b'\n'):
if len(line) > 0:
print("< " + line.decode() )
try:
serialPort.write(bytes.fromhex(line.decode()))
except:
e = sys.exc_info()[0]
print(e)
#elif reader is serialPort:
## copy data from serial port to remote radio
command_from_programmer = b''
command_from_programmer += serialPort.read()
while serialPort.in_waiting > 0:
command_from_programmer += serialPort.read()
if ( len (command_from_programmer) > 0 ):
# send data from programming software on serial port to server with connected radio as hex encoded line
to_network = (command_from_programmer.hex() + '\n').encode()
print("> " + to_network[:-1].decode() )
try:
sock.sendall(to_network)
except:
e = sys.exc_info()[0]
print(e)
finally:
print('QRT')
serialPort.close()
sock.close()