[python] Test if console port is working

This is a simple script to test if the com port is working or not.

from serial import Serial
from serial.serialutil import SerialException


try:
    with Serial(port='COM4', baudrate=9600, parity='N', stopbits=1, bytesize=8, timeout=8) as console:
        if console.isOpen():
            print("{} is working".format(console.port))
except SerialException:
    print("The com port could be in used or not exists.")

The SerialException will invoke PermissionError if the current com port you try to open with this script is in used.
If the SerialException invokes FileNotFoundError then it is highly the com port you requested with the script is not installed, check your device manager to make sure the com port number is correct.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s