Motivation – The Arduino IDE can only be used to monitor a single port. Arduino IDE does not allow multiple serial ports to be monitored simultaneously. Serial library can be used to read, write and print to different serial ports. Therefore, different ports can be monitored simultaneously from the terminal. This will be helpful when multiple microcontrollers are connected to the same laptop

Run the following command to install the serial library –

pip install pyserial 

Code –

Copy the code and store it in a file named ‘serial_monitor.py’

import serial 
import argparse

def print_serial(name):
    try:
        serial_port = serial.Serial(name,115200)
        print(f"The Port name is {serial_port.name}")
        while True:
            lines = serial_port.readline()
            print(lines)
    except:
        print("ERROR")
        print("check port")
        exit() 

ap = argparse.ArgumentParser()
ap.add_argument("-p","--port",required = True, help = "Enter Port Name")
args = vars(ap.parse_args())

PORT = args['port']

print_serial(PORT) 

Usage – Run the following command from the terminal.

python serial_monitor.py -p [PORT NAME] 

Next Post Previous Post