Python list usb ports I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's, but I would like something less.NET specific - I want to get the list of ports in a Python or a C program, without.NET. I currently know of two other approaches:. Reading the information in the HARDWARE DEVICEMAP SERIALCOMM registry key. This looks like a great option, but is it robust? I can't find a guarantee online or in MSDN that this registry cell indeed always holds the full list of available ports.
![Python Python](/uploads/1/2/5/4/125480905/632507496.png)
COM3 - Intel(R) Active Management Technology - SOL (COM3) COM6 - MyCDCDevice (COM6) COM1 - Communications Port (COM1) >>> Great! However, I want this script to automatically pick out MyCDCDevice from the bunch and connect to it.
Tryint to call CreateFile on COMN with N a number from 1 to something. This isn't good enough, because some COM ports aren't named COMN. For example, some virtual COM ports created are named CSNA0, CSNB0, and so on, so I wouldn't rely on this method.
Any other methods/ideas/experience to share? Edit: by the way, here's a simple Python implementation of reading the port names from registry: import winreg as winreg import itertools def enumerateserialports: ' Uses the Win32 registry to return a iterator of serial (COM) ports existing on this computer. ' path = 'HARDWARE DEVICEMAP SERIALCOMM' try: key = winreg.OpenKey(winreg.HKEYLOCALMACHINE, path) except WindowsError: raise IterationError for i in itertools.count: try: val = winreg.EnumValue(key, i) yield (str(val1), str(val0)) except EnvironmentError: break. I think WMI is the way to go since it's pretty easy to get going and it has minimal code. It saves you from having to dig inside the registry and gives you some guarantee that it will work for more general situations in the future.
You can install everything needed with pip install pypiwin32 WMI and it works out-of-the-box. Code import wmi query = 'SELECT. FROM Win32PnPEntity WHERE Name LIKE '%(COM%)' coms = wmi.WMI.query(query) for com in coms: print(com.Name) Output Communications Port (COM1) mbed Serial Port (COM3) mbed Serial Port (COM5) My guess is that your serial port is some sort of Plug 'n Play so this should work fine. For some reason Win32SerialPort doesn't work for all ports.
I would like to read from the USB serial port from time to time in a loop. I can open the port and read data with: import serial ser = serial.Serial('/dev/ttyACM0', 9600) while 1: ser.readline An Arduino Uno is connected to the USB port of the RPi. The Arduino acts as a sensor and it will constantly produce readings. I need some help in adding timing features to the above code.
I want to open the port and read from it for a certain period of time. After that period of time, the port closes and the received data will be analysed. After a pause of several minutes, the port will reopen and the RPi will read data from it again.
This continues in a loop. Any help is much appreciated. All you would need to add, aside from closing the port when you're done;), is import time and then use: import serial, time ser = serial.Serial('/dev/ttyACM0', 9600) while 1: serialline = ser.readline print(serialline) # If using Python 2.x use: print serialline # Do some other work on the data time.sleep(300) # sleep 5 minutes # Loop restarts once the sleep is finished ser.close # Only executes once the loop exits I don't know if pySerial is buffered (data sent while sleeping is stored or simply dropped), but I usually prefer to use a generator, if you don't explicitly need to wait.
They seem a bit more flexible (in my opinion): def serialdata(port, baudrate) ser = serial.Serial(port, baudrate) while True: yield ser.readline ser.close for line in serialdata('/dev/ttyACM0', 9600):.transform data. You might also be able to use the with syntax instead of the while, but I'm not too sure how that'd work with pySerial. I was just editing my comment to clarify, but locked after 5 minutes:/ The ser.close never runs until the while loop is done (which doesn't really ever occur since it's while 1/ while true), so the serial port isn't actually closed between readline calls. Since simply having a pySerial port open isn't blocking, that shouldn't be an issue. If the while loop only ran x number of times, and then you wanted to work on that the port again, just leave the port open until done (move ser.close after all code that interacts with the port).
– Apr 30 '14 at 19:01.