UART 串口示例代码
- /* uart_tx.c */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <termios.h>
- #define TEST_LEN (1024 * 400)
- static char *dev_name = "/dev/ttyS2";
- static int baud_rate = ;
- static struct option opts[] = {
- {"device", required_argument, NULL, 'd'},
- {"baud", required_argument, NULL, 'b'},
- {"help", no_argument, NULL, 'h'},
- };
- static void parse_cmd(int argc, char *argv[])
- {
- int ch;
- while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
- switch (ch) {
- case 'd':
- //printf("dev_name: %s\n", optarg);
- dev_name = optarg;
- break;
- case 'b':
- //printf("baud_rate: %s\n", optarg);
- baud_rate = atoi(optarg);
- break;
- case 'h':
- printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
- printf("like:\n");
- printf("\t %s -d /dev/ttyS2\n", argv[]);
- printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
- break;
- default:
- printf("Unknown option or invalid format!\n");
- printf("Pls: %s --help for more info\n", argv[]);
- break;
- }
- }
- }
- int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
- {
- struct termios tty_cfg;
- memset(&tty_cfg, , sizeof(tty_cfg));
- tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
- tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
- switch(baud_rate) {
- case :
- cfsetispeed(&tty_cfg, B2400);
- cfsetospeed(&tty_cfg, B2400);
- break;
- case :
- cfsetispeed(&tty_cfg, B4800);
- cfsetospeed(&tty_cfg, B4800);
- break;
- case :
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- case :
- cfsetispeed(&tty_cfg, B115200);
- cfsetospeed(&tty_cfg, B115200);
- break;
- case :
- cfsetispeed(&tty_cfg, B460800);
- cfsetospeed(&tty_cfg, B460800);
- break;
- default:
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- }
- switch(nBits) {
- case :
- tty_cfg.c_cflag |= CS7;
- break;
- case :
- tty_cfg.c_cflag |= CS8;
- break;
- }
- switch(nEvent) {
- case '': /* 奇校验 */
- tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
- tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
- tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
- break;
- case 'E': /*偶校验*/
- tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
- tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
- tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
- break;
- case 'N': /*无奇偶校验*/
- tty_cfg.c_cflag &= ~PARENB;
- break;
- }
- /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
- if(nStop == )
- tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
- else if( nStop == )
- tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
- /* flow control option */
- tty_cfg.c_cflag |= CRTSCTS;
- /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
- tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
- tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
- tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
- /*激活配置使其生效*/
- if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
- printf("com set error");
- exit();
- }
- return ;
- }
- char buff[TEST_LEN];
- int main(int argc, char *argv[])
- {
- int fd=;
- int cnt=;
- int sum=;
- static int num=;
- char *p=NULL;
- parse_cmd(argc, argv);
- printf("TX: dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
- fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
- if(fd < ) {
- printf("Can't Open %s\n", dev_name);
- return -;
- }
- set_serial(fd, baud_rate, , 'N', );
- sleep();
- memset(buff, 0x55, TEST_LEN);
- //printf("start send: %ds\n", time(NULL));
- p = &buff[];
- while() {
- cnt = write(fd, p, (TEST_LEN-sum));
- if(cnt < ) {
- //sleep(1);
- continue;
- }
- sum += cnt;
- if(sum >= TEST_LEN)
- break;
- p += cnt;
- printf("TX%d: cnt = %d, sum = %d\n", num++, cnt, sum);
- }
- printf("send %d: %ds\n", sum, time(NULL));
- close(fd);
- return ;
- }
- /* uart_rx.c */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <termios.h>
- #define TEST_LEN (1024 * 400)
- static char *dev_name = "/dev/ttyS2";
- static int baud_rate = ;
- static struct option opts[] = {
- {"device", required_argument, NULL, 'd'},
- {"baud", required_argument, NULL, 'b'},
- {"help", no_argument, NULL, 'h'},
- };
- static void parse_cmd(int argc, char *argv[])
- {
- int ch;
- while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
- switch (ch) {
- case 'd':
- //printf("dev_name: %s\n", optarg);
- dev_name = optarg;
- break;
- case 'b':
- //printf("baud_rate: %s\n", optarg);
- baud_rate = atoi(optarg);
- break;
- case 'h':
- printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
- printf("like:\n");
- printf("\t %s -d /dev/ttyS2\n", argv[]);
- printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
- break;
- default:
- printf("Unknown option or invalid format!\n");
- printf("Pls: %s --help for more info\n", argv[]);
- break;
- }
- }
- }
- int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
- {
- struct termios tty_cfg;
- memset(&tty_cfg, , sizeof(tty_cfg));
- tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
- tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
- switch(baud_rate) {
- case :
- cfsetispeed(&tty_cfg, B2400);
- cfsetospeed(&tty_cfg, B2400);
- break;
- case :
- cfsetispeed(&tty_cfg, B4800);
- cfsetospeed(&tty_cfg, B4800);
- break;
- case :
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- case :
- cfsetispeed(&tty_cfg, B115200);
- cfsetospeed(&tty_cfg, B115200);
- break;
- case :
- cfsetispeed(&tty_cfg, B460800);
- cfsetospeed(&tty_cfg, B460800);
- break;
- default:
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- }
- switch(nBits) {
- case :
- tty_cfg.c_cflag |= CS7;
- break;
- case :
- tty_cfg.c_cflag |= CS8;
- break;
- }
- switch(nEvent) {
- case '': /* 奇校验 */
- tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
- tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
- tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
- break;
- case 'E': /*偶校验*/
- tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
- tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
- tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
- break;
- case 'N': /*无奇偶校验*/
- tty_cfg.c_cflag &= ~PARENB;
- break;
- }
- /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
- if(nStop == )
- tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
- else if( nStop == )
- tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
- /* flow control option */
- tty_cfg.c_cflag |= CRTSCTS;
- /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
- tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
- tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
- tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
- /*激活配置使其生效*/
- if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
- printf("com set error");
- exit();
- }
- return ;
- }
- void dump_data(char *buf)
- {
- int i;
- for(i=; i<TEST_LEN; i++) {
- if(i% == )
- printf("\n");
- printf("0x%x, ", buf[i]);
- }
- }
- char buff[TEST_LEN];
- int main(int argc, char *argv[])
- {
- int fd=;
- int cnt=;
- int sum=;
- static int num=;
- char *p=NULL;
- parse_cmd(argc, argv);
- printf("RX: dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
- fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
- if(fd < ) {
- printf("Can't Open %s\n", dev_name);
- return -;
- }
- set_serial(fd, baud_rate, , 'N', );
- sleep();
- memset(buff, 0x0, TEST_LEN);
- //printf("start recv: %ds\n", time(NULL));
- p = &buff[];
- while() {
- cnt = read(fd, p, TEST_LEN);
- if(cnt <= ) {
- //sleep(1);
- continue;
- }
- sum += cnt;
- if(sum >= TEST_LEN)
- break;
- p += cnt;
- printf("RX%d: cnt = %d, sum = %d\n", num++, cnt, sum);
- }
- printf("recv %d: %ds\n", sum, time(NULL));
- p = NULL;
- close(fd);
- //dump_data(buff);
- return ;
- }
UART 串口示例代码的更多相关文章
- Linux下读写UART串口的代码
Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下- 两个实用的函数- //////////////// ...
- Arduino - 串口操作函数与示例代码大全
来源:https://blog.csdn.net/iracer/article/details/50334041 Arduino - 串口操作函数与示例代码大全 本文总结了Arduino常用串口操作函 ...
- (三) UART 串口通讯
UART : university asynchronous receiver and transmitter UART // 通用异步接收器和发送器 为什么要有串口:因为许多嵌入式设备没有显示屏 ...
- 【C51】UART串口通信
我们常需要单片机和其他模块进行通信,数据传输,常用的方式就是串口通信技术. 常用来 单片机<-->电脑, 单片机<-->单片机之间通信. 串行通信 versus 并行通信 并 ...
- uart串口协议
uart串口协议 /* USART Word Length ---------------------------------------------------------*/ US ...
- 第十六章 IIC协议详解+UART串口读写EEPROM
十六.IIC协议详解+Uart串口读写EEPROM 本文由杭电网友曾凯峰根据小梅哥FPGA IIC协议基本概念公开课内容整理并最终编写Verilog代码实现使用串口读写EEPROM的功能. 以下为原文 ...
- linux UART串口驱动开发文档
转:http://www.360doc.com/content/10/0417/18/829197_23519037.shtml linux UART串口驱动开发文档时间:2010-01-09 14: ...
- 基于STM32之UART串口通信协议(四)Printf发送
一.前言 1.简介 前面在UART发送中已经讲解过如何调用HAL库的HAL_UART_Transmit函数来实现串口发送,而在调用这个函数来实现串口发送的话,但是在发送数据或者字符的时候,需要将数据或 ...
- 基于STM32之UART串口通信协议(二)发送
一.前言 1.简介 在上一篇UART详解中,已经有了关于UART的详细介绍了,也有关于如何使用STM32CubeMX来配置UART的操作了,而在该篇博客,主要会讲解一下如何实现UART串口的发送功能. ...
随机推荐
- elasticsearch中mapping的_source和store的笔记(转)
原文地址: https://www.cnblogs.com/zklidd/p/6149120.html 0.故事引入 无意中看到了ES的mapping中有store字段,作为一个ES菜鸡,有必要对这个 ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器软件实现的多码率视频点播功能说明
关于EasyDSS EasyDSS(http://www.easydss.com)流媒体解决方案采用业界优秀的流媒体框架模式设计,服务运行轻量.高效.稳定.可靠.易维护,支持RTMP直播.RTMP推送 ...
- pdf转换成jpg不清晰怎么办
用Photoshop打开pdf文件,然后用“文件”-“另存为”“JPG”.
- linux内存管理swap分区
一.什么是linux的内存机制? 我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成,而内存是有限的,这样就引出了物理内存与虚拟内存的概念. 物 ...
- 【C/C++开发】模板类
1.模板的概念 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数需要 ...
- (IO模型介绍,阻塞IO,非阻塞IO,多路复用IO,异步IO,IO模型比较分析,selectors模块,垃圾回收机制)
参考博客: https://www.cnblogs.com/xiao987334176/p/9056511.html 内容回顾 协程实际上是一个线程,执行了多个任务,遇到IO就切换 切换,可以使用yi ...
- SQL - 外链接和内连接
外链接和内连接: leetcode 题目:编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息: 第一次的答案:(错误) select ...
- Appium 中使用 pressKeyCode 方法不起作用也没有报错
为了使 appium 支持 Android 系统 7 及以上,automationName 使用了 UIAutomator2.但是发现,使用androidDriver.pressKeyCode(And ...
- C++ 智能指针 shared_ptr 分析
引文: C++对指针的管理提供了两种解决问题的思路: 1.不允许多个对象管理一个指针 2.允许多个对象管理一个指针,但仅当管理这个指针的最后一个对象析构时才调用delete ps:这两种思路的共同点就 ...
- 【LeetCode】 #7:反转整数 C语言
目录 题目 思路 初步想法 进一步想法 总结 最近打算练习写代码的能力,所以从简单题开始做. 大部分还是用C语言来解决. @(解法) 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数 ...