linux 串口测试指令和测试程序

一、串口设备查看

查看串口 (/dev)

ls /dev/tty*

在这里插入图片描述
在这里插入图片描述

查看串口(或串口终端)属性 ( /proc)

cat /proc/tty/driver/serial                                                                                                              或
cat /proc/tty/drivers

在这里插入图片描述
在这里插入图片描述

二、串口自测指令

测试打印口

echo haha > /dev/ttyS0

在这里插入图片描述
在这里插入图片描述

三、串口测试参考程序

连接方式:以nuc980开发板为例,将串口1和串口2的收、发线交叉连接。
nuc980官方uart测试程序

/*****************************************************************************                                                                          ** Copyright (c) 2014 Nuvoton Technology Corp. All rights reserved.         **                                                                          *****************************************************************************//***************************************************************************** * FILENAME*     uart_test.c** VERSION*     1.0** DESCRIPTION*     This is the test program used to test the UARTs on NUC980 EV board** DATA STRUCTURES*     None** FUNCTIONS*     None** HISTORY*     ** REMARK*     None****************************************************************************/
#include     <stdio.h>
#include     <stdlib.h>
#include     <unistd.h> 
#include     <sys/types.h> 
#include     <sys/stat.h> 
#include     <fcntl.h> 
#include     <termios.h>  
#include     <errno.h>
#include     <string.h>
#include 	<signal.h>
#include    <pthread.h>#define FALSE 0
#define TRUE  1int fd[2];pthread_t threads[10];char buff[101];static struct termios newtios,oldtios; /*termianal settings */
static int saved_portfd=-1;            /*serial port fd */static void reset_tty_atexit(void)
{if(saved_portfd != -1){tcsetattr(saved_portfd,TCSANOW,&oldtios);} 
}/*cheanup signal handler */
static void reset_tty_handler(int signal)
{if(saved_portfd != -1){tcsetattr(saved_portfd,TCSANOW,&oldtios);}_exit(EXIT_FAILURE);
}static int open_port(const char *portname)
{struct sigaction sa;int portfd;printf("opening serial port:%s\n",portname);/*open serial port */if((portfd=open(portname,O_RDWR | O_NOCTTY)) < 0 ){printf("open serial port %s fail \n ",portname);return portfd;}/*get serial port parnms,save away */tcgetattr(portfd,&newtios);memcpy(&oldtios,&newtios,sizeof newtios);/* configure new values */cfmakeraw(&newtios); /*see man page */newtios.c_iflag |=IGNPAR; /*ignore parity on input */newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL); newtios.c_cflag = CS8 | CLOCAL | CREAD;newtios.c_cc[VMIN]=1; /* block until 1 char received */newtios.c_cc[VTIME]=0; /*no inter-character timer *//* 115200 bps */cfsetospeed(&newtios,B115200);cfsetispeed(&newtios,B115200);/* register cleanup stuff */atexit(reset_tty_atexit);memset(&sa,0,sizeof sa);sa.sa_handler = reset_tty_handler;sigaction(SIGHUP,&sa,NULL);sigaction(SIGINT,&sa,NULL);sigaction(SIGPIPE,&sa,NULL);sigaction(SIGTERM,&sa,NULL);/*apply modified termios */saved_portfd=portfd;tcflush(portfd,TCIFLUSH);tcsetattr(portfd,TCSADRAIN,&newtios);return portfd;
}void * process1(void* arg)
{int portfd = (int) arg;unsigned char i, j;int rev1, rev2;char RxBuffer[101];	rev1 =0;rev2 =0;while(rev2 < 100){rev1 = write(portfd,(buff+rev2),100);rev2 += rev1;}printf("\n uart1 send %d byts\n", rev2);rev1 = 0;rev2 = 0;while(rev2 < 100){rev1 = read(portfd,(RxBuffer+rev2),100);rev2 += rev1;}printf("\n uart1 receive %d bytes\n", rev2);for(i = 0; i < 100; i++){if(i != RxBuffer[i]){printf("\n uart1 compare Error!!");while(1);}}printf("\n uart1 compare correct!!\n");printf("\n uart1 test done!!\n");}	void * process2(void* arg)
{int portfd = (int) arg;unsigned char i, j;int rev1, rev2;char RxBuffer[101];rev1 =0;rev2 =0;while(rev2 < 100){rev1 = write(portfd,(buff+rev2),100);rev2 += rev1;}printf("\n uart2 send %d bytes \n", rev2);rev1 = 0;rev2 = 0;while(rev2 < 100){rev1 = read(portfd,(RxBuffer+rev2),100);rev2 += rev1;}printf("\n uart2 receive %d bytes \n", rev2);for(i = 0; i < 100; i++){if(i != RxBuffer[i]){printf("\n uart2 compare Error!!");while(1);}}printf("\n uart2 compare correct!!\n");printf("\n uart2 test done!!\n");}/**
*@breif 	main()
*/
int main(int argc, char **argv)
{char *dev[10]={"/dev/ttyS1", "/dev/ttyS2"};unsigned int i;printf("\n demo uart1/uart2 external loop back function \n");for(i = 0; i < 100; i++){buff[i] = (i & 0xff);}for(i = 0; i < 2; i++){if((fd[i] = open_port(dev[i]))<0)return -1;}pthread_create(&threads[0], NULL, process1, (void*)(fd[0]));pthread_create(&threads[1], NULL, process2, (void*)(fd[1]));pthread_join(threads[0], NULL);pthread_join(threads[1], NULL);return 0;
}

