1. /* uart_tx.c */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <getopt.h>
  11. #include <termios.h>
  12.  
  13. #define TEST_LEN (1024 * 400)
  14. static char *dev_name = "/dev/ttyS2";
  15. static int baud_rate = ;
  16. static struct option opts[] = {
  17. {"device", required_argument, NULL, 'd'},
  18. {"baud", required_argument, NULL, 'b'},
  19. {"help", no_argument, NULL, 'h'},
  20. };
  21.  
  22. static void parse_cmd(int argc, char *argv[])
  23. {
  24. int ch;
  25.  
  26. while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
  27. switch (ch) {
  28. case 'd':
  29. //printf("dev_name: %s\n", optarg);
  30. dev_name = optarg;
  31. break;
  32. case 'b':
  33. //printf("baud_rate: %s\n", optarg);
  34. baud_rate = atoi(optarg);
  35. break;
  36. case 'h':
  37. printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
  38. printf("like:\n");
  39. printf("\t %s -d /dev/ttyS2\n", argv[]);
  40. printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
  41. break;
  42. default:
  43. printf("Unknown option or invalid format!\n");
  44. printf("Pls: %s --help for more info\n", argv[]);
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
  51. {
  52. struct termios tty_cfg;
  53.  
  54. memset(&tty_cfg, , sizeof(tty_cfg));
  55. tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
  56. tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
  57.  
  58. switch(baud_rate) {
  59. case :
  60. cfsetispeed(&tty_cfg, B2400);
  61. cfsetospeed(&tty_cfg, B2400);
  62. break;
  63. case :
  64. cfsetispeed(&tty_cfg, B4800);
  65. cfsetospeed(&tty_cfg, B4800);
  66. break;
  67. case :
  68. cfsetispeed(&tty_cfg, B9600);
  69. cfsetospeed(&tty_cfg, B9600);
  70. break;
  71. case :
  72. cfsetispeed(&tty_cfg, B115200);
  73. cfsetospeed(&tty_cfg, B115200);
  74. break;
  75. case :
  76. cfsetispeed(&tty_cfg, B460800);
  77. cfsetospeed(&tty_cfg, B460800);
  78. break;
  79. default:
  80. cfsetispeed(&tty_cfg, B9600);
  81. cfsetospeed(&tty_cfg, B9600);
  82. break;
  83. }
  84.  
  85. switch(nBits) {
  86. case :
  87. tty_cfg.c_cflag |= CS7;
  88. break;
  89. case :
  90. tty_cfg.c_cflag |= CS8;
  91. break;
  92. }
  93.  
  94. switch(nEvent) {
  95. case '': /* 奇校验 */
  96. tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
  97. tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
  98. tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
  99. break;
  100. case 'E': /*偶校验*/
  101. tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
  102. tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
  103. tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
  104. break;
  105. case 'N': /*无奇偶校验*/
  106. tty_cfg.c_cflag &= ~PARENB;
  107. break;
  108. }
  109.  
  110. /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
  111. if(nStop == )
  112. tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
  113. else if( nStop == )
  114. tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
  115.  
  116. /* flow control option */
  117. tty_cfg.c_cflag |= CRTSCTS;
  118.  
  119. /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
  120. tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
  121. tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
  122. tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
  123.  
  124. /*激活配置使其生效*/
  125. if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
  126. printf("com set error");
  127. exit();
  128. }
  129.  
  130. return ;
  131. }
  132.  
  133. char buff[TEST_LEN];
  134.  
  135. int main(int argc, char *argv[])
  136. {
  137. int fd=;
  138. int cnt=;
  139. int sum=;
  140. static int num=;
  141. char *p=NULL;
  142.  
  143. parse_cmd(argc, argv);
  144. printf("TX: dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
  145.  
  146. fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
  147. if(fd < ) {
  148. printf("Can't Open %s\n", dev_name);
  149. return -;
  150. }
  151.  
  152. set_serial(fd, baud_rate, , 'N', );
  153. sleep();
  154.  
  155. memset(buff, 0x55, TEST_LEN);
  156. //printf("start send: %ds\n", time(NULL));
  157. p = &buff[];
  158. while() {
  159. cnt = write(fd, p, (TEST_LEN-sum));
  160. if(cnt < ) {
  161. //sleep(1);
  162. continue;
  163. }
  164. sum += cnt;
  165. if(sum >= TEST_LEN)
  166. break;
  167. p += cnt;
  168. printf("TX%d: cnt = %d, sum = %d\n", num++, cnt, sum);
  169. }
  170.  
  171. printf("send %d: %ds\n", sum, time(NULL));
  172.  
  173. close(fd);
  174. return ;
  175. }
  1. /* uart_rx.c */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <getopt.h>
  11. #include <termios.h>
  12.  
  13. #define TEST_LEN (1024 * 400)
  14. static char *dev_name = "/dev/ttyS2";
  15. static int baud_rate = ;
  16. static struct option opts[] = {
  17. {"device", required_argument, NULL, 'd'},
  18. {"baud", required_argument, NULL, 'b'},
  19. {"help", no_argument, NULL, 'h'},
  20. };
  21.  
  22. static void parse_cmd(int argc, char *argv[])
  23. {
  24. int ch;
  25.  
  26. while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
  27. switch (ch) {
  28. case 'd':
  29. //printf("dev_name: %s\n", optarg);
  30. dev_name = optarg;
  31. break;
  32. case 'b':
  33. //printf("baud_rate: %s\n", optarg);
  34. baud_rate = atoi(optarg);
  35. break;
  36. case 'h':
  37. printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
  38. printf("like:\n");
  39. printf("\t %s -d /dev/ttyS2\n", argv[]);
  40. printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
  41. break;
  42. default:
  43. printf("Unknown option or invalid format!\n");
  44. printf("Pls: %s --help for more info\n", argv[]);
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
  51. {
  52. struct termios tty_cfg;
  53.  
  54. memset(&tty_cfg, , sizeof(tty_cfg));
  55. tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
  56. tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
  57.  
  58. switch(baud_rate) {
  59. case :
  60. cfsetispeed(&tty_cfg, B2400);
  61. cfsetospeed(&tty_cfg, B2400);
  62. break;
  63. case :
  64. cfsetispeed(&tty_cfg, B4800);
  65. cfsetospeed(&tty_cfg, B4800);
  66. break;
  67. case :
  68. cfsetispeed(&tty_cfg, B9600);
  69. cfsetospeed(&tty_cfg, B9600);
  70. break;
  71. case :
  72. cfsetispeed(&tty_cfg, B115200);
  73. cfsetospeed(&tty_cfg, B115200);
  74. break;
  75. case :
  76. cfsetispeed(&tty_cfg, B460800);
  77. cfsetospeed(&tty_cfg, B460800);
  78. break;
  79. default:
  80. cfsetispeed(&tty_cfg, B9600);
  81. cfsetospeed(&tty_cfg, B9600);
  82. break;
  83. }
  84.  
  85. switch(nBits) {
  86. case :
  87. tty_cfg.c_cflag |= CS7;
  88. break;
  89. case :
  90. tty_cfg.c_cflag |= CS8;
  91. break;
  92. }
  93.  
  94. switch(nEvent) {
  95. case '': /* 奇校验 */
  96. tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
  97. tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
  98. tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
  99. break;
  100. case 'E': /*偶校验*/
  101. tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
  102. tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
  103. tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
  104. break;
  105. case 'N': /*无奇偶校验*/
  106. tty_cfg.c_cflag &= ~PARENB;
  107. break;
  108. }
  109.  
  110. /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
  111. if(nStop == )
  112. tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
  113. else if( nStop == )
  114. tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
  115.  
  116. /* flow control option */
  117. tty_cfg.c_cflag |= CRTSCTS;
  118.  
  119. /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
  120. tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
  121. tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
  122. tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
  123.  
  124. /*激活配置使其生效*/
  125. if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
  126. printf("com set error");
  127. exit();
  128. }
  129.  
  130. return ;
  131. }
  132.  
  133. void dump_data(char *buf)
  134. {
  135. int i;
  136.  
  137. for(i=; i<TEST_LEN; i++) {
  138. if(i% == )
  139. printf("\n");
  140. printf("0x%x, ", buf[i]);
  141. }
  142. }
  143.  
  144. char buff[TEST_LEN];
  145.  
  146. int main(int argc, char *argv[])
  147. {
  148. int fd=;
  149. int cnt=;
  150. int sum=;
  151. static int num=;
  152. char *p=NULL;
  153.  
  154. parse_cmd(argc, argv);
  155. printf("RX: dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
  156.  
  157. fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
  158. if(fd < ) {
  159. printf("Can't Open %s\n", dev_name);
  160. return -;
  161. }
  162.  
  163. set_serial(fd, baud_rate, , 'N', );
  164. sleep();
  165.  
  166. memset(buff, 0x0, TEST_LEN);
  167. //printf("start recv: %ds\n", time(NULL));
  168. p = &buff[];
  169. while() {
  170. cnt = read(fd, p, TEST_LEN);
  171. if(cnt <= ) {
  172. //sleep(1);
  173. continue;
  174. }
  175. sum += cnt;
  176. if(sum >= TEST_LEN)
  177. break;
  178. p += cnt;
  179. printf("RX%d: cnt = %d, sum = %d\n", num++, cnt, sum);
  180. }
  181. printf("recv %d: %ds\n", sum, time(NULL));
  182. p = NULL;
  183. close(fd);
  184.  
  185. //dump_data(buff);
  186.  
  187. return ;
  188. }

UART 串口示例代码的更多相关文章

  1. Linux下读写UART串口的代码

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

  2. Arduino - 串口操作函数与示例代码大全

    来源:https://blog.csdn.net/iracer/article/details/50334041 Arduino - 串口操作函数与示例代码大全 本文总结了Arduino常用串口操作函 ...

  3. (三) UART 串口通讯

    UART  : university asynchronous receiver and transmitter UART  // 通用异步接收器和发送器 为什么要有串口:因为许多嵌入式设备没有显示屏 ...

  4. 【C51】UART串口通信

    我们常需要单片机和其他模块进行通信,数据传输,常用的方式就是串口通信技术. 常用来 单片机<-->电脑,  单片机<-->单片机之间通信. 串行通信 versus 并行通信 并 ...

  5. uart串口协议

      uart串口协议   /* USART Word Length ---------------------------------------------------------*/     US ...

  6. 第十六章 IIC协议详解+UART串口读写EEPROM

    十六.IIC协议详解+Uart串口读写EEPROM 本文由杭电网友曾凯峰根据小梅哥FPGA IIC协议基本概念公开课内容整理并最终编写Verilog代码实现使用串口读写EEPROM的功能. 以下为原文 ...

  7. linux UART串口驱动开发文档

    转:http://www.360doc.com/content/10/0417/18/829197_23519037.shtml linux UART串口驱动开发文档时间:2010-01-09 14: ...

  8. 基于STM32之UART串口通信协议(四)Printf发送

    一.前言 1.简介 前面在UART发送中已经讲解过如何调用HAL库的HAL_UART_Transmit函数来实现串口发送,而在调用这个函数来实现串口发送的话,但是在发送数据或者字符的时候,需要将数据或 ...

  9. 基于STM32之UART串口通信协议(二)发送

    一.前言 1.简介 在上一篇UART详解中,已经有了关于UART的详细介绍了,也有关于如何使用STM32CubeMX来配置UART的操作了,而在该篇博客,主要会讲解一下如何实现UART串口的发送功能. ...

随机推荐

  1. elasticsearch中mapping的_source和store的笔记(转)

    原文地址: https://www.cnblogs.com/zklidd/p/6149120.html 0.故事引入 无意中看到了ES的mapping中有store字段,作为一个ES菜鸡,有必要对这个 ...

  2. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器软件实现的多码率视频点播功能说明

    关于EasyDSS EasyDSS(http://www.easydss.com)流媒体解决方案采用业界优秀的流媒体框架模式设计,服务运行轻量.高效.稳定.可靠.易维护,支持RTMP直播.RTMP推送 ...

  3. pdf转换成jpg不清晰怎么办

    用Photoshop打开pdf文件,然后用“文件”-“另存为”“JPG”.

  4. linux内存管理swap分区

    一.什么是linux的内存机制? 我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成,而内存是有限的,这样就引出了物理内存与虚拟内存的概念. 物 ...

  5. 【C/C++开发】模板类

    1.模板的概念 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数需要 ...

  6. (IO模型介绍,阻塞IO,非阻塞IO,多路复用IO,异步IO,IO模型比较分析,selectors模块,垃圾回收机制)

    参考博客: https://www.cnblogs.com/xiao987334176/p/9056511.html 内容回顾 协程实际上是一个线程,执行了多个任务,遇到IO就切换 切换,可以使用yi ...

  7. SQL - 外链接和内连接

    外链接和内连接: leetcode 题目:编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息: 第一次的答案:(错误) select ...

  8. Appium 中使用 pressKeyCode 方法不起作用也没有报错

    为了使 appium 支持 Android 系统 7 及以上,automationName 使用了 UIAutomator2.但是发现,使用androidDriver.pressKeyCode(And ...

  9. C++ 智能指针 shared_ptr 分析

    引文: C++对指针的管理提供了两种解决问题的思路: 1.不允许多个对象管理一个指针 2.允许多个对象管理一个指针,但仅当管理这个指针的最后一个对象析构时才调用delete ps:这两种思路的共同点就 ...

  10. 【LeetCode】 #7:反转整数 C语言

    目录 题目 思路 初步想法 进一步想法 总结 最近打算练习写代码的能力,所以从简单题开始做. 大部分还是用C语言来解决. @(解法) 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数 ...