Python: determining device name / identifier on serial COM

I would like to know how to do this in python:

  • Define the port with the name of a specific name in the serial interface (\ Device \ VCP0 and \ Device \ VCP1, which they receive by looking in the regedit window)

  • And get the identifier of the connected device

I can already identify avalable COM with this pySerial code that scans the active serial COM port

import serial

def scan():
    """scan for available ports. return a list of tuples (num, name)"""
    available = []
    for i in range(256):
        try:
            s = serial.Serial(i)
            available.append( (i, s.portstr))
            s.close()   # explicit close 'cause of delayed GC in java
        except serial.SerialException:
            pass
    return available

if __name__=='__main__':
    print "Found ports:"
    for n,s in scan():
        print "(%d) %s" % (n,s)

Thanks in advance

+5
source share
2 answers

I'm not sure which operating system you are using, but this is in Win7-x64

import win32com.client
wmi = win32com.client.GetObject("winmgmts:")
for serial in wmi.InstancesOf("Win32_SerialPort"):
       print (serial.Name, serial.Description)

, COM. Serial: http://msdn.microsoft.com/en-us/library/aa394413 (v = vs .85).aspx

+4

1) , , , , - , Windows Linux. 0 - , COM1 ..

2). Windows COM- DOS, COM1, COM2 - , int ( TCP/IP). Windows \.\COMnotanumber, , , USB- . pyserial SerialBase serialutil.py, IMO, AFAICT self.name , , self.port( ). , serport = Serial (0), serport.port('COM1') ( COM1).

. ...:)

import serial

def scan():
    available = []
    for i in range(256):
        try:
            s = serial.Serial('COM'+str(i))
            available.append( (s.portstr))
            s.close()   # explicit close 'cause of delayed GC in java
        except serial.SerialException:
            pass

    for s in available:
        print "%s" % (s)


if __name__=='__main__':
    print "Found ports:"
    scan()
0

All Articles