forked from mrworf/serial-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.py
executable file
·118 lines (90 loc) · 2.82 KB
/
bridge.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
#!/usr/bin/env python3
import subprocess
import re
import serial
import select
import sys
import logging
def find_ports():
dmesg = subprocess.run(['dmesg'], stdout=subprocess.PIPE)
s = dmesg.stdout.decode('utf-8')
r = re.findall(r'tty[A-Z]{3}[0-9]', s)
r = list(set(r))
logging.debug('Found a total of %d serial ports', len(r))
for serial in r:
logging.info(f'Found {serial} port')
if len(r) < 2:
logging.error('Too few ports, cannot bridge')
sys.exit(255)
if len(r) > 3:
logging.error('Too many ports, do not know what to bridge')
sys.exit(255)
if len(r) == 2 and 'ttyAMA0' not in r:
logging.error('Native UART not found (looking for ttyAMA0)')
sys.exit(255)
if len(r) == 3: # Standard bridging
r.remove('ttyAMA0')
r.sort() # So we're consistent
return r
def to_hex(data):
return ' '.join('{:02x}'.format(x) for x in data)
def to_str(data):
return ''.join('%c' % (x if x > 31 and x < 127 else '.') for x in data)
def splitdata(data):
return [data[i:i + 8] for i in range(0, len(data), 8)]
def open_port(port):
p = serial.Serial(f'/dev/{port}', baudrate=19200)
p.reset_input_buffer()
p.reset_output_buffer()
return p
def transfer_data(a, b):
data = a.read(a.in_waiting)
b.write(data)
return data
if len(sys.argv) > 1 and sys.argv[1].lower() == '--debug':
level = logging.DEBUG
print('Debug logging enabled')
else:
level = logging.INFO
logging.basicConfig(format='%(levelname)s: %(message)s', level=level)
r = find_ports()
srcport = r[0]
dstport = r[1]
logging.info(f'Bridging between {srcport} and {dstport}')
src = open_port(srcport)
dst = open_port(dstport)
ports = [src, dst]
logging.debug(' %-33s | %-33s' % ('/dev/' + srcport, '/dev/' + dstport))
logging.debug('-' * 71)
while True:
readable, writeable, exceptional = select.select(ports, [], ports)
if len(exceptional):
logging.warning('Error detected on serial communication')
if src in exceptional:
logging.info(f'Reopening {srcport}')
try:
src.close()
except:
logging.exception('Unable to close src')
src = open_port(srcport)
if dst in exceptional:
logging.info(f'Reopening {dstport}')
try:
dst.close()
except:
logging.exception('Unable to close dst')
dst = open_port(dstport)
ports = [src, dst]
logging.info('Resuming bridging')
continue
srcdata = ''
dstdata = ''
if src in readable:
srcdata = splitdata(transfer_data(src, dst))
if dst in readable:
dstdata = splitdata(transfer_data(dst, src))
for row in range(0,max(len(srcdata), len(dstdata))):
adata = srcdata[row] if row < len(srcdata) else b''
bdata = dstdata[row] if row < len(dstdata) else b''
str = '%-23s ; %-8s | %-23s ; %-8s' % (to_hex(adata), to_str(adata), to_hex(bdata), to_str(bdata))
logging.debug(str)