Pyserial in Python(List out available comports without for loop)
You can iterate through the list and get the names:
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
print([port.name for port in ports])
How can I get a list of available serial ports using 'serial.tools.list_ports' python module?
According to the documentation you've linked,
The function returns a list of ListPortInfo objects.
They have several attributes which you can use, for example device
:
Full device name/path, e.g.
/dev/ttyUSB0
In order to emulate the command python3 -m serial.tools.list_ports
, you could do:
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
for p in ports:
print(p.device)
print(len(ports), 'ports found')
Which is a simplified version of what it actually does.
Listing serial (COM) ports on Windows?
Several options are available:
Call QueryDosDevice with a NULL lpDeviceName to list all DOS devices. Then use CreateFile and GetCommConfig with each device name in turn to figure out whether it's a serial port.
Call SetupDiGetClassDevs with a ClassGuid of GUID_DEVINTERFACE_COMPORT.
WMI is also available to C/C++ programs.
There's some conversation on the win32 newsgroup and a CodeProject, er, project.
Use pySerial to list open serial ports *from python script*, not from the command line
I don't know if I can explain it, but sometimes Python is fussy about dotted imports. Also to get an iterable within a script, you can use comports()
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial.tools.list_ports
>>> for port in serial.tools.list_ports.comports():
... print(f'Current port: {port}')
...
Current port: COM1 - Communications Port (COM1)
>>>
The serial.tools.list_ports module is documented here.
Need help listing the devices on COMM Ports in Python
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 )
pySerial running command to list ports
You need to import it first:
from serial.tools import list_ports
list_ports.main() # Same result as python -m serial.tools.list_ports
You can check out the source here
Related Topics
Error: 'Int' Object Is Not Subscriptable - Python
Dll Load Failed When Importing Pyqt5
Running a Process in Pythonw with Popen Without a Console
Force Numpy Ndarray to Take Ownership of Its Memory in Cython
Board-Drawing Code to Move an Oval
Pytz Localize VS Datetime Replace
Accessing Every 1St Element of Pandas Dataframe Column Containing Lists
Python Regular Expressions - How to Capture Multiple Groups from a Wildcard Expression
Count Consecutive Occurences of Values Varying in Length in a Numpy Array
Pyqt4 Wait in Thread for User Input from Gui
Syntaxerror: Multiple Statements Found While Compiling a Single Statement
Python Numpy Arange Unexpected Results
Generate a Random Derangement of a List
Why Not Generate the Secret Key Every Time Flask Starts
Play an Animated Gif in Python with Tkinter
Is' Operator Behaves Differently When Comparing Strings with Spaces