def toggle_serial_monitor(window):
    monitor_module = pyarduino.base.serial_monitor
    serial_monitor = None

    settings = st_base.get_settings()
    serial_port = settings.get('serial_port', '')
    serial_ports = pyarduino.base.serial_port.list_serial_ports()
    if serial_port in serial_ports:
        if serial_port in monitor_module.serials_in_use:
            serial_monitor = monitor_module.serial_monitor_dict.get(
                serial_port, None)
        if not serial_monitor:
            monitor_view = st_console.MonitorView(window, serial_port)
            serial_monitor = pyarduino.base.serial_monitor.SerialMonitor(
                serial_port, monitor_view)

        if not serial_monitor.is_running():
            serial_monitor.start()
            if not serial_port in monitor_module.serials_in_use:
                monitor_module.serials_in_use.append(serial_port)
            monitor_module.serial_monitor_dict[serial_port] = serial_monitor
        else:
            serial_monitor.stop()
            monitor_module.serials_in_use.remove(serial_port)
dataset/ETHPy150Open Robot-Will/Stino/stino/main.py/toggle_serial_monitor/original