Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python : How to detect device name/id on a serial COM

I would like some indication on how to do this in python:

  • Identify the port named a specific name in the serial com (\Device\VCP0 and \Device\VCP1 these are get by browsing in regedit window)

  • And get the id of the device that is pluged

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

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

like image 384
yemi oke Avatar asked Oct 13 '11 15:10

yemi oke


2 Answers

I am not sure what 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)

Using this information, you can parse it and get the COM numbers. You can get other attributes of the Serial instances here: http://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx

like image 63
Ken Avatar answered Oct 06 '22 00:10

Ken


Two answer

1) Because this relies on the hardware available, it is perfectly possible that the test code worked in the environment it was written on, but doesn't work in your environment - may be quite likely if you are on Windows and this was written on Linux. The code uses port 0 - don't know how that maps to COM1 etc.

2) On Windows, COM ports used to have DOS names like COM1, COM2 - i.e. A string, not an int (they aren't like TCP/IP port numbers). More recently in Windows there is the \.\COMnotanumber format which allows a more generic name, I've seen these used by a USB to serial converter. Having had a quick look at the source code of pyserial SerialBase in serialutil.py, it's a bit odd IMO, because AFAICT self.name only gets set when you use an explicit port setting by calling self.port(portname). You might want to try intializing the serial port instance with serport = Serial(0) then explicitly calling serport.port('COM1') (or whatever your port name is instead of COM1).

Just corrected the code. its working fine... :)

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()
like image 22
Bavan Thomas Avatar answered Oct 05 '22 23:10

Bavan Thomas