通过串口从GPS传感器读取

我是C ++的初学者,请保持友善,

我想读取gps传感器发送的数据。

我的代码现在看起来像这样:

#include <iostream>
#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions
#include <string>
#include <sstream>
#include <vector>

char * testSerialComm(int USB)
{
    /* *** Configure Port *** */
        struct termios tty;
        memset (&tty,sizeof tty);

    /* Error Handling */
        if ( tcgetattr ( USB,&tty ) != 0 )
        {
            std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
        }

    /* Set Baud Rate */
        cfsetospeed (&tty,B9600);
        cfsetispeed (&tty,B9600);

    /* Setting other Port Stuff */
        tty.c_cflag     &=  ~PARENB;        // Make 8n1
        tty.c_cflag     &=  ~CSTOPB;
        tty.c_cflag     &=  ~CSIZE;
        tty.c_cflag     |=  CS8;
        tty.c_cflag     &=  ~CRTSCTS;       // no flow control
        tty.c_lflag     =   0;          // no signaling chars,no echo,no canonical processing
        tty.c_oflag     =   0;                  // no remapping,no delays
        tty.c_cc[VMIN]      =   0;                  // read doesn't block
        tty.c_cc[VTIME]     =   15;                  // 0.5 seconds read timeout

        tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines
        tty.c_iflag     &=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
        tty.c_lflag     &=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
        tty.c_oflag     &=  ~OPOST;              // make raw

    /* Flush Port,then applies attributes */
        tcflush( USB,TCIFLUSH );

        if ( tcsetattr ( USB,TCSANOW,&tty ) != 0)
        {
            std::cout << "Error " << errno << " from tcsetattr" << std::endl;
        }

    /* *** WRITE *** */

        unsigned char cmd[] = {'I','N','I','T',' ','\r','\0'};
        int n_written = write( USB,cmd,sizeof(cmd) -1 );

    /* Allocate memory for read buffer */
        char buf [1024];
        memset (&buf,'\0',sizeof buf);

    /* *** READ *** */
        int n = read( USB,&buf,sizeof buf );

    /* Error Handling */
        if (n < 0)
        {
            std::cout << "Error reading: " << strerror(errno) << std::endl;
        }

    /* Print what I read... */
       return buf;

    close(USB);
}

int main() {
    /* Open File Descriptor */
    int USB = open( "/dev/tty.usbmodem14201",O_RDONLY | O_NDELAY | O_NONBLOCK);

    /* Error Handling */
    if ( USB < 0 )
    {
        std::cout << "Error " << errno << " opening " << "/dev/ttyUSB0" << ": " << strerror (errno) << std::endl;
    }
    while(true){
        std::string g = testSerialComm(USB);
        std::stringstream stringStream(g);
        std::string segment;
        std::vector<std::string> seglist;

        while(std::getline(stringStream,segment))
        {
            seglist.push_back(segment);
        }
        for(int i = 0; i < seglist.size(); i++){
            std::string s = seglist[i];
            if( g != ""){
                if(s.find("$GNGLL") != std::string::npos){
                    std::cout << g ;
                    if(s.find(",") != std::string::npos){
                        std::cout << "Keine Position erkannt" << std::endl;
                    }
                    else{
                        std::cout << "s = " << s;
                        std::stringstream stringStream(s);
                        std::string segment2;
                        std::vector<std::string> seglist2;
                        while(std::getline(stringStream,segment))
                        {
                            seglist2.push_back(segment2);
                        }
                        std::cout << "Lat: "<< seglist2[1] << " " << seglist2[2] << std::endl;
                        std::cout << "Lon: "<< seglist2[3] << " " << seglist2[4] << std::endl;
                        std::cout << "UTC Time: " << seglist2[5] << std::endl;
                    }
                }
            }
        }
    }
    return 0;
}

输出看起来像这样:

