概述
头文件:
termios.h
结构体:
//终端属性结构体 :起始位/停止位/数据位/校验位/硬件流控
struct termios {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
};
获得和设置终端属性函数:
/* Put the state of FD into *TERMIOS_P. */
extern int tcgetattr (int __fd, struct termios *__termios_p) ; //从内核中获取termios 结构体
/* Set the state of FD to *TERMIOS_P.
Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>. */
extern int tcsetattr (int __fd, int __optional_actions, __const struct termios *__termios_p) ; //设置终端属性
波特率获取设置函数:
/* Return the output baud rate stored in *TERMIOS_P. */
extern speed_t cfgetospeed (__const struct termios *__termios_p) ;
/* Return the input baud rate stored in *TERMIOS_P. */
extern speed_t cfgetispeed (__const struct termios *__termios_p) ;
/* Set the output baud rate stored in *TERMIOS_P to SPEED. */
extern int cfsetospeed (struct termios *__termios_p, speed_t __speed);
/* Set the input baud rate stored in *TERMIOS_P to SPEED. */
extern int cfsetispeed (struct termios *__termios_p, speed_t __speed);
————————————————————————————————————————————————————————————————————————————
GPS 设备读取,未解码
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(void)
{
int fd;
fd = open("/dev/ttyUSB0", O_RDWR|O_NOCTTY); //打开GPS设备文件
if (fd < 0)
{
perror("open");
return 1;
}
///
struct termios opts;
tcgetattr(fd, &opts);
cfsetispeed(&opts, B38400); //设置波特率
cfsetospeed(&opts, B38400);
//8N1
opts.c_cflag |= CLOCAL|CREAD;
opts.c_cflag &= ~PARENB;
opts.c_cflag &= ~CSTOPB;
opts.c_cflag &= ~CSIZE;
opts.c_cflag |= CS8;
//硬件流控
opts.c_cflag &= CRTSCTS;
tcsetattr(fd, TCSANOW, &opts);
tcflush(fd, TCIOFLUSH);
char ch;
while (1) //循环读取
{
read(fd, &ch, 1);
putchar(ch);
}
return 0;
}
最后
以上就是奋斗酸奶为你收集整理的Linux下串口终端通信基础编程及简单的GPS设备数据读取的全部内容,希望文章能够帮你解决Linux下串口终端通信基础编程及简单的GPS设备数据读取所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复