Python to automatically select serial ports (for Arduino)

前端 未结 4 1596
心在旅途
心在旅途 2020-11-29 23:10

Currently the python program must know which port a device (Arduino) is on before Python can communicate the device.

Problem: Whenever the device is

4条回答
  •  囚心锁ツ
    2020-11-30 00:02

    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
    

    This gives me the following:

    ('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')
    

    To work out if it's an Arduino you could do something like:

        if "Arduino" in p.description:
            print "This is an Arduino!"
    

提交回复
热议问题