Linux下实现串口接收GPS数据

本文介绍了在Linux环境下,通过串口接收并处理GPS数据的步骤。首先,使用`cat /dev/ttyS1`命令接收GPS数据,然后详细阐述了串口通信流程,包括打开、配置和关闭串口。接着,讲解了GPS数据处理,特别是针对GNRMC命令的解析,并提供了设置GPS模块只输出GNRMC信息的方法。最后,给出了GPS数据处理的代码实现及在Ubuntu下的编译运行过程。
摘要由CSDN通过智能技术生成

最近在做Linux下的串口接收并处理GPS数据,那对于是新手的我来说,就将这个项目分成两步,第一,接收数据,第二,处理数据。本文注重第二步的代码实现,第一步只会简单提及。本文使用的嵌入式开发板龙芯智龙1C,GPS模块采用的是正点原子的ATK-S1216F8-BD模块。
在这里插入图片描述
第一,接收数据:在配置好开发板的串口及相关硬件后,使用cat /dev/ttyS1能将串口接收到的GPS数据打印在控制台上。模块连接图如下:在这里插入图片描述
若控制台上能够显示GPS数据,则开始第二步。
在这里插入图片描述

第二,处理数据:
(1).串口操作:Linux下编程的过程有些固定,很多都是比如打开、配置、关闭等等
串口通信流程:打开串口ttySn—>初始化串口—>读写(read、write)—>关闭串口。

以下是usart.h代码实现:

#ifndef __USART_H
#define __USART_H

#include<stdio.h>      /*标准输入输出定义*/    
#include<stdlib.h>     /*标准函数库定义*/    
#include<unistd.h>     /*Unix 标准函数定义*/    
#include<sys/types.h>     
#include<sys/stat.h>       
#include<fcntl.h>      /*文件控制定义*/    
#include<termios.h>    /*PPSIX 终端控制定义*/    
#include<errno.h>      /*错误号定义*/    
#include<string.h>

#define SERIAL1 "/dev/ttyS1"
#define SERIAL3 "/dev/ttyS3"

int UART_Open(int fd, char *port);
void UART_Close(int fd); 
int UART_Set(int fd, int speed, int flow_ctrl, int databits, int stopbits, char parity);
int UART_Init(int fd, int speed, int flow_ctrl, int databits, int stopbits, char parity) ;
int UART_Recv(int fd, char *rcv_buf, int data_len);
int UART_Send(int fd, char *send_buf, int data_len);

#endif

usart.c代码实现:

#include "usart.h"

//fd:串口文件描述符   port:串口号(/SERIAL1/SERIAL3)
int UART_Open(int fd, char *port)
{
	fd = open(port, O_RDWR|O_NOCTTY|O_NDELAY);
	if(fd < 0)
	{
		perror("Can't Open Serial Port:");
		return -1;
	}
	
	//恢复串口为阻塞状态                                   
    if(fcntl(fd, F_SETFL, 0) < 0)    
    {    
        printf("fcntl failed!\n");    
        return -1;    
    }         
    else    
    {    
        printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));    
    }    
    //测试是否为终端设备        
    if(0 == isatty(STDIN_FILENO))    
    {    
        printf("standard input is not a terminal device\n");    
        return -1;    
    }    
    else    
    {    
        printf("isatty success!\n");    
    }                  
    printf("fd->open=%d\n",fd);    
	
    return fd; 
}

void UART_Close(int fd)
{
	close(fd);
}

/*******************************************************************  
*名称:             UART_Set  
*功能:             设置串口数据位,停止位和效验位  
*入口参数:        fd          串口文件描述符
*                   speed       串口速度  
*                   flow_ctrl   数据流控制  
*                   databits    数据位   取值为7或者8  
*                   stopbits    停止位   取值为1或者2  
*                   parity      效验类型 取值为N,E,O,S   
*******************************************************************/ 
int UART_Set(int fd, int speed, int flow_ctrl, int databits, int stopbits, char parity)
{
	int   i;    
    int   status;    
    int   speed_arr[] = { B1152
  • 10
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在C语言中接收GPS数据,你需要使用串口通信来与GPS设备进行数据交换。下面是一个基本的串口通信代码示例,可以用来接收GPS数据: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> int main() { int fd; struct termios options; char buffer[255]; fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { printf("Error opening serial port\n"); return -1; } fcntl(fd, F_SETFL, 0); tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~CRTSCTS; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; tcsetattr(fd, TCSANOW, &options); while (1) { int bytes_read = read(fd, buffer, sizeof(buffer)); if (bytes_read > 0) { buffer[bytes_read] = '\0'; printf("%s", buffer); } } close(fd); return 0; } ``` 在这个示例中,我们使用了Linux系统上的串口通信库。打开串口设备时,我们指定了设备名称 `/dev/ttyUSB0`,这是Linux系统中GPS设备串口的默认名称。要确保你的GPS设备连接到了正确的串口,并且串口的波特率设置正确。在本例中,我们将波特率设置为9600。 在 `while` 循环中,我们使用 `read` 函数从串口读取数据。如果读取到了数据,就将其打印到控制台上。你可以将此代码作为起点,根据你的具体需求进行修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值