$GNVTG,T,M,1.346,N,2.493,K,A*31
$GNGGA,162555.00,5219.07535,01337.93745,E,1,06,2.06,41.2,42.1,*71
$GNGSA,A,3,08,21,13,15,27,05,3.73,3.11*11
$GNGSA,3.11*1C
$GPGSV,11,04,114,51,227,14,07,34,067,18,050,24*7F
$GPGSV,2,09,00,116,57,291,24,295,19,10,325,12*75
$GPGSV,017,28,45,149,30,68,074,20*4F
$GLGSV,01,23*65
$GNGLL,A*76
$GNRMC,162556.00,5219.07530,01337.93762,0.984,071119,A*$GPGSV,11*7E
$GPGSV,22*64
$GNGLL,A*75
$GNRMC,162557.00,01337.93755,1.021,A*64
$GNVTG,1.890,A*3F
$GNGGA,40.8,*7C
$GNGSA,3.72,3.10*11
$GNGSA,3.10*1C
$GPGSV,23*79
$GPGSV,12,09*75
$GPGSV,19*4C
$GLGSV,A*70
$GNRMC,162558.00,5219.07513,01337.93724,0.889,A*67
$GNVTG,1.646,*74
$GNGSA,20,22*74
$GPGSV,13*7C
$GPGSV,19*45
$GLGSV,A*78
$GNRMC,162559.00,5219.07523,01337.93720,0.766,A*$GNGSA,23*7E
$GPGSV,18*45
$GLGSV,A*7E
Keine Position erkannt
$GNRMC,162600.00,5219.07611,01337.93712,0.764,19*4E
$GLGSV,A*72
$GNRMC,162601.00,5219.07575,01337.93735,0.483,A*6E
$GNVTG,0.895,A*36
$GNGGA,2.05,40.3,*73
$GNGSA,3.10*12
$GNGSA,3.10*1F
$GPGSV,13*7D
$GPGSV,19*4F
$GLGSV,A*77
Keine Position erkannt
$GNRMC,162602.00,V,N*6D
$GNVTG,N*2E
$GNGGA,15.84,36.98,33.41*28
$GNGSA,33.41*27
$GPGSV,16,22*7D
$GPGSV,17,17*71
$GPGSV,N*55
Keine Position erkannt
$GNRMC,162603.00,5219.07421,01337.93894,0.811,21*7E
$GPGSV,10*79
$GPGSV,18*4A
$GLGSV,A*71
Keine Position erkannt
$GNRMC,162604.00,5219.07430,01337.93925,0.640,A*69
$GNVTG,1.186,*72
$GNGSA,18*7E
$GPGSV,A*7D

Process finished with exit code 15

我遇到了几个问题:

首先:每次插拔电源时,usb设备的名称都会更改。唯一改变的是数字。因此它从以下更改:

/dev/tty.usbmodem14201

收件人:

/dev/tty.usbmodem14101

如何找到正确的设备名称?

第二个问题: 我想从GPS传感器获取位置。如果它有位置,则消息看起来像这样:

$GNGLL,N*55

获得职位后,它看起来像这样:

$GNGLL,A*7D

坐标在第二和第四段中: $ GNGLL, 5219.07430 ,N, 01337.93925 ,E,162604.00,A,A * 7D

我的主要问题是,我想逐行读取设备,但是read( USB,sizeof buf );方法不适合这种情况。

xxd1111 回答:通过串口从GPS传感器读取

#$GNGLL decoder


import serial

port = "/dev/ttyS0"

def parseGPS(data):
#   print "raw:",data #prints raw data
    if data[0:6] == "$GNGLL":
        sdata = data.split(",")
        if sdata[2] == 'V':
            print "no satellite data available"
            return
        print "---Parsing GNGLL---",time = sdata[5][0:2] + ":" + sdata[5][2:4] + ":" + sdata[5][4:6]
        lat = decode(sdata[1]) #latitude
        lon = decode(sdata[3]) #longitute

        print  time+" "+lat+" "+lon


def decode(coord):
    #Converts  DMS to decimal system
       
    if(coord[0:1]=='0'):
      gps = coord[1:3]
    else:
      gps=coord[0:2]
    gps1=float(gps)+round((float(coord)/60)%10,4)
    return str(gps1)


print "Receiving GPS data"
ser = serial.Serial(port,baudrate = 9600,timeout = 0.5)
while True:
    data = ser.readline()
    parseGPS(data)
本文链接:https://www.f2er.com/3132891.html

大家都在问