0

I would like to automatically read all of the devices connected to the computer, so that I can communicate with all of the Keyspans, and none of the other devices. I am using Python, with the PySerial module.

The closest I have gotten is through CMD:

python -m serial.tools.list_ports -v

COM1
    desc: Communications Port (COM1)
    hwid: ACPI\PNP0501\2
COM3
    desc: Keyspan USB Serial Port (COM3)
    hwid: KEYSPAN\*USA19HMAP\00_00
COM4
    desc: Keyspan USB Serial Port (COM4)
    hwid: KEYSPAN\*USA19HMAP\01_00
3 ports found


I would really like to get this information using a python script instead, something like


print(serial.tools.comports)

but all that returns is

[<serial.tools.list_ports_common.ListPortInfo object at 0x00000000044022C8>, 
<serial.tools.list_ports_common.ListPortInfo object at 0x00000000043F8288>, 
<serial.tools.list_ports_common.ListPortInfo object at 0x00000000043F8388>]

and I am not exactly sure what that means. If you have any suggestions I would love to hear them! EDIT: Thanks for the help!

Liam Plybon
  • 58
  • 10
  • 1
    Before asking this question, did you try reading the documentation for PySerial such as https://pyserial.readthedocs.io/en/latest/tools.html. If not, why don’t you read it now? – DisappointedByUnaccountableMod Nov 20 '19 at 19:39
  • I have read through the documentation quite a bit, but I think the problem is my own understanding of how to implement it. I am on a windows computer so the function works for my system, and it outputs a list of objects as shown here. A better way to ask this question might have been, "How do I read the relevant data from this list of objects?" – Liam Plybon Nov 20 '19 at 20:11
  • 1
    your result shows that `serial.tools.comports` is a list so you can use `for`-loop to display every element separatelly - and then you should use other methods in every item to get more information. Check `print( dir(serial.tools.comports[0]) )` to see methods avaliable for item on list. – furas Nov 20 '19 at 20:32
  • Whoa. That is super cool. I will play around with this for a bit, but I was able to get it to tell me the comm port with `x = serial.tools.listports.comports print(x[0].device)`. Thanks for showing me dir, that is an incredible function to know. – Liam Plybon Nov 20 '19 at 20:56
  • I got it! using `print(x[1].description)` output `Keyspan USB Serial Port (COM3)`. Should I post it as the answer? – Liam Plybon Nov 20 '19 at 20:59

1 Answers1

1

Your result shows that serial.tools.comports is a list so you can use for-loop to format it or run some functions with every element separatelly.

for item in serial.tools.comports:
    print( item )

Using dir(item) you can see all methods and properties avaliable in this object.

Using help(item) you should see documentation which is inside source code.

If you will know methods then you can use them

for item in serial.tools.comports:
    print("Device:", item.device)
    print("Description:", item.desctiption)

EDIT: base on documentation for ListPortInfo you should have

for item in serial.tools.comports:
    print( item.name )
    print( item.description )
    print( item.hwid )
    print( item.vid )
    print( item.pid )
    print( item.serial_number )
    print( item.location )
    print( item.manufacturer )
    print( item.product )
    print( item.interface )
furas
  • 134,197
  • 12
  • 106
  • 148