【昉·星光2 RISC-V 单板计算机】GPS定位
分享作者:lijinlei
作者昵称:无垠的广袤
评测品牌:赛昉科技
评测型号:VF202040-A0
发布时间:2025-07-07 15:08:08
0
前言
本文介绍了昉·星光2单板计算机结合 GPS 模块和串口通信功能实现 GPS 定位 的项目设计。
开源口碑分享内容

【昉·星光2 RISC-V 单板计算机】GPS定位

本文介绍了昉·星光2单板计算机结合 GPS 模块和串口通信功能实现 GPS 定位 的项目设计。

硬件连接


GPS module pin name40-pin GPIO header pin numberDetails
VCC45V 电压
GND6GND
TXD10GPIO6 (UART RX)
RXD8GPIO5 (UART TX)

实物连接

参考:使用昉·星光 2的UART读取GPS数据 .

流程图

代码

import serial

# Set serial port parameters (adjust according to your GPS module)
SERIAL_PORT = "/dev/ttyS0"  # It might be /dev/ttyUSB0 or others
BAUDRATE = 9600  # Common baud rate for GPS modules is 9600

def read_all_gps_data():
    try:
        print(f"Starting to read GPS data (Serial port: {SERIAL_PORT}, Baudrate: {BAUDRATE})...")
        with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=1) as ser:
            while True:
                line = ser.readline().decode('ascii', errors='ignore').strip()
                if line:  # Print only if not an empty line
                    print(line)
    except KeyboardInterrupt:
        print("\nStopping GPS data reading")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    read_all_gps_data()

保存代码并运行

效果

打印 GPS 原始数据,包括多套卫星定位系统

解析 GPS 原始数据

安装 NMEA 解析库

apt install python3-nmea2

代码

调用 pynmea2serial 库函数,实现串口数据读取与时间及坐标转换,并将结果打印至终端。

import serial
import time
from pynmea2 import parse

def parse_gps_data():
    try:
        ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
        print("Reading GPS data...")

        while True:
            line = ser.readline().decode('ascii', errors='ignore').strip()
            if not line:
                continue

            try:
                data = parse(line)
                
                # Force extraction of time (return "00:00:00" if it doesn't exist)
                time_utc = getattr(data, 'timestamp', "0").strftime("%H:%M:%S") if hasattr(data, 'timestamp') else "00:00:00"
                
                # Force extraction of latitude and longitude (return 0.0 if they don't exist)
                lat = getattr(data, 'latitude', 0.0)
                lon = getattr(data, 'longitude', 0.0)
                lat_dir = 'N' if lat >= 0 else 'S'
                lon_dir = 'E' if lon >= 0 else 'W'
                lat_deg = f"{abs(lat):.6f} deg {lat_dir}"
                lon_deg = f"{abs(lon):.6f} deg {lon_dir}"

                # Print a single set of data
                print(f"Time (UTC): {time_utc}")
                print(f"Latitude: {lat_deg}")
                print(f"Longitude: {lon_deg}")
                time.sleep(1)
            except Exception as e:
                continue  # Silently ignore parsing errors

    except KeyboardInterrupt:
        print("\nStopping GPS data reading")
    except Exception as e:
        print(f"Serial port error: {e}")
    finally:
        if 'ser' in locals():
            ser.close()
            print("Serial port closed")

if __name__ == "__main__":
    parse_gps_data()

保存代码,终端执行指令 python3 gps_time_coordinate.py 运行串口数据转换程序;

效果

终端打印时间和经纬度信息,更新的时间间隔为 1 秒。

总结

本文介绍了昉·星光2单板计算机实现 GPS 定位 的项目设计,为该开发板在物联网领域的应用和快速开发提供了参考。


全部评论
暂无评论
0/144