Makefile文件

.SUFFIXES : .x .o .c .sCC := arm-linux-gcc
STRIP := arm-linux-stripTARGET = uart_demo
SRCS := uart.c
LIBS = -lpthread -lc -lgccall:$(CC) -static $(SRCS) -o $(TARGET) $(LIBS)$(STRIP) $(TARGET)clean:rm -f *.orm -f *.xrm -f *.flatrm -f *.maprm -f temprm -f *.imgrm -f $(TARGET)rm -f *.gdb

测试结果
在这里插入图片描述

nuc980官方rs485测试程序

/** Copyright (c) 2014 Nuvoton technology corporation* All rights reserved.** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.**/#include     <stdio.h>
#include     <stdlib.h>
#include     <unistd.h> 
#include     <sys/types.h>
#include     <sys/stat.h> 
#include     <fcntl.h> 
#include     <termios.h>  
#include     <errno.h>
#include     <string.h>
#include 	<signal.h>
#include    <pthread.h>
#include <linux/serial.h>#define FALSE 0
#define TRUE  1/* Driver-specific ioctls: ...\linux-3.10.x\include\uapi\asm-generic\ioctls.h */
#define TIOCGRS485      0x542E
#define TIOCSRS485      0x542Fint fd[2];pthread_t threads[10];char buff[256];static struct termios newtios,oldtios; /*termianal settings */
static int saved_portfd=-1;            /*serial port fd *//* Test GCC version, this structure is consistent in GCC 4.8, thus no need to overwrite */
#if (__GNUC__ == 4 && __GNUC_MINOR__ == 3)struct my_serial_rs485
{unsigned long	flags;			/* RS485 feature flags */#define SER_RS485_ENABLED		(1 << 0)	/* If enabled */#define SER_RS485_RTS_ON_SEND		(1 << 1)	/* Logical level for RTS pin when sending */#define SER_RS485_RTS_AFTER_SEND	(1 << 2)	/* Logical level for  RTS pin after sent*/#define SER_RS485_RX_DURING_TX		(1 << 4)unsigned long	delay_rts_before_send;	/* Delay before send (milliseconds) */unsigned long	delay_rts_after_send;	/* Delay after send (milliseconds) */unsigned long	padding[5];		/* Memory is cheap, new structs  are a royal PITA .. */
};#endifstatic void reset_tty_atexit(void)
{if(saved_portfd != -1){tcsetattr(saved_portfd,TCSANOW,&oldtios);} 
}/*cheanup signal handler */
static void reset_tty_handler(int signal)
{if(saved_portfd != -1){tcsetattr(saved_portfd,TCSANOW,&oldtios);}_exit(EXIT_FAILURE);
}static int open_port(const char *portname)
{struct sigaction sa;int portfd;
#if (__GNUC__ == 4 && __GNUC_MINOR__ == 3)struct my_serial_rs485 rs485conf;struct my_serial_rs485 rs485conf_bak;
#elsestruct serial_rs485 rs485conf;struct serial_rs485 rs485conf_bak;
#endif	//printf("opening serial port:%s\n",portname);/*open serial port */if((portfd=open(portname,O_RDWR | O_NOCTTY, 0)) < 0 ){printf("open serial port %s fail \n ",portname);return portfd;}printf("opening serial port:%s\n",portname);/*get serial port parnms,save away */tcgetattr(portfd,&newtios);memcpy(&oldtios,&newtios,sizeof newtios);/* configure new values */cfmakeraw(&newtios); /*see man page */newtios.c_iflag |=IGNPAR; /*ignore parity on input */newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL); newtios.c_cflag = CS8 | CLOCAL | CREAD;newtios.c_cc[VMIN]=1; /* block until 1 char received */newtios.c_cc[VTIME]=0; /*no inter-character timer *//* 115200 bps */cfsetospeed(&newtios,B9600);cfsetispeed(&newtios,B9600);/* register cleanup stuff */atexit(reset_tty_atexit);memset(&sa,0,sizeof sa);sa.sa_handler = reset_tty_handler;sigaction(SIGHUP,&sa,NULL);sigaction(SIGINT,&sa,NULL);sigaction(SIGPIPE,&sa,NULL);sigaction(SIGTERM,&sa,NULL);/*apply modified termios */saved_portfd=portfd;tcflush(portfd,TCIFLUSH);tcsetattr(portfd,TCSADRAIN,&newtios);if (ioctl (portfd, TIOCGRS485, &rs485conf) < 0) {/* Error handling.*/ printf("ioctl TIOCGRS485 error.\n");}/* Enable RS485 mode: */rs485conf.flags |= SER_RS485_ENABLED;/* Set logical level for RTS pin equal to 1 when sending: */rs485conf.flags |= SER_RS485_RTS_ON_SEND;//rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;/* set logical level for RTS pin equal to 0 after sending: */ rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);//rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);/* Set rts delay after send, if needed: */rs485conf.delay_rts_after_send = 0x80;if (ioctl (portfd, TIOCSRS485, &rs485conf) < 0){/* Error handling.*/ printf("ioctl TIOCSRS485 error.\n");}if (ioctl (portfd, TIOCGRS485, &rs485conf_bak) < 0){/* Error handling.*/ printf("ioctl TIOCGRS485 error.\n");}else{printf("rs485conf_bak.flags 0x%x.\n", rs485conf_bak.flags);printf("rs485conf_bak.delay_rts_before_send 0x%x.\n", rs485conf_bak.delay_rts_before_send);printf("rs485conf_bak.delay_rts_after_send 0x%x.\n", rs485conf_bak.delay_rts_after_send);}return portfd;
}void * process1(void* arg)
{int portfd = (int) arg;unsigned int i, j;int rev1, rev2;char RxBuffer[sizeof(buff)];	rev1 =0;rev2 =0;while(rev2 < sizeof(buff)){rev1 = write(portfd,(buff+rev2),sizeof(buff));rev2 += rev1;}printf("\n uart1 send %d byts\n", rev2);}	void * process2(void* arg)
{int portfd = (int) arg;unsigned int i, j;int rev1, rev2;char RxBuffer[sizeof(buff)];rev1 = 0;rev2 = 0;while(rev2 < sizeof(RxBuffer)){rev1 = read(portfd,(RxBuffer+rev2),sizeof(RxBuffer) - rev2);rev2 += rev1;if(rev1 > 0)printf("\n uart2 receive %d bytes \n", rev2);}printf("\n uart2 receive %d bytes \n", rev2);for(i = 0; i < sizeof(RxBuffer); i++){if(i != RxBuffer[i]){printf("\n uart2 compare Error!!");while(1);}}printf("\n uart2 compare correct!!\n");printf("\n uart2 test done!!\n");}/**
*@breif 	main()
*/
int main(int argc, char **argv)
{char *dev[10]={"/dev/ttyS1", "/dev/ttyS2"};unsigned int i;printf("\n demo uart1/uart2 external loop back function \n");for(i = 0; i < sizeof(buff); i++){buff[i] = (i & 0xff);}for(i = 0; i < 2; i++){if((fd[i] = open_port(dev[i]))<0)return -1;}pthread_create(&threads[0], NULL, process1, (void*)(fd[0]));pthread_create(&threads[1], NULL, process2, (void*)(fd[1]));pthread_join(threads[0], NULL);pthread_join(threads[1], NULL);return 0;
}

