-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ports.py
55 lines (48 loc) · 1.47 KB
/
get_ports.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
#!/usr/bin/python3
#fixed somehow :)
import glob
from sys import argv, version, exit
def manual():
#default_ports should contain all ports available without any external devices
input("Unplug all virtual port(s) and press enter...")
defaultPorts = all_ports()
input("Connect virtual port(s) and press enter...")
allPorts = all_ports()
unique = []
for item in allPorts:
if not (item in defaultPorts):
unique.append(item)
return unique
def all_ports():
return glob.glob("/dev/tty[A-Za-z]*")
def by_name(name):
print("Trying with:", name)
specified = [x for x in all_ports() if (name in x)]
return specified
def help():
print("Usage:")
print(" -a, --all -> print all ports")
print(" -m, --manual -> need to unplug specified device and plug after all")
print(" -n [string], --name [string] -> with some name e.g. USB")
print(" -o, --auto -> USB name on default")
def main(argv):
if (version[0] == "2"):
print("Please run python3...")
exit()
ports = []
try:
if argv[0] in ("-a", "--all"):
ports = all_ports()
elif argv[0] in ("-m", "--manual"):
ports = manual()
elif argv[0] in ("-o, --auto"):
ports = by_name("USB")
elif argv[0] in ("-n", "--name"):
ports = by_name(argv[1])
except:
help()
exit()
return ports
if __name__=="__main__":
ports = main(argv[1:])
print("Ports: ", ports)