最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

testing - Minicom Not Exiting Cleanly in Python Script, Blocking Access to Serial Port for Subsequent Operations - Stack Overflo

programmeradmin6浏览0评论

I am working on a Python-based testing script for an OBU board accessed via /dev/ttyUSB0. The script tests various peripherals like Bluetooth (/dev/ttyLP1) and GNSS (/dev/ttyLP3) using separate scripts (ble.py and gnss.py) orchestrated by a main script (main.py).

The issue arises after running ble.py, which uses Minicom to interact with the Bluetooth peripheral. Minicom does not exit cleanly, leaving /dev/ttyLP1 occupied. This prevents gnss.py from accessing /dev/ttyLP3, as the serial port is blocked.

Interestingly, running ble.py alone exits Minicom properly. However, when executed via main.py, Minicom remains active and blocks the port. I have tried sending control commands to exit Minicom programmatically but without success.

I am looking for a solution to ensure Minicom exits cleanly and releases the port after the Bluetooth test so that subsequent tests can run without issues.

  1. Manually Closing Minicom:

    • Attempted sending Ctrl-A followed by X to exit Minicom using serial.write(b'\x01x').

    • Tried using flush() and close() on the serial port to ensure Minicom is closed and the port is released.

  2. Running Scripts Sequentially:

    • Running ble.py followed by gnss.py through the main script (main.py), but Minicom does not exit cleanly in this case, blocking the GNSS port (/dev/ttyLP3).
  3. Using Subprocess for Minicom Control:

    • Attempted using subprocess.Popen to launch Minicom and kill the process, but faced issues with process management and port release.
    1. After running ble.py for Bluetooth testing, Minicom should exit cleanly and release the /dev/ttyLP1 port.

    2. Following the Bluetooth test, gnss.py should be able to run without any issues, with the GNSS port (/dev/ttyLP3) accessible.

    3. The scripts should run sequentially without manual intervention, ensuring that all peripherals (Bluetooth, GNSS) are tested one after another without port conflicts.

here is the ble code

import time
import serial
import sys
from utils import write_to_log  # Import the utility function for logging

def bluetooth_test(obu_number):
    """Perform Bluetooth testing."""
    try:
        # Configuration for PC-to-OBU connection
        obu_serial_port = '/dev/ttyUSB0'  # PC connection to OBU
        obu_baud_rate = 115200

        with serial.Serial(obu_serial_port, obu_baud_rate, timeout=1) as pc_to_obu:
            log_message = f"Bluetooth module testing started for OBU {obu_number}."
            #print(log_message)
            #write_to_log(log_message, obu_number)  # Log the start of testing

            # Open Minicom on the OBU
            pc_to_obu.write(b'minicom -D /dev/ttyLP1 -b 115200\n')  # Start Minicom
            time.sleep(2)  # Give Minicom time to initialize

            # Send AT+CGMI command to check manufacturer
            pc_to_obu.write(b'AT+CGMI\r')
            time.sleep(1)
            response = pc_to_obu.read_all().decode(errors="ignore")
            manufacturer = "u-blox" if "u-blox" in response else None

            # Send AT+CGMM command to check model
            pc_to_obu.write(b'AT+CGMM\r')
            time.sleep(1)
            response = pc_to_obu.read_all().decode(errors="ignore")
            model = "NINA-B4" if "NINA-B4" in response else None

            # Check Bluetooth module status
            # if manufacturer == "u-blox" and model == "NINA-B4":
            if manufacturer == "u-blox":
                success_message = f"Bluetooth - Yes."
                print(success_message)
                write_to_log(success_message, obu_number)
            else:
                failure_message = f"Bluetooth peripheral test failed."
                print(failure_message)
                write_to_log(failure_message, obu_number)

            # Exit Minicom
            pc_to_obu.write(b'\x01')  # Send Ctrl-A
            time.sleep(0.5)  # Wait for a moment
            pc_to_obu.write(b'x')  # Send 'X' to exit
            time.sleep(1)  # Wait to ensure Minicom exits properly


    except serial.SerialException as e:
        error_message = f"Bluetooth testing failed for OBU {obu_number}: {e}"
        print(error_message)
        write_to_log(error_message, obu_number)

if __name__ == "__main__":
    # Get OBU number from command-line argument
    if len(sys.argv) < 2:
        print("Error: OBU number is required.")
        sys.exit(1)
    
    obu_number = sys.argv[1]  # Get OBU number passed from main.py
    bluetooth_test(obu_number)


与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论