UART 接口测试「建议收藏」

UART 接口测试「建议收藏」串口UART测试程序带传参波特率、奇偶校验、停止位、数据位#include<stdio.h>#include<stdlib.h>#include<string.h>#include<pthread.h>#include<fcntl.h>#include<errno.h>#include<std…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

UART 接口测试

一、系统下串口测试

1.minicom

minicom -s

组合键 Ctrl+a 进入设置状态
按z打开帮助菜单,或者直接输入菜单对应的字母即可
S键:发送文件到目标系统中
W键:自动卷屏。当显示的内容超过一行之後,自动将後面的内容换行
C键:清除屏幕的显示内容
B键:浏览minicom的历史显示
X键:退出minicom

2.stty命令

stty -F /dev/ttyS0 -a  				查看串口参数
stty -F /dev/ttyS0 ispeed 115200 ospeed 115200 cs8 设置串口参数
cat /dev/ttyS0 						打印串口数据
echo “hello word” >  /dev/ttyS0   	向串口发送数据

3.microcom

microcom /dev/ttyS0   ctrl+x打断

二、串口UART测试程序

带传参波特率、奇偶校验、停止位、数据位

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>

typedef  unsigned int uint32_t ; 
static speed_t speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B600, B300};
static int      name_arr[] = { 230400,  115200,  57600,  38400,  19200,  9600,  4800,  2400,  1200, 600,  300};

static int uart_fd;

/**
 *@brief  设置串口通信速率
 *@param  fd     类型 int  打开串口的文件句柄
 *@param  speed  类型 int  串口速度
 *@return  void
 */
