Skip to content

Commit 4d60cf5

Browse files
committed
Added control port and port autodetection.
1 parent d1d6506 commit 4d60cf5

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

mux.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from io import BlockingIOError
22
from os import openpty, read, set_blocking, ttyname, write
33
from serial import serial_for_url
4-
from sys import stderr
4+
from sys import stderr, stdout
55
from time import sleep, time
66
from tty import setcbreak
77

88

9+
_commands = {
10+
'get_ports': 0,
11+
'enable': 1,
12+
'disable': 2}
13+
914

1015
class Mux():
1116
""""""
@@ -68,14 +73,29 @@ def update(self):
6873
self._write(data)
6974

7075

71-
def serialmux(name, number):
76+
def _control(connection, cmd):
77+
connection.write(bytes([0, _commands[cmd]]))
78+
79+
if ord(connection.read()):
80+
raise IOError('Invalid control response.')
81+
82+
return ord(connection.read())
83+
84+
85+
def serialmux(handle, log_handle, name):
7286
""""""
7387
connection = serial_for_url(name)
88+
sleep(2)
89+
90+
number_of_ports = _control(connection, 'get_ports')
91+
handle.write('Virtual ports detected: {}\n'.format(number_of_ports))
7492

7593
muxs = []
76-
for i in range(1, number + 1):
77-
muxs.append(Mux(connection, i, stderr))
78-
print('mux {} is on {}'.format(muxs[-1].id, muxs[-1].name))
94+
for i in range(1, number_of_ports + 1):
95+
muxs.append(Mux(connection, i, log_handle))
96+
handle.write(' Mux{}: {}\n'.format(muxs[-1].id, muxs[-1].name))
97+
98+
_control(connection, 'enable')
7999

80100
while True:
81101
for mux in muxs:
@@ -85,7 +105,7 @@ def serialmux(name, number):
85105

86106
def main():
87107
"""Main entry point."""
88-
serialmux('/dev/ttyUSB0', 2)
108+
serialmux(stdout, None, '/dev/ttyUSB0')
89109

90110

91111
if __name__ == '__main__':

ping.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#from serial import Serial
21
from time import sleep
32

43
from simple_rpc import Interface
54

65

7-
interface = Interface('/dev/pts/20', wait=2)
6+
interface = Interface('/dev/pts/14', wait=2)
87

98
while True:
109
for i in range(256):

0 commit comments

Comments
 (0)