I have a Raspberry Pi Zero trying to read GPS data.
With this command:
sudo cat /dev/ttyAMA0
I get the following correct data:
$GPGSV,1,1,0*49
$GPRMC,,V,,,,,,,,,,N*53
$GPVTG,,,,,,,,,N*30
$GPGGA,,,,,,0,00,99.99,,,,,,*48
$GPGLL,,,,,,V,N*64
$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
But in the Python script:
import serial
import time
import string
while True:
port="/dev/ttyAMA0"
ser=serial.Serial(port, baudrate=9600, timeout=0.5)
newdata=ser.readline().strip().decode('UTF-8')
print(newdata)
the data is corrupted:
GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
SV,1,1,0*49
RMC,,V,,,,,,,,,,N*53
PVTG,,,,,,,,,N*30
,,,,,,0,00,99.99,,,,,,*48
L,,,,,,V,N*64
I've spent two days trying to figure out what went wrong with no success. Any tip is much appreciated.
(RaspberryPi OS Lite didn't have serial
preinstalled, so I installed it like this:)
sudo apt-get install python-serial
You are recreating and reinitialising your serial port in every iteration through the loop. You should do that outside the loop as that is loop-invariant:
# Set up serial interface
port="/dev/ttyAMA0"
ser=serial.Serial(port, baudrate=9600, timeout=0.5)
while True:
# read next line
newdata = ser.readline().strip().decode('UTF-8')