从串行端口Ubuntu读取GPS数据

时间:2017-07-21 12:24:06

标签: c linux gps serial-port

我正在尝试从串口读取GPS数据,此刻我可以读取数据。但我试图只从“$ GPRMC”行获取数据。 我有这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>

int main(int argc, char** argv) {

    int sfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
    if (sfd == -1) {
        printf("Error no is : %d\n", errno);
        printf("Error description is : %s\n", strerror(errno));
        return (-1);
    };
    struct termios options;
    tcgetattr(sfd, &options);

    cfsetspeed(&options, B9600);
    cfmakeraw(&options);
    //options.c_cc[VTIME]=1;
    //options.c_cc[VMIN]=100;
    //tcflush(sfd, TCIFLUSH);
    tcsetattr(sfd, TCSANOW, &options);
    usleep(100000);
    char serialBuffer[500];
    int bytes;
    int count;
    ioctl(sfd, FIONREAD, &bytes);
    int i =0;
    char *ret;
    char *sepa;
    char sepa2[500];
    int compare;

    for(;;){


        if(bytes!=0){
            count = read(sfd, serialBuffer, sizeof(serialBuffer));
        }
        if (count == -1){
            printf("ERROR");
            break;
        }
        else if(count == 0){
            printf("NODATA");
            break;
        }
        else{
            serialBuffer[count] = '\0';
            //strcpy(serialBuffer2, serialBuffer);
            //printf("%s,", serialBuffer);
            sepa = strtok(serialBuffer, "\n");

            while(sepa != NULL){
                //printf("%s", sepa);
                //strcpy(serialBuffer2, sepa);
                sepa = strtok(NULL, "\n");

                ret = strstr(sepa, "$GPRMC");
                if (NULL != ret) {
                    printf("The substring is: %s\n", ret);
                }

            }

        }

    }
    close(sfd);
return (EXIT_SUCCESS);
}

这个脚本总是给我回复“Segmentation Faul(Core Dumped)”,我不知道为什么。我是新来的c,任何人都可以帮我吗?

由于

0 个答案:

没有答案