0

Using 2020-12-02-raspios-buster-armhf-lite I have a GPS that outputs TTL connected to the UART pins (pins 4, 6, 8, 10) with serial login disabled. After first booting up I can see output from the GPS if I cat /dev/serial0 (output masked for privacy).

$ cat /dev/serial0 | tr [0-9] X
$GPGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,XX*XX

$GPGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX*XX

$GLGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,,XX,XX,XXX,XX,XX,XX,XXX,*XE

$GLGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,,XX,XX,XXX,*XX

$GNGLL,XXXX.XXXXX,N,XXXXX.XXXXX,W,XXXXXX.XX,A,A*XX

^C

I can also run a python program to receive output.

$ cat gps.py
#!/usr/bin/python3
import serial
import re

sp = serial.Serial('/dev/serial0', 9600, timeout=1.0)
sp.flushInput()
while True:
    try:
        data = sp.read_until()
        print(re.sub('[0-9]', 'X', data.decode('ascii')))
    except serial.SerialTimeoutException:
        print('Timeout.')
        continue
    except KeyboardInterrupt:
        print('Interrupt.')
        break;
print('Exiting...')
sp.close()

$ ./gps.py 
$GPGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,,XX,XX,XXX,XX*XC

$GPGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,XX*XD

$GPGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX*XX

$GLGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,,XX,XX,XXX,XX,XX,XX,XXX,XX*XX

$GLGSV,X,X,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,XX,XX,XX,XXX,*XX

^CInterrupt.
Exiting...

However, after running the python program I can no longer see output from cat /dev/serial0. The command returns immediately and no output is produced. I can rerun the python program and see output, but can no longer see anything from cat /dev/serial0 until I reboot. Can someone tell me why that is?

1
0

My guess is close() function is hanging for some reason, so you are not able to read via cat because the port is not closed properly. Try adding the followings to make sure that is not happening:

import time

sp.reset_input_buffer()
time.sleep(0.3)
sp.reset_output_buffer()
time.sleep(0.3)
sp.close()

So what you are doing is basically flushing buffers, which is mostly recommended for these kind of uses. You might also want to check Pyserial API documentation for further information.

0

So I found out if I reset the terminal line settings after I run the python program I can then cat from /dev/serial0 again. So the sequence is:

$ cat /dev/serial0
$ ./gps.py 
$ stty -F /dev/serial0 sane
$ cat /dev/serial0

Not sure how to get pySerial to reset things for me before I exit the python program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.