Blame view

robots/little_john/telemetry/code/monitor/serialselect.py 2.11 KB
cfa61172   Christopher Stone   Moved serial port...
1
2
3
4
5
# Module to choose and open a serial port
# Written as a telemetry tool by:
# The UoN Robot Wars Project, 2018

def selectserial():
a621482e   Christopher Stone   Minor cleaning up...
6
    """Cross-platform function to find appropriate serial ports, query the user if necessary, and open one of them"""
cfa61172   Christopher Stone   Moved serial port...
7
8
9
    import platform
    import serial
    import os
c0c9a4b1   Christopher Stone   Migrated serial p...
10
    import easygui
ef651028   Christopher Stone   Moved serialselec...
11
    import logging
cfa61172   Christopher Stone   Moved serial port...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    
    devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm']
    targetdevs = []
    if platform.system()=='Windows':
        com_ports = ['COM%s' % (i + 1) for i in range(256)]
        for port in com_ports:
            try:
                s = serial.Serial(port)
                s.close()
                targetdevs.append(port)
            except (OSError, serial.SerialException):
                pass
        os='Windows'    #may be useful
    else:
        alldevs = os.listdir("/dev/")
        targetdevs = []
        for dev in alldevs:
            for pattern in devpatterns:
                if pattern in dev:
                    targetdevs.append("/dev/" + dev)
        os='Other'     #may be useful

    if len(targetdevs) == 0:
376deebd   Christopher Stone   Began to actually...
35
        return "Sorry, no serial devices found."
cfa61172   Christopher Stone   Moved serial port...
36
    elif len(targetdevs) > 1:
ef651028   Christopher Stone   Moved serialselec...
37
        logging.info("Found multiple serial devices: ")
cfa61172   Christopher Stone   Moved serial port...
38
        for i, dev in enumerate(targetdevs):
ef651028   Christopher Stone   Moved serialselec...
39
            logging.info("  " + str(i) + ": " + dev)
c0c9a4b1   Christopher Stone   Migrated serial p...
40
41
42
        message = "Please choose a serial port to recieve data through:"
        title = "Found multiple serial ports!"
        serialport = easygui.choicebox(message, title, targetdevs)
af437b8f   Christopher Stone   Improved handling...
43
        if serialport == None:
fef2ac1d   Christopher Stone   Simplified error ...
44
45
            logging.info("User cancelled selection dialogue")
            return None
cfa61172   Christopher Stone   Moved serial port...
46
    else:
ef651028   Christopher Stone   Moved serialselec...
47
        logging.info("Only found one likely serial device: " + targetdevs[0])
c0c9a4b1   Christopher Stone   Migrated serial p...
48
49
        serialport = targetdevs[0]
        
cfa61172   Christopher Stone   Moved serial port...
50
51
52
53
54
55
56
57
58
59
    try:
        datafeed = serial.Serial(
        port=serialport,
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
        )

ef651028   Christopher Stone   Moved serialselec...
60
        logging.info("Sucessfully opened " + serialport + " as data source!")
cfa61172   Christopher Stone   Moved serial port...
61
62
        return datafeed
    except Exception as e:
fef2ac1d   Christopher Stone   Simplified error ...
63
64
        logging.critical(str(e))
        return None