[Python] How to resolve 'Broken pipe' error when accessing GPS data via Python while Minicom...

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to access GPS data from my GPS module using Python, but I encounter a Broken pipe error. However, when I use Minicom to access the same GPS module, it works fine and I can see the GPS location

    My setup:

    1. Device: Raspberry Pi
    2. GPS Module: Connected via USB (/dev/ttyUSB2)
    3. Baud rate: 115200 (same as in Minicom)
    4. OS: Raspberry Pi OS
    5. Minicom Configuration: Works fine and I can see the GPS data

    I am using the pyserial library to access the GPS data in my Python script.

    This is my code:

    import serial

    # Open the serial connection to the GPS
    ser = serial.Serial('/dev/ttyUSB2', 115200, timeout=1)

    while True:
    line = ser.readline().decode('utf-8').strip() # Read a line from GPS
    if line.startswith("+CGNSINF"):
    # Print the whole line or process it
    print(line)



    and this is the error

    Traceback (most recent call last):
    File "/home/jana/gps.py", line 4, in <module>
    ser = serial.Serial('/dev/ttyUSB2', 115200, timeout=1)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/jana/venv/lib/python3.11/site-packages/serial/serialutil.py", line 244, in __init__
    self.open()
    File "/home/jana/venv/lib/python3.11/site-packages/serial/serialposix.py", line 336, in open
    self._update_dtr_state()
    File "/home/jana/venv/lib/python3.11/site-packages/serial/serialposix.py", line 713, in _update_dtr_state
    fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
    BrokenPipeError: [Errno 32] Broken pipe

    Continue reading...

Compartilhe esta Página