static void set_speed(int fd, int speed){
	uint32_t  i; 
	struct termios opt;

	tcgetattr(fd, &opt); 
	tcflush(fd, TCIOFLUSH);     
	cfmakeraw(&opt);
#if 1
	for(i= 0; i < sizeof(speed_arr)/sizeof(speed_t); i++) { 
		if  (speed == name_arr[i]) {     
			printf("serial speed=%d ", speed);
			cfsetispeed(&opt, speed_arr[i]);  
			cfsetospeed(&opt, speed_arr[i]);   
		}    
	}
#else
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
#endif
    if (tcsetattr(fd, TCSANOW, &opt) == -1) {
            printf("tcsetattr(): %s", strerror(errno));
            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
 */
static int set_parity(int fd, int speed, int databits,char *parity,int stopbits)
{ 
    set_speed(fd,speed);

	struct termios options; 
	if  ( tcgetattr( fd,&options)  !=  0) { 
		perror("SetupSerial 1");     
		return -1;  
	}
	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 -1;  
	}
	printf("databits=%d ",databits);
	switch (parity[0]) 
	{   
		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 -1;  
	}  
	printf("parity=%c ",parity[0]);
	/* 设置停止位*/  
	switch (stopbits)
	{   
		case 1:    
			options.c_cflag &= ~CSTOPB;  
			break;  
		case 2:    
			options.c_cflag |= CSTOPB;  
			break;
		default:    
			fprintf(stderr,"Unsupported stop bits\n");  
			return -1; 
	} 
	printf("stopbits=%d\n",stopbits);
	/* Set input parity option */ 
	if (parity[0] != '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 -1;  
	} 
	return 0;  
}


static void uart_init(char * pserial_dev, int speed, int databits, char *parity, int stopbits)
{
	int ret = 0;
    printf("dev name is: %s \r\n", pserial_dev);
    uart_fd = open(pserial_dev, O_RDWR | O_NOCTTY);
    if (uart_fd < 0) {
        printf("open(): %s\r\n", strerror(errno));
        exit(1);
    }
    //set_parity(uart_fd, speed, 8, 'O', 1);
    ret = set_parity(uart_fd, speed, databits, parity, stopbits);
	
	if(ret) {
        printf("\noperating error!\r\n");
		close(uart_fd);
        exit(1);
	}
}

static int test_send(char * file_name)
{
    char buf[1024];
    int  fd, ret, tmp;
    fd = open(file_name, O_RDONLY);
	if(fd < 0) {
		printf("open %s faild!\n",file_name);
		return -1;
	}
    while(1) {
		ret = 0;
		tmp = 0;
		ret = read(fd, buf, sizeof(buf)); 
        if(ret <= 0) {
			//printf("read error!\n");
            break;
		}
		do {
			tmp += write(uart_fd, buf+tmp, ret-tmp);
		} while(tmp != ret);
    }
	close(fd);

    return 0;
}

static int test_receive(char * file_name)
{
	char buf[1024];
    int  fd, ret, tmp;
    fd = open(file_name,O_WRONLY|O_TRUNC|O_CREAT,0666);
	if(fd < 0) {
		printf("open %s faild!\n",file_name);
		return -1;
	}
    while(1) {
		ret = 0;
		tmp = 0;
		ret = read(uart_fd, buf, sizeof(buf)); 
        if(ret <= 0) {
			printf("read error!\n");
            break;
		}
		do {
			tmp += write(fd, buf+tmp, ret-tmp);
		} while(tmp != ret);
    }
	close(fd);
    return 0;
}

void print_info(void)
{
	int i;
	printf("\n./test_uart /dev/ttyX speed databits parity stopbits file-name transport\n\n");
	printf("	/dev/ttyX	/dev/ttySn or /dev/ttyUSBn\n");
	printf("	speed		");
	for(i=0;name_arr[i];i++) {
		printf("%d ",name_arr[i]);
	}
	printf("\n");
	printf("	databits	8	7\n");
	printf("	parity		n	o	e\n");
	printf("	stopbits	1	2\n");
	printf("	file-name	./1.log\n");
	printf("	transport	send	receive\n");	
	printf("\n	E.g# ./test_uart /dev/ttyS1 115200 8 n 1 1.log receive\n\n");
}

int main(int argc,char *argv[])
{
	char * parity;
	int speed, databits, stopbits;
	if(argc != 8) {
		print_info();
        exit(1);
	}
	parity	 = argv[4];
	speed    = atoi(argv[2]);
	databits = atoi(argv[3]);
	stopbits = atoi(argv[5]);
	
	uart_init(argv[1],speed,databits,parity,stopbits);
	
	if(!strcmp(argv[7],"send")) {
		test_send(argv[6]);
	} else if(!strcmp(argv[7],"receive")) {
		test_receive(argv[6]);
	}else{
		print_info();
	}
	close(uart_fd);
	return 0;
}

1.测试接受

	将串口ttyS1接受到的数据保存在1.log文件里
	./uart_text /dev/ttyS1 115200 8 n 1 1.log receive

2.测试发送

	将文件1.log内容发送到串口ttyS1上
	./uart_text /dev/ttyS1 115200 8 n 1 1.log send  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193848.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • webAR涉及的技术「建议收藏」

    webAR涉及的技术「建议收藏」1.技术体系1.1技术体系整理其中绿色底色的代表Demo中表现出的能力比较成熟,可以直接应用。脑图地址:http://naotu.baidu.com/file/3392a895a90397252

    2022年8月5日
  • vue 正则表达式验证_vue表单自定义验证

    vue 正则表达式验证_vue表单自定义验证首先需要自己写正则表达式,正则学得不好就不乱指点了=>方法1如果你是单独建的js文件先引入正则表达式文件才进行下一步或者直接在组件内写正则2data({constregExpID=(rule,value,callback)=>{//regExpID自定义类名if(value===”){//value验证的值不要更改会自动匹配你所需要验证的值ca…

    2022年10月22日
  • 磁盘阵列恢复方法以及注意事项

    磁盘阵列恢复方法以及注意事项想要弄清楚磁盘阵列恢复,首先就得知道什么是磁盘阵列,磁盘阵列多用于存储服务器,数据服务器等企业级大数据存储领域,磁盘阵列是把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,当磁盘瘫痪或硬件损坏后,为了恢复存储在阵列平台的数据被称之为磁盘阵列数据恢复,而磁盘阵列能够提供比单个硬盘有着更高的性能和提供数据冗余的技术。  对于做数据恢复人员来讲,最怕的是什么,那就是在客户发现数据丢失后,没有停

    2022年4月29日
  • Batchnorm原理详解「建议收藏」

    Batchnorm原理详解「建议收藏」Batchnorm原理详解前言:Batchnorm是深度网络中经常用到的加速神经网络训练,加速收敛速度及稳定性的算法,可以说是目前深度网络必不可少的一部分。本文旨在用通俗易懂的语言,对深度学习的常用算法–batchnorm的原理及其代码实现做一个详细的解读。本文主要包括以下几个部分。Batchnorm主要解决的问题 Batchnorm原理解读 Batchnorm的优点 Batc…

    2022年4月29日
  • python执行测试用例_测试用例执行结果有哪些

    python执行测试用例_测试用例执行结果有哪些前言通常我们认为每个测试用例都是相互独立的,因此需要保证测试结果不依赖于测试顺序,以不同的顺序运行测试用例,可以得到相同的结果。pytest默认运行用例的顺序是按模块和用例命名的ASCII编码

    2022年7月29日
  • 二叉树的先序,中序,后序遍历的序列_二叉树先序遍历和后序遍历正好相反

    二叉树的先序,中序,后序遍历的序列_二叉树先序遍历和后序遍历正好相反    二叉树的遍历主要有三种:(1)先(根)序遍历(根左右)(2)中(根)序遍历(左根右)(3)后(根)序遍历(左右根)举个例子:先(根)序遍历(根左右):ABDHEICFJKG中(根)序遍历(左根右):DHBEIAJFKCG后(根)序遍历(左右根):HDIEBJKFGCA    以后(根)序…

    2022年9月14日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号