Python自动选择串行端口(适用于Arduino) [英] Python to automatically select serial ports (for Arduino)

查看:161
本文介绍了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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