最近找到一个GPS模块,在接上了CH340串口工具后,我想试试自己编写一个程序在Linux环境下读取GPS数值。
- 串口编程的流程
串口也是一种字符设备,串口编程的流程包括:
打开串口:open函数
初始化串口:自定义个函数,需要设置波特率、数据位、校验位等
发送和接收数据:write和read函数
关闭串口:close函数
一般Linux系统中,在/dev目录下都会有tty*的设备节点,启动开发板,超级中输入命令查看如下,列出了多种形式的设备节点,这里GPS的节点是ttyUSB0.
1.2 初始化串口
Linux下串口初始化流程:
获取需要配置的串口句柄fd;
定义新旧两个termios结构体;
使用tcgetattr函数获取当前串口配置参数,用于检测;
设置需要的参数:波特率、停止位、校验位等;
使用tcflush函数清除寄存器;
使用tcsetattr函数设置新的参数
1.2.1 termios结构体
tcgetattr函数
作用:读取当前串口的配置参数,一般用于先确认该串口是否能够配置,做检测用
头文件:#include <termios.h>、#include <unistd.h>
原型:int tcgetattr(int fd, struct termios *termios_p)
参数:fd,是open函数返回的文件句柄,*termios_p是参数配置结构体,读取的参数值存储在这里
cfsetispeed和cfsetospeed函数
作用:设置串口的输入和输出波特率
头文件:#include <termios.h>、#include <unistd.h>
原型:int cfsetispeed(struct termios *termios_p, speed_t speed)、int cfsetospeed(struct termios *termios_p, speed_t speed)
参数:*termios_p是参数配置结构体,speed是波特率,常用B2400,B4800,B9600,B115200,B460800等表示,返回值0成功,-1失败
cfgetispeed和cfgetospeed函数
作用:读取串口的输入和输出波特率
头文件:#include <termios.h>、#include <unistd.h>
原型:speed_t cfgetispeed(const struct termios *termios_p)、speed_t cfgetospeed(const struct termios *termios_p)
参数:speed_t 返回值,当前波特率
cflush函数
作用:清空串口寄存器缓存数据
头文件:#include <termios.h>、#include <unistd.h>
原型:int tcflush(int fd, int queue_selector)
参数:fd,是open函数返回的文件句柄;queue_selector,控制操作类型,常用值:一般TCIFLUSH 清除正收到的数据,且不会读取出来;TCOFLUSH 清除正写入的数据,且不会发送至终端;TCIOFLUSH 清除所有正在发生的 I/O 数据;返回0成功,-1失败
tcsetattr函数
作用:设置串口参数
头文件:#include <termios.h>、#include <unistd.h>
原型:int tcsetattr(int fd, int optional_actions,const struct termios *termios_p)
参数:fd,是open函数返回的文件句柄;optional_actions,参数生效时间,常用值:TCSANOW不等数据传输完毕就立即改变属性;TCSADRAIN等待所有数据传输结束才改变属性;TCSAFLUSH清空输入输出缓冲区才改变属性;*termios_p要设置的串口参数;返回0成功,-1失败
初始化串口代码
根据以上串口初始化流程和关键结构体及函数的总结,这里很容易写出一个初始化串口的函数,函数不再单独说明了,里面有注释,代码如下:
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) { //检测串口是否可用
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits ) //设置数据位
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )//设置检验位
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed ) //设置波特率
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )//设置停止位
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0) //设置串口参数
{
perror("com set error");
return -1;
}
// printf("set done!\n\r");
return 0;
}
2 串口的读写测试例程
【需求】:打印出串口发送的GPS数据
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
int set_opt(int,int,int,char,int);
void main()
{
int fd,nByte;
char *uart3 = "/dev/ttyUSB0";//要使用的开发板串口
char buffer[512];
char *uart_out = "please input\r\n";
memset(buffer, 0, sizeof(buffer));
if((fd = open(uart3, O_RDWR|O_NOCTTY))<0)
printf("open %s is failed",uart3);
else{
set_opt(fd, 115200, 8, 'N', 1);
write(fd,uart_out, strlen(uart_out));
while(1){
while((nByte = read(fd, buffer, 512))>0){
buffer[nByte+1] = '\n';
buffer[nByte+2] = '\0';
printf("%s\n",buffer);
memset(buffer, 0, strlen(buffer));
nByte = 0;
}
}
}
}
参考 https://cloud.tencent.com/developer/article/1760214