/* 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 串口示例代码的更多相关文章

  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. EVE-NG使用手册

    转裁于https://www.cnblogs.com/51yuki/articles/eve01.html EVE-NG使用手册   一)EVE-NG的安装 1)下载EVE镜像包 https://pa ...

  2. cube-slide组件的应用

    <template> <div> <cube-slide :data="items"/> </div> </template& ...

  3. 【Docker学习之一】初始Docker

    一.云计算的概念 PaaS(Platform-as-a-Service:平台即服务),把应用服务的运行和开发环境作为一种服务.SaaS(Software-as-a-Service),意思为软件即服务, ...

  4. docker swarm集群搭建及使用Portainer、shipyard

    一.规划 1.swarm01作为manager节点,swarm02和swarm03作为worker节点. # cat /etc/hosts 127.0.0.1   localhost 192.168. ...

  5. Python Tkinter的学习

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macinto ...

  6. Linux下使用matlab在后台默默的运行.m文件(无界面形式)

    Linux下使用matlab在后台默默的运行.m文件(无界面形式)本主在Ubuntu18.04LTS上已经安装了matlab直接运行Matlab$ matlab会启动 matlab,出现启动界面但想要 ...

  7. 005 SpringCloud 学习笔记01-----系统架构的演变

    1.系统架构的演变 随着互联网的发展,网站应用的规模不断扩大.需求的激增,带来的是技术上的压力.系统架构也因此不断的演进.升级.迭代.从单一应用,到垂直拆分,到分布式服务,到SOA,以及现在火热的微服 ...

  8. Python学习之路:通过分片的方式修改列表的技巧(拓展知识)

    一.为列表添加值 用分片的方式可以在列表的头部和尾部添加值 1.在列表的头部添加值 x = [1, 2, 3] #创建列表x x[:0] = [0] #用分片的方式在列表头部添加值 print(x) ...

  9. c#学习笔记2-委托

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  10. appium 方法整理

    1.contexts contexts(self):     Returns the contexts within the current session.     返回当前会话中的上下文,使用后可 ...