0
votes

I'm working on a FTDI chip and want to connect this chip to the operating system Windows 10 via a serial port. I'm using this code below and the results show me the all visible ports what I don't want. what I need is to detect only the port which the chip is connected to and ignore the rest. for example Com 4, so I just want my program written in Python to detect Com4 ONLY. I'm using Pyserial by the way. I'm pretty thankful and grateful for your help in advance

def serial_ports():

if sys.platform.startswith('win'):
    ports = ['COM%s' % (i + 1) for i in range(256)]
    print ports
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
    # this excludes your current terminal "/dev/tty"
    ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
    ports = glob.glob('/dev/tty.*')
else:
    raise EnvironmentError('Unsupported platform')

result = []
print ports
for port in ports:
    try:
        s = serial.Serial(port)
        s.close()
        result.append(port)
    except (OSError, serial.SerialException):
        pass
return result 
1

1 Answers

0
votes

Read about

serial.tools.list_ports

This module can be executed to get a list of ports (python -m serial.tools.list_ports).
It also contains the following functions.

serial.tools.list_ports.comports()
Returns:    an iterable that yields ListPortInfo objects.

The function returns an iterable that yields tuples of three strings:
port name as it can be passed to serial.Serial or serial.serial_for_url()
description in human readable form
sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.