How can I enumerate (list) serial ports?


J

Jason

Hi,

I'm developing an application to talk to a device over a serial port.
I'm trying my hardest to keep it cross platform, so I was using
pySerial for the serial communications. But I can't see a pythonic,
cross-platform way to enumerate serial ports on a machine.

Ideally I could show the user a list of "COM1","COM2", etc, or ttyS0,
ttyS1, etc. At the moment I think they'll just have to type it into a
box.

Is there a known way to do this? Is there anyone who has a hack
they're willing to share?

Thanks,
Jason
 
Ad

Advertisements

T

Tim Golden

Jason said:
Hi,

I'm developing an application to talk to a device over a serial port.
I'm trying my hardest to keep it cross platform, so I was using
pySerial for the serial communications. But I can't see a pythonic,
cross-platform way to enumerate serial ports on a machine.

As with many other such issues, instant cross-platformability
is likely to be rare. I admin I haven't looked at pySerial
but I imagine that, behind the scenes, it's doing some sort
of conditional imports according to platform. I imagine you'd
have to do something similar to get a list of port names.

On Windows, WMI can give you what you want. Look at the
Win32_SerialPort class. Hopefully, someone else can help
for other platforms.

TJG
 
Ad

Advertisements

J

Jason

but I imagine that, behind the scenes, it's doing some sort
of conditional imports according to platform.

I have, and yes indeed it does.
I imagine you'd
have to do something similar to get a list of port names.

That's what I thought, although I might just defer to the user knowing
what to type in (since they would have to identify the port allocated
to the device in the first place).
On Windows, WMI can give you what you want. Look at the
Win32_SerialPort class. Hopefully, someone else can help
for other platforms.

Thanks for this pointer :)

Cheers,
Jason
 

Top