Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下~

两个实用的函数~


/**
*@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void
*/
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200,  300, 115200, 38400, 19200, 9600, 4800, 2400, 1200,  300, };
void set_speed(int fd, int speed){int   i; int   status; struct termios   Opt;tcgetattr(fd, &Opt); for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) { if  (speed == name_arr[i]) {     tcflush(fd, TCIOFLUSH);     cfsetispeed(&Opt, speed_arr[i]);  cfsetospeed(&Opt, speed_arr[i]);   status = tcsetattr(fd, TCSANOW, &Opt);  if  (status != 0) {        perror("tcsetattr fd1");  return;     }    tcflush(fd,TCIOFLUSH);   }  }
}

/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄
*@param  databits 类型  int 数据位   取值 为 7 或者8
*@param  stopbits 类型  int 停止位   取值为 1 或者2
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{ struct termios options; if  ( tcgetattr( fd,&options)  !=  0) { perror("SetupSerial 1");     return(FALSE);  }options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/{   case 7:      options.c_cflag |= CS7; break;case 8:     options.c_cflag |= CS8;break;   default:    fprintf(stderr,"Unsupported data size\n"); return (FALSE);  }switch (parity) {   case 'n':case 'N':    options.c_cflag &= ~PARENB;   /* Clear parity enable */options.c_iflag &= ~INPCK;     /* Enable parity checking */ break;  case 'o':   case 'O':     options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/  options.c_iflag |= INPCK;             /* Disnable parity checking */ break;  case 'e':  case 'E':   options.c_cflag |= PARENB;     /* Enable parity */    options.c_cflag &= ~PARODD;   /* 转换为偶效验*/     options.c_iflag |= INPCK;       /* Disnable parity checking */break;case 'S': case 's':  /*as no parity*/   options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;break;  default:   fprintf(stderr,"Unsupported parity\n");    return (FALSE);  }  /* 设置停止位*/  switch (stopbits){   case 1:    options.c_cflag &= ~CSTOPB;  break;  case 2:    options.c_cflag |= CSTOPB;  break;default:    fprintf(stderr,"Unsupported stop bits\n");  return (FALSE); } /* Set input parity option */ if (parity != 'n')   options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH);options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/   options.c_cc[VMIN] = 0; /* Update the options and do it NOW */if (tcsetattr(fd,TCSANOW,&options) != 0)   { perror("SetupSerial 3");   return (FALSE);  } options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/options.c_oflag  &= ~OPOST;   /*Output*/return (TRUE);
}

调用的方法比較的简单,例如以下。fd是打开的tty设备的文件句柄

    set_speed(fd,115200);if (set_Parity(fd,8,1,'N') == FALSE)  {printf("Set Parity Error\n");}

总的測试代码例如以下。

#include <sys/types.h>#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define BAUDRATE        B115200
#define UART_DEVICE     "/dev/ttyS3"#define FALSE  -1
#define TRUE   0/**
*@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void
*/
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200,  300, 115200, 38400, 19200, 9600, 4800, 2400, 1200,  300, };
void set_speed(int fd, int speed){int   i; int   status; struct termios   Opt;tcgetattr(fd, &Opt); for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) { if  (speed == name_arr[i]) {     tcflush(fd, TCIOFLUSH);     cfsetispeed(&Opt, speed_arr[i]);  cfsetospeed(&Opt, speed_arr[i]);   status = tcsetattr(fd, TCSANOW, &Opt);  if  (status != 0) {        perror("tcsetattr fd1");  return;     }    tcflush(fd,TCIOFLUSH);   }  }
}/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄
*@param  databits 类型  int 数据位   取值 为 7 或者8
*@param  stopbits 类型  int 停止位   取值为 1 或者2
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{ struct termios options; if  ( tcgetattr( fd,&options)  !=  0) { perror("SetupSerial 1");     return(FALSE);  }options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/{   case 7:      options.c_cflag |= CS7; break;case 8:     options.c_cflag |= CS8;break;   default:    fprintf(stderr,"Unsupported data size\n"); return (FALSE);  }switch (parity) {   case 'n':case 'N':    options.c_cflag &= ~PARENB;   /* Clear parity enable */options.c_iflag &= ~INPCK;     /* Enable parity checking */ break;  case 'o':   case 'O':     options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/  options.c_iflag |= INPCK;             /* Disnable parity checking */ break;  case 'e':  case 'E':   options.c_cflag |= PARENB;     /* Enable parity */    options.c_cflag &= ~PARODD;   /* 转换为偶效验*/     options.c_iflag |= INPCK;       /* Disnable parity checking */break;case 'S': case 's':  /*as no parity*/   options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;break;  default:   fprintf(stderr,"Unsupported parity\n");    return (FALSE);  }  /* 设置停止位*/  switch (stopbits){   case 1:    options.c_cflag &= ~CSTOPB;  break;  case 2:    options.c_cflag |= CSTOPB;  break;default:    fprintf(stderr,"Unsupported stop bits\n");  return (FALSE); } /* Set input parity option */ if (parity != 'n')   options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH);options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/   options.c_cc[VMIN] = 0; /* Update the options and do it NOW */if (tcsetattr(fd,TCSANOW,&options) != 0)   { perror("SetupSerial 3");   return (FALSE);  } options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/options.c_oflag  &= ~OPOST;   /*Output*/return (TRUE);
}int main(int argc, char *argv[])
{int    fd, c=0, res;char  buf[256];printf("Start...\n");fd = open(UART_DEVICE, O_RDWR);if (fd < 0) {perror(UART_DEVICE);exit(1);}printf("Open...\n");set_speed(fd,115200);if (set_Parity(fd,8,1,'N') == FALSE)  {printf("Set Parity Error\n");exit (0);}printf("Reading...\n");while(1) {res = read(fd, buf, 255);if(res==0)continue;buf[res]=0;printf("%s", buf);if (buf[0] == 0x0d)printf("\n");if (buf[0] == '@') break;}printf("Close...\n");close(fd);return 0;
}

