Hi forum,
this PC has one genuine serial port (COM1) and one virtual serial port in the form of a USB dongle. This is preparational work for an STM32 that creates another USB-VSP device that is still to come out of our hardware department. I need to write a python program that finds that very device. For now, I'm failing to open a serial port.
The list_ports part works, but both of the available ports tell me that they are open.
They are not. I tested that using HTerm.exe opening that port. I can send data out to the oscilloscope on pin3.
Even after a clean reboot, both ports report to be already open (which we know they're not).
from serial import Serial, SerialException
def dongle_discover():
try:
from serial.tools.list_ports import comports
except ImportError:
return "Ex: not importing comports."
if comports:
report = "These are the ports:"
com_ports_list = list(comports())
dongle_port = None
for port in com_ports_list:
report += "<hr />"
report += "Device: \"" + port.device + "\""
report += "<br />" + port[1]
report += "<br />" + port[2]
candidate = Serial(port=str(port.device), baudrate=115200, timeout=10)
if(candidate.isOpen):
report += "<br />Port is already open."
else:
report += "<br />Port is not open at the moment."
try:
candidate.open()
report += "<br />Successfully opened port."
except SerialException as ex:
report += "<br />Couldn't open port ("+ str(ex) +")."
continue
output = bytearray([0x1b, 0x02, 0x05, 1,2,3,4, 0xAA, 0x55])
candidate.write(output)
candidate.close()
return report
else:
return "No comports."
How do I open ports properly?
What I have tried:
Added the
.isOpen
code prior to the
try
/
open()
/
except
part.