Repository navigation
Expand file tree
/
Copy pathscan.py
More file actions
61 lines (54 loc) · 1.93 KB
/
Copy pathscan.py
File metadata and controls
61 lines (54 loc) · 1.93 KB
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
"""Discover serial ports and addressed motor devices."""
import serial as s
import serial.tools.list_ports as listports
from .errors import ExternalDeviceNotFound
from .motor import Motor
def find_ports():
"""Find usable serial ports with a discoverable serial device.
Returns
-------
list of str
Names of ports that report a serial number and can be opened.
"""
available_ports = []
for port in listports.comports():
if port.serial_number:
try:
connection = s.Serial(port.device)
connection.close()
available_ports.append(port)
except (OSError, s.SerialException):
print(f"{port.device} unavailable.\n")
port_names = [port.device for port in available_ports]
return port_names
def scan_for_devices(controller, start_address=0, stop_address=0, debug=True):
"""Scan a range of addresses for responsive motor devices.
Parameters
----------
controller : BaseSerialController
Controller used to communicate with each address.
start_address : int, default=0
First address to test, inclusive.
stop_address : int, default=0
Last address to test, inclusive.
debug : bool, default=True
Whether to enable debug output on discovered motors.
Returns
-------
list of dict
Device records containing ``info`` and ``controller`` entries.
"""
devices = []
for address in range(start_address, stop_address + 1):
try:
motor = Motor(controller, address=str(address), debug=debug)
print(f"{controller.port}, address {address}: ELL{motor.motor_type} \t(S/N: {motor.serial_no})")
device = {
"info": motor.info,
"controller": controller,
}
devices.append(device)
del motor
except ExternalDeviceNotFound:
pass
return devices