I have a basic bit of code I am writing, where I list available serial ports, then later on assign the desired port depending on a desired port name in the description (ie 'Serial Device) and receive information from it, etc.
The problem I'm finding is that the connected device could be anything from 'COM4' to 'COM99', and there is no way of me knowing this in advance.
Below is the relevant part of the code that I have written.
ports = list(serial.tools.list_ports.comports())
for p in ports:
print(p.description)
if 'Serial Device' in p.description:
find_com = re.findall(r'COM[0-9][0-9]',p.description)
However, I can't work out how to get my code to work depending on if COM ends in a single or double digits. I have found that when using two [sets]
find_com = re.findall(r'COM[0-9][0-9]',p.description)
any COM with a single digit is missed. But when using a single [set]
find_com = re.findall(r'COM[0-99]',p.description)
# OR
find_com = re.findall(r'COM[00-99]',p.description)
this misses any COM with a double digit, (ie COM12)
I feel like I'm close to being able to connect to the correct COM, no matter if it ends with a single or double digit, but I'm missing something?
r'COM\d{1,2}'
would match COM
, then exactly one or two d
igits.