This is a tool to remember and detect communication ports using hardware identifiers.
Lets assume you've plugged in a USB device and you know it is currently at port "COM3". We will use COMtacts to remember this hardware so next time you plug it in, we can find it even if its not at "COM3".
comtacts add --name "My USB Device" --port "COM3"To overwrite an existing contact:
comtacts add --name "My USB Device" --port "COM3" --overwriteor if you're in python already:
from comtacts import add_port
add_port(name="My USB Device", port="COM3", overwrite=False) # or overwrite=True, up to youcomtacts get --name "My USB Device"or if you're in python already:
from comtacts import get_port
get_port(name="My USB Device")Typical usage would be to let your Python script automatically find the port of a device.
from serial import Serial
from comtacts import get_port
ser = Serial(
port = get_port(name="My USB Device"),
baudrate = 9600,
timeout = 1
)comtacts allor if you're in python already:
from comtacts import all_contacts
all_contacts()comtacts whereUnder the hood, COMtacts uses serial.tools.list_ports.comports() to find serial ports. list_ports.comports() returns a list of serial.tools.list_ports.ListPortInfo objects, which have attributes that can be used to identify a device. If you know the values of these attributes for your device, you can use them to find the port. See the documentation on these functions to see which attributes are available.
For example, if you know the manufacturer, vid, and pid of your device, you can use the manufacturer, vid, and pid attributes to find the port:
from comtacts import get_port_with_attributes
get_port_with_attributes(manufacturer="My Manufacturer", vid="0403", pid="6001")By default, COMtacts saves contacts inside its package directory. This was done such that contacts are specific to a python environment -- most of my use cases are specific to a certain application. If you would like to make contacts available to all python environments, you can set the COMTACTS_DIR environment variable to the directory where you would like to save contacts.
