Nesty
Nesty

Reputation: 11

Python Pyserial auto detect com ports

I'm trying to connect to my old switch D-link 1210 to serial port (console) by python3. First script must find COM-port:

import serial
import serial.tools.list_ports

print ('Search ports...')
ports = list(serial.tools.list_ports.comports())

for p in ports:
    print ('-- Find ports --')
    print (p)

Then the script needs to connect to the found port automatically, open port, and send commands.

I don't understand how do this :( Can you help me? PS sorry for my english

Upvotes: 0

Views: 8722

Answers (1)

Nesty
Nesty

Reputation: 11

My solution for Python 3.6

import os
import sys
import time
import serial
import serial.tools.list_ports

print('Search...')
ports = serial.tools.list_ports.comports(include_links=False)
for port in ports :
    print('Find port '+ port.device)

ser = serial.Serial(port.device)
if ser.isOpen():
    ser.close()

ser = serial.Serial(port.device, 9600, timeout=1)
ser.flushInput()
ser.flushOutput()
print('Connect ' + ser.name)

Upvotes: 1

Related Questions