Python serial、pynmea2的使用

代码作用:
使用serial、pynmea2实现python对串口的GPS模块的NMEA数据进行解析。

 

import serial
import pynmea2

ser = serial.Serial('COM4', 9600, timeout=0.2)

while True:
    try:
        recv = ser.readline().decode()
        if recv.startswith('$'):
            record = pynmea2.parse(recv)
            if recv.startswith('$GPRMC') or recv.startswith('$GNRMC'):
                print('--------------------------------')
                print('Fix Status: ', record.status)
                #print('Latitude: ', record.latitude)
                #print('Longitude: ', record.longitude)
            elif recv.startswith('$GPGGA') or recv.startswith('$GNGGA'):
                print('Number of Satellites availabe:', record.num_sats)
            elif recv.startswith('$GPGSV') or recv.startswith('$BDGSV') or recv.startswith('$GBGSV') or recv.startswith('$GLGSV'):
                if record.msg_num =='1':
                    print('Number of Satellites in View:', record.num_sv_in_view)
                print("Satallites No.:  GROUP", record.msg_num+'    ','['+record.sv_prn_num_1+':'+record.snr_1+']', '['+record.sv_prn_num_2+':'+record.snr_2+']', '['+record.sv_prn_num_3+':'+record.snr_3+']', '['+record.sv_prn_num_4+':'+record.snr_4+']')
                #print("Satallites CN0: ", record.snr_1, record.snr_2, record.snr_3, record.snr_4)
            elif recv.startswith('$GPGSA') or recv.startswith('$BDGSA') or recv.startswith('$GNGSA'):
                    print('Fixed Satellites No.: ', record.sv_id01, record.sv_id02, record.sv_id03, record.sv_id04,record.sv_id05, record.sv_id06,record.sv_id07, record.sv_id08,record.sv_id09, record.sv_id10,record.sv_id11, record.sv_id12)
    except pynmea2.nmea.ParseError:
        print('NMEA wrong!')

 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值