ubuntu如何测试串口
AB资源网 2023-05-03 14:59 84 浏览 0 评论
#include /*标准输入输出定义*/
#include
#include /*Unix标准函数定义*/
#include /**/
#include /**/
#include /*文件控制定义*/
#include /*PPSIX终端控制定义*/
#include /*错误号定义*/
#include
#include
#define FALSE 1
#define TRUE 0
char *recchr=\"We received:\\\"\";
void print_usage();
int speed_arr[] = {
B921600, B460800, B230400, B115200, B57600, B38400, B19200,
B9600, B4800, B2400, B1200, B300,
};
int name_arr[] = {
921600, 460800, 230400, 115200, 57600, 38400, 19200,
9600, 4800, 2400, 1200, 300,
};
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror(\"tcsetattr fd1\");
return;
}
tcflush(fd,TCIOFLUSH);
}
if (i == 12){
printf(\"\\tSorry, please set the correct baud rate!\\n\\n\");
print_usage(stderr, 1);
}
}
/*
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值 为 7 或者8*
*@param stopbits 类型 int 停止位 取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror(\"SetupSerial 1\");
return(FALSE);
}
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 (FALSE);
}
switch (parity) {
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 (FALSE);
}
/* 设置停止位*/
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,\"Unsupported stop bits\\n\");
return (FALSE);
}
/* Set input parity option */
if (parity != \'n\')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
options.c_lflag &= ~(ECHO | ICANON);
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror(\"SetupSerial 3\");
return (FALSE);
}
return (TRUE);
}
/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd) { /*设置数据位数*/
perror(\"Can\'t Open Serial Port\");
return -1;
} else
return fd;
}
/* The name of this program */
const char * program_name;
/* Prints usage information for this program to STREAM (typically
* stdout or stderr), and exit the program with EXIT_CODE. Does not
* return.
*/
void print_usage (FILE *stream, int exit_code)
{
fprintf(stream, \"Usage: %s option [ dev... ] \\n\", program_name);
fprintf(stream,
\"\\t-h --help Display this usage information.\\n\"
\"\\t-d --device The device ttyS[0-3] or ttySCMA[0-1]\\n\"
\"\\t-b --baudrate Set the baud rate you can select\\n\"
\"\\t [230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300]\\n\"
\"\\t-s --string Write the device data\\n\");
exit(exit_code);
}
/*
*@breif main()
*/
int main(int argc, char *argv[])
{
int fd, next_option, havearg = 0;
char *device;
int i=0,j=0;
int nread; /* Read the counts of data */
char buff[512]; /* Recvice data buffer */
pid_t pid;
char *xmit = \"1234567890\"; /* Default send data */
int speed ;
const char *const short_options = \"hd:s:b:\";
const struct option long_options[] = {
{ \"help\", 0, NULL, \'h\'},
{ \"device\", 1, NULL, \'d\'},
{ \"string\", 1, NULL, \'s\'},
{ \"baudrate\", 1, NULL, \'b\'},
{ NULL, 0, NULL, 0 }
};
program_name = argv[0];
do {
next_option = getopt_long (argc, argv, short_options, long_options, NULL);
switch (next_option) {
case \'h\':
print_usage (stdout, 0);
case \'d\':
device = optarg;
havearg = 1;
break;
case \'b\':
speed = atoi(optarg);
break;
case \'s\':
xmit = optarg;
havearg = 1;
break;
case -1:
if (havearg) break;
case \'?\':
print_usage (stderr, 1);
default:
abort ();
}
}while(next_option != -1);
sleep(1);
fd = OpenDev(device);
if (fd > 0) {
set_speed(fd, speed);
} else {
fprintf(stderr, \"Error opening %s: %s\\n\", device, strerror(errno));
exit(1);
}
if (set_Parity(fd,8,1,\'N\')== FALSE) {
fprintf(stderr, \"Set Parity Error\\n\");
close(fd);
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, \"Error in fork!\\n\");
} else if (pid == 0){
while(1) {
printf(\"%s SEND: %s id %d\\n\",device, xmit,i);
write(fd, xmit, strlen(xmit));
sleep(1);
i++;
}
exit(0);
} else {
while(1) {
nread = read(fd, buff, sizeof(buff));
if (nread > 0) {
buff[nread] = \'\\0\';
printf(\"%s RECV %d total\\n\", device, nread);
printf(\"%s RECV: %s\\n\", device, buff);
}
}
}
close(fd);
exit(0);
}
相关推荐
- 云主机FTP软件:高效传输与安全管理的一站式解决方案
-
在云计算时代,云主机已成为企业和个人用户托管应用和存储数据的首选。为了方便文件传输,FTP(文件传输协议)软件在云主机环境中扮演着重要角色。本文将详细介绍如何在云主机上配置和使用FTP软件...
- 云主机FP:引领未来计算,解锁无限可能
-
云主机FP(FloatingPoint)是指在云计算环境中,针对浮点运算性能进行优化的虚拟机实例。浮点运算在科学计算、工程模拟、金融建模、图形处理等领域中占据重要地位,因此云主机FP的设计和配置...
- 云主机ECS:解锁企业数字化转型的新引擎,高效、安全、灵活的云计算解决方案
-
云主机ECS(ElasticComputeService)是阿里云提供的一种弹性计算服务,它允许用户在云端创建和管理虚拟机实例。ECS的核心优势在于其灵活性和可扩展性,能够满足各种规模和类型的业...
- 云主机D盘:解锁无限存储空间,轻松应对大数据挑战!
-
云主机是一种基于云计算技术的虚拟化服务器,它允许用户在云平台上创建、配置和管理虚拟机实例。在云主机中,磁盘分区是存储数据的关键部分,通常包括系统盘和数据盘。系统盘用于安装操作系统和运行应用...
- 云主机DNS解析:提升网站速度与稳定性的关键策略
-
云主机DNS(DomainNameSystem)是云计算环境中至关重要的一部分,它负责将域名转换为IP地址,从而使得用户能够通过易于记忆的域名访问云主机上的服务和应用。本文将深入探讨云主机DNS...
- 云主机C盘爆满?快速解决方法大揭秘,让你的服务器重获新生!
-
云主机C盘满了是一个常见但棘手的问题,尤其对于依赖云服务进行日常运营的企业和个人用户来说,这可能导致系统性能下降、应用程序崩溃,甚至数据丢失。本文将详细探讨云主机C盘满的原因、影响以及解决方法。...
- 云主机CPU选择指南:提升性能与效率的关键决策
-
在选择云主机的CPU时,用户需要考虑多个因素,以确保所选的CPU能够满足其应用的需求,同时优化成本效益。以下是一些关键点,帮助用户在云主机CPU选择过程中做出明智的决策。了解应用的性能需求...
- 云主机CPU性能大比拼:揭秘顶级云服务商的核心竞争力
-
云主机CPU是云计算环境中至关重要的组成部分,它直接影响着云服务的性能、稳定性和用户体验。CPU,即中央处理器,是计算机系统的核心,负责执行指令和处理数据。在云主机中,CPU的性能决定了虚...
- 云主机ASP:高效搭建动态网站,轻松实现业务扩展与性能优化
-
云主机ASP(ActiveServerPages)是一种在云环境中运行ASP应用程序的技术。ASP是一种由微软开发的动态网页技术,允许开发者使用VBScript或JScript等脚本语言编写服务...
- 云主机API:解锁无限可能,引领企业数字化转型新纪元
-
云主机API(ApplicationProgrammingInterface)是云计算服务提供商为用户提供的一种编程接口,允许开发者通过编程方式管理和操作云主机资源。这些API通常基于RESTf...
- 云主机99idc:高效稳定,轻松搭建您的专属云端空间,一键部署,畅享无限可能!
-
云主机99idc是一家专注于提供云计算服务的公司,其核心业务是为企业和个人用户提供高性能、高可靠性的云主机服务。随着数字化转型的加速,云计算已经成为企业IT基础设施的重要组成部分,而云主机99i...
- 云主机80端口:解锁无限可能,开启高效网络新时代!
-
云主机是一种基于云计算技术的虚拟化服务器,它通过互联网提供计算资源和服务。在云主机中,80端口是一个非常重要的端口,通常用于HTTP协议,即网页服务。本文将详细探讨云主机80端口的相关内容...
- 云主机403错误:解锁高效解决方案,提升网站性能与安全
-
云主机403错误是一个常见的网络问题,通常表示用户在尝试访问某个资源时被服务器拒绝。这种错误可能由多种原因引起,包括权限问题、配置错误、防火墙设置等。以下是关于云主机403错误的一些详细信...
- 云主机360:全方位云端解决方案,助力企业数字化转型无忧
-
云主机360是一种基于云计算技术的虚拟化服务器解决方案,它通过将物理服务器资源虚拟化,为用户提供灵活、高效、安全的计算服务。云主机360的核心优势在于其高度的可扩展性和弹性,用户可以根据业务需求...
- 云主机301:引领未来云计算的新纪元,高效稳定,助力企业数字化转型!
-
云主机301是一种常见的网络重定向状态码,通常用于指示用户请求的资源已被永久移动到新的URL。在云计算环境中,云主机301状态码的出现可能涉及到多种技术和管理策略,下面我们将详细探讨这一现象。...
你 发表评论:
欢迎- 一周热门
-
-
HostYun廉价洛杉矶三网回程CN2 GIA云服务器内测13元/月起(美国原生IP,去程10Gbps防御)
-
大网数据:双12秒杀聚惠,湖北100G高防云低至0元/月,湖北独服务器低至210元、200G高防+50Mbps带宽
-
HostYun洛杉矶大硬盘云服务器9折22.5元/月起(240G-500G硬盘/1Gbps/10G防御)
-
樊云香港双程CN2及洛杉矶50G高防三网CN2 GIA云服务器9折22.5元/月起
-
大网数据、湖北高防云服务器低至39元/月起、湖北高防独服务器低至245元起(200G硬防、金盾+傲盾防CC)
-
spinservers圣何塞/达拉斯10Gbps带宽高配服务器月付89美元起
-
tmhhost美国高防云服务器8折_CeRaNetworks机房/三网cn2直连/适合建站
-
高防服务器大网数据湖北独服务器低至210元、200G高防+50Mbps带宽
-
DogYun新上韩国独立服务器,E5/SSD+NVMe优惠后300元/月,自动化上架
-
初忆云 – 2020年中云聚惠全场五折 BGP云服务器低至88/年,抓紧上车
-
- 互动交流
- 标签列表
- 最新评论
-
您的文章条理清晰,论述有据,说服力强。您的文章情感真挚,能够触动人心,引起共鸣。https://www.renhehui.com/renhehui/1479.h
沉醉于月色 评论于:08-09虚拟机部署好后跟物理机一样当服务器的,只是它依赖了本地物理机不要关机为前提。对于外网访问内网场景,本地内网搭建服务器后需要提供到互联网上连接访问的,比较简便的
访客 评论于:03-01刘中宜 评论于:11-01
访客 评论于:06-03
AB资源网 评论于:05-08
AB资源网 评论于:11-22
AB资源网 评论于:11-22
頹廢了悲伤 评论于:11-15
南风知我意 评论于:11-15
心若冰凝 评论于:11-15