转载于:https://www.cnblogs.com/ldxsuanfa/p/10071521.html

Linux下读写UART串口的代码相关推荐

  1. Linux uart寄存器读写,Linux下读写UART串口的代码

    Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下- 两个实用的函数- /** *@brief 设置串口 ...

  2. linux下c的串口收发

    linux下c的串口收发录 转自: https://blog.csdn.net/weixin_41471318/article/details/116230465 文章目录 linux下c的串口收发录 ...

  3. linux i2c 读写函数,Linux下读写芯片的I2C寄存器

    要想在Linux下读写芯片的I2C寄存器,一般需要在Linux编写一份该芯片的I2C驱动,关于Linux下如何编写I2C驱动,前一篇文章<手把手教你写Linux I2C设备驱动>已经做了初 ...

  4. Linux下读写芯片的I2C寄存器

    Linux下读写芯片的I2C寄存器 2012-01-10 11:40:18 标签:Linux 寄存器 驱动 读写 I2C 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本 ...

  5. linux下Qt编写串口调试助手,如何在linux下用QT写一个简单的串口调试助手

    如何在linux下用QT写一个简单的串口调试助手 QT5串口类 在QT5以前,编写串口一般使用的是qextserialport类,但在QT5之后有了QT自带的串口类SerialPort(串口基础类)和 ...

  6. Qt4 Linux下读写excel

    背景 最近项目中涉及到excel的读写,因为是在Linux上操作,而且是Qt4的版本,所以相对有些麻烦.之前我们用的是直接通过python去读写excel,把数据存储到中间文件.然后读中间文件进行操作 ...

  7. 一份简单的在 Linux下编译及调试 C 代码的指南

    摘要: 一份简单的在 Linux下编译及调试 C 代码的指南 对于Linux下的C程序员来说,几乎天天都会和Linux打交道.但在很多人的眼中,Linux是一个易用性极差.靠命令驱动的操作系统,根本无 ...

  8. Linux下向GitHub 上传代码

    Linux下向GitHub 上传代码 1.先在Github个人主页创建一个仓库 2.在根目录下,复制仓库链接,将仓库复制到本地 git clone https://gitclone.com/githu ...

  9. linux如何调用rs232串口,linux下的rs232串口通讯c代码

    补充: 针口的叫"公头",有孔的叫"母头",如果没有两个母头的串口线的话,可以使用虚拟机,两个虚拟机之间采用"管道"的方式连接,可达到几乎和 ...

  10. Linux下C语言串口应用编程,Linux下串口C语言编程

    Linux下串口C语言编程 (5页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.9 积分 串口操作代码#include #include #inclu ...

最新文章

  1. jffs2 告警 和 一般性错误
  2. Java动态追踪技术探究
  3. Google、Azure、阿里云、RedHat…全球的 K8s 圈大佬聚在一起要聊啥?
  4. python添加、修改、删除、访问类对象属性的2种方法
  5. python 知识点总结
  6. latex 分页_latex 图片跨页显示问题???
  7. java 建立ssh隧道_如何使用IntelliJ和JDBC SSH隧道并连接到数据库?
  8. $(img)是什么意思_什么原因可以让你坚持跑步?医生总结5点,足以让你告别懒惰...
  9. php多条件查询统计,PHP-----多条件查询
  10. Windows系列服务器上配置JSP运行环境,以及网站上线
  11. 数学之美——google大脑和人工神经网络
  12. 【css】让img图片居中显示
  13. 一张图片中多个图标如何通过CSS定位显示?
  14. 【书影观后感 五】你的名字
  15. 51Nod 1228 序列求和
  16. 计算机上语音是什么符号,语音
  17. 求营业额占比 【问题描述】 从键盘输入学校附近某烧烤店某年每月的营业额,然后计算每月的营业额在年营业额中所占的百分比(四舍五入为整数,且不会超过全年的70%),并以样例输出所示的水平直方图形式
  18. 国产单片机GD32系列开坑,带你零死角玩转GD32 第六章
  19. ettercap的使用帮助文档 官方man page的个人理解
  20. 贝叶斯算法及其应用案例

热门文章

  1. A/B Testing
  2. 【Firewalld(Iptables)】
  3. SpannableString 给TextView添加不同的显示样式
  4. Android studio如何导入已有的eclipse工程
  5. 使用SCVMM2008 R2管理Hyper-V之3-使用模板部署虚拟机
  6. Mac 下安装RabbitMQ及配置RabbitMQ可远程访问
  7. 这是我见过Java版的最好的OA系统,拿来即用,非常方便(附项目地址)
  8. 张一鸣念员工报告讽刺“互联网八股文” 网友:老板都看不惯了
  9. 别乱提交代码了,你必须知道的 Git 分支开发规范!
  10. 卧槽!火爆GitHub的算法电子书开放下载了!