Makefile文件

.SUFFIXES : .x .o .c .sCC := arm-linux-gcc
STRIP := arm-linux-stripTARGET = uart_rs485
SRCS := uart.c
LIBS = -lpthread -lc -lgccall:$(CC) -static $(SRCS) -o $(TARGET) $(LIBS)$(STRIP) $(TARGET)clean:rm -f *.orm -f *.xrm -f *.flatrm -f *.maprm -f temprm -f *.imgrm -f $(TARGET)rm -f *.gdb

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/293035.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

虾皮跨境电商物流:打造高效便捷的全球供应链解决方案

随着全球化的推进和电子商务的蓬勃发展&#xff0c;跨境电商物流成为了越来越多商家和消费者关注的焦点。虾皮&#xff08;Shopee&#xff09;作为一家领先的电商平台&#xff0c;不仅提供了丰富多样的商品选择&#xff0c;还致力于为卖家和消费者提供高效便捷的跨境电商物流服…

【C语言】指针详解(二)

目录 1.指针变量类型的意义 1.1指针的解引用 1.2指针 - 整数 1.3void*指针 2.const修饰指针 2.1const修饰变量 2.2const修饰指针变量 1.指针变量类型的意义 1.1指针的解引用 指针变量的大小和类型无关&#xff0c;只要是指针变量&#xff0c;在同一个平台下&#xff0…

每日一题——LeetCode844

方法一 暴力法&#xff1a; 对两个字符串分别从头到尾遍历一遍&#xff0c;遇到#就删除#和它之前的那个字符&#xff0c;如果遇到#在字符串的第一位则只用删除#&#xff0c;最后将删除后的不含#的两个字符串进行比较是否一样 var backspaceCompare function(s, t) {for(var …

《Python Advanced Programming + Design Patterns + Clean Code》

清洁代码 — 学习如何编写可读、可理解且可维护的代码 高级Python编程知识 Python之常用设计模式 Advanced Programming装饰器 decorators生成器 & 迭代器with 上下文管理器面向对象Mixin 模式反射机制并发编程 Design Patterns设计模式分类简单工厂模式工厂模式 √抽象工厂…

用C#也能做机器学习?

前言✨ 说到机器学习&#xff0c;大家可能都不陌生&#xff0c;但是用C#来做机器学习&#xff0c;可能很多人还第一次听说。其实在C#中基于ML.NET也是可以做机器学习的&#xff0c;这种方式比较适合.NET程序员在项目中集成机器学习模型&#xff0c;不太适合专门学习机器学习&a…

GNSS技术在城市规划中的革新:精准定位引领智慧城市发展

随着城市化的快速推进&#xff0c;城市规划愈发关键&#xff0c;而全球导航卫星系统&#xff08;GNSS&#xff09;技术的广泛应用正为城市规划带来一场前所未有的变革。本文将深入探讨GNSS模块在城市规划中的多重应用&#xff0c;以及如何通过精准定位推动智慧城市的发展。 城市…

【Python小知识 - 6】:QLabel设置图片

文章目录 QLabel设置图片 QLabel设置图片 from PyQt5.QtWidgets import * from PyQt5.QtGui import * import sysapp QApplication(sys.argv)window QWidget()hbox QHBoxLayout(window)# 设置标签图片 lable QLabel() lable.setPixmap(QPixmap(./img/window.png).scaled(1…

【ARM Cortex-M 系列 5 -- RT-Thread renesas/ra4m2-eco 移植编译篇】

文章目录 RT-Thread 移植编译篇编译os.environ 使用示例os.putenv使用示例python from 后指定路径 编译问题_POSIX_C_SOURCE 介绍编译结果 RT-Thread 移植编译篇 本文以瑞萨的ra4m2-eco 为例介绍如何下载rt-thread 及编译的设置。 RT-Thread 代码下载&#xff1a; git clone …

听GPT 讲Rust源代码--src/tools(22)

File: rust/src/tools/tidy/src/lib.rs rust/src/tools/tidy/src/lib.rs是Rust编译器源代码中tidy工具的实现文件之一。tidy工具是Rust项目中的一项静态检查工具&#xff0c;用于确保代码质量和一致性。 tidy工具主要有以下几个作用&#xff1a; 格式化代码&#xff1a;tidy工具…

12.21_黑马数据结构与算法笔记Java

//最近在复习&#xff0c;&#xff0c;java的进度会比较慢一些 目录 219 排序算法 基数排序2 220 排序算法 java排序 221 排序 e01 根据另一个数组次序排序 222 排序 e02 根据出现频率排序 thinking&#xff1a;关于比较器 223 排序 e03 最大间距 解法1&#xff08;超出内…

【图神经网络 · 科研笔记5】异构信息网络,利用注意力选择元路径;利用进化邻域和社群实现自监督动态图嵌入,交叉监督对比学习;近期科研思维导图小汇总;

记录部分科研文献阅读相关内容【划重点】,主题“图神经网络”,仅学习使用。 🎯作者主页: 追光者♂🔥 🌸个人简介: 📝[1] CSDN 博客专家📝 🏆[2] 人工智能领域优质创作者🏆 🌟[3] 2023年城市之星领跑者TOP1(哈尔滨)🌿 🌿[4] 2022年度…