我需要列出或枚举现有的串口,直到现在我使用这个方法enumerate_serial_ports(),但它不能与Windows 7一起工作.你知道一些替代方法如何在Windows 7下找到可用的串口?
def enumerate_serial_ports():
""" Uses the Win32 registry to return an
iterator of serial (COM) ports
existing on this computer.
"""
path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)
except WindowsError:
raise IterationError
for i in itertools.count():
try:
val = winreg.EnumValue(key, i)
yield str(val[1])
except EnvironmentError:
break
Run Code Online (Sandbox Code Playgroud)
我得到了IterationError

mga*_*lgs 16
现在有一个内置于pyserial 的list_ports模块.
In [26]: from serial.tools import list_ports
In [27]: list_ports.comports()
Out[27]:
[('/dev/ttyS3', 'ttyS3', 'n/a'),
('/dev/ttyS2', 'ttyS2', 'n/a'),
('/dev/ttyS1', 'ttyS1', 'n/a'),
('/dev/ttyS0', 'ttyS0', 'n/a'),
('/dev/ttyUSB0',
'Linux Foundation 1.1 root hub ',
'USB VID:PID=0403:6001 SNR=A1017L9P')]
Run Code Online (Sandbox Code Playgroud)
该模块也可以直接执行:
$ python -m serial.tools.list_ports
/dev/ttyS0
/dev/ttyS1
/dev/ttyS2
/dev/ttyS3
/dev/ttyUSB0
5 ports found
Run Code Online (Sandbox Code Playgroud)
您正在引发IterationError,但该异常实际上并不存在。也许你EnvironmentError也应该尝试针对这种情况加注。
pySerial 文档包含一些用于查找串行端口的示例代码。查看它们:http ://pyserial.sourceforge.net/examples.html#finding-serial-ports
| 归档时间: |
|
| 查看次数: |
13155 次 |
| 最近记录: |