Python to automatically select serial ports (for Arduino)(Python 自动选择串口(适用于 Arduino))
问题描述
目前,python 程序必须知道设备 (Arduino) 在哪个端口上,然后 Python 才能与设备通信.
Currently the python program must know which port a device (Arduino) is on before Python can communicate the device.
问题:每当设备被拔出和重新插入时,它的 COM 端口都会改变,因此必须再次将正确的串行端口提供给 Python 才能找到设备.
Problem: Whenever the device is plugged out and back in, its COM port changes, so the correct serial port must be given to Python again for it to find the device.
Python(使用pySerial)如何自动搜索要使用的正确串口?python是否可以正确地将串口上的设备识别为Arduino?
How can Python (using pySerial) automatically search for the correct serial port to use? Is it possible for python to correctly identify the device on a serial port as an Arduino?
推荐答案
使用以下代码查看所有可用的串口:
Use the following code to see all the available serial ports:
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
print p
这给了我以下信息:
('COM4', 'Arduino Due Programming Port (COM4)', 'USB VID:PID=2341:003D SNR=75330303035351300230')
('COM11', 'RS-232 Port (COM11)', 'FTDIBUS\VID_0856+PID_AC27+BBOPYNPPA\0000')
要确定它是否是 Arduino,您可以执行以下操作:
To work out if it's an Arduino you could do something like:
if "Arduino" in p.description:
print "This is an Arduino!"
这篇关于Python 自动选择串口(适用于 Arduino)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 自动选择串口(适用于 Arduino)
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
