NMEA定位测试,硬件验证
请在可视天空下可以定位,GPS 室内搜星不稳定,请将模块或者天线放到阳台或窗户旁,或者直接在户外进行实验
NEMA 定位测试
打开树莓派终端,输入以下指令进入配置界面
sudo raspi-config
*选择Interfacing Options -> Serial,关闭shell访问,打开硬件串口
然后重启树莓派:
sudo reboot
是能否,在config.txt文件 可以看到
dtparam=uart0=on
或
enable uart=1
运行以下程序,会扫描ttyAMA0 和 ttyS0,需要10秒
import serial
import time# Serial ports to try
PORTS = ['/dev/ttyAMA0', '/dev/ttyS0']
BAUD = 9600def try_ports():print("Trying to connect to a serial GPS device...")for port in PORTS:try:print(f"Trying port {port} at {BAUD} baud...")ser = serial.Serial(port, baudrate=BAUD, timeout=1)time.sleep(0.5) # Wait for GPS datafor _ in range(10):line = ser.readline().decode('ascii', errors='ignore').strip()if line.startswith('$'):print(f"✅ Connected to {port} at {BAUD} baud.\n")return serser.close()except Exception as e:print(f"⚠️ Could not open {port}: {e}")return Nonedef read_gps(ser):try:print("Reading GPS data... (Press Ctrl+C to stop)\n")while True:line = ser.readline().decode('ascii', errors='ignore').strip()if line.startswith('$'):print(line)except KeyboardInterrupt:print("\nExiting...")finally:ser.close()if __name__ == "__main__":ser = try_ports()if ser:read_gps(ser)else:print("❌ No valid GPS device found. Please check the connection.")