Click here to Skip to main content
15,742,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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():
        from 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)
                report += "<br />Port is already open."
                report += "<br />Port is not open at the moment."
                report += "<br />Successfully opened port."
            except SerialException as ex:
                report += "<br />Couldn't open port ("+ str(ex) +")."
            output = bytearray([0x1b, 0x02, 0x05, 1,2,3,4, 0xAA, 0x55])

        return report
        return "No comports."

How do I open ports properly?

What I have tried:

Added the .isOpen code prior to the try/open()/except part.
Updated 10-Dec-20 4:26am

1 solution

According to Short introduction — pySerial 3.4 documentation[^] the call to Serial opens the port. So after that call you would expect isOpen to return True.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900