Linux下串口终端通信基础编程及简单的GPS设备数据读取

头文件:
          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;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值