AB资源网(www.xxab.cn)服务器导航站-找服务器商就上AB资源网
百度360必应搜狗本站头条
【本站公告】:本站互助计划,欢迎有活动的服务器商免费投稿,免费收录,最新收录会在首页展示! - 站长QQ:6502567
当前位置:网站首页 > 技术文档 > 正文

ubuntu如何测试串口

AB资源网 2023-05-03 14:59 57 浏览 0 评论

ubuntu如何测试串口

#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);

}

腾讯云

相关推荐

踏入阿里云服务器代理商之门:步骤和技巧 (怎么成为阿里云服务器代理商)

作为目前全球更大的云计算服务提供商之一,阿里云在中国市场的份额一直占据领导地位。如果你希望在云计算领域开展业务,成为阿里云服务器代理商可能是个不错的选择。本文将为大家详细介绍具体的步骤和技巧,帮助你成...

高性价比,足够优惠! 30美元起,年付美国VPS,值得拥有! (美国vps 年付)

近年来,随着互联网的不断发展,越来越多的网站需要使用虚拟主机服务,以提供更加稳定的服务质量和更好的访问速度。而虚拟主机的使用也因此成为了越来越普遍的一种方式。但是,在选择虚拟主机服务时,除了考虑到稳定...

评测美国云服务器,推荐性价比高的品牌 (美国云服务器哪个好用)

近年来,随着人们对于云计算的需求不断增加,云服务器也逐渐成为了企业、机构和个人等用户参与服务的主要方式之一。而在众多的云服务器品牌中,美国云服务器更是备受欢迎。而对于那些想要评测美国云服务器的用户来说...

群晖服务器的登录方法详解 (群晖服务器如何登陆)

群晖服务器是一种高效可靠的存储和共享平台,它可以提供非常多的实用功能和服务。但是,在开始使用之前,你需要登录到你的群晖服务器。在本文中,我们将详细讲解群晖服务器的登录方法。一、了解群晖服务器的基本概...

紧急通知:CDN服务器可能遇到故障,需及时解决! (cdn服务器可能发生故障)

作为一个网站管理员,CDN服务器的重要性我们再清楚不过了。最近,我们收到了来自网络运营商的紧急通知,通知我们CDN服务器可能遇到故障,需要及时解决。CDN服务器,即内容分发网络服务器,是在全球各个位...

办公室网络瘫痪!无法连接局域服务器怎么办? (连接不了局域服务器)

办公室网络瘫痪是企业中常见的问题之一。一旦网络瘫痪,会影响到员工的工作效率和企业的营运。当你打开电脑,却发现无法连接局域服务器时,这时该如何应对呢?1.检查网络连接检查电缆是否连接正确,网线是否...

利润吗?买云主机,能否带来收益? (买云主机能赚)

随着互联网的飞速发展,云计算作为一种新型的计算模式,其广泛应用在各个领域之中。其中,云主机服务是云计算的重要组成部分,已经成为很多企业选择托管的首选方式。随着云主机的发展和普及,很多人开始关注,如果购...

.NET轻松打开FTP服务器文件夹,方便快捷管理文件 (.net打开ftp服务器文件夹)

在现代科技的浪潮下,越来越多的企业选择使用云服务器来存储和共享数据。FTP服务器是一个非常有用的工具,它允许用户上传、下载、删除和共享文件。然而,FTP管理文件需要一些特定技能和知识,否则操作可能会变...

2023企业服务器版:全面升级,助力企业发展 (2023 企业服务器版)

2023年,微软推出了全新的企业服务器版本,旨在为企业用户提供更为稳定、高效的IT系统支持,更好地助力企业发展。随着互联网和信息技术的飞速发展,企业面临着越来越大的信息化压力。如何建设一个高效、稳定...

享受超低价格!2023年付VPS,轻松搭建私人网站 (2023便宜年付vps)

当今时代,互联网充斥着各种各样的网站,无论是企业还是个人都会拥有一个网站,用来展示自己的产品或者知识。而搭建一个私人网站也是越来越普遍的事情。那么,如何搭建一个便捷而且又不贵的私人网站呢?本文将为大家...

回顾2023服务器系统:经典之作还是过时技术? (2023服务器系统)

2023年,微软公司推出了WindowsServer2023,这是一款非常成功的服务器操作系统,许多企业和机构使用它进行各种任务和应用程序。然而,随着时间的推移,新技术的发展以及安全漏洞的增加,2...

2023 Q3服务器排名发布:领先厂商与新兴品牌争夺冠军 (2023 Q3服务器排名)

近年来,随着互联网技术的迅速发展,服务器市场也持续升温。市场上主要的服务器品牌包括戴尔、惠普、联想、IBM等。而在这些老牌企业的竞争下,新兴的服务器品牌也在不停崛起。根据2023Q3服务器排名发布,...

「低成本高性能!100g云服务器价格惊喜震撼!」 (100g云服务器价格)

低成本高性能!100g云服务器价格惊喜震撼!现今的互联网时代,任何一家公司都需要拥有自己的网站,以便宣传公司产品、服务和品牌。一个高速、可靠的云服务器是每个公司的必备工具之一。就在不久之前,网络服务...

1u服务器显卡:强大性能让服务器效率提升 (1u服务器显卡)

1U服务器显卡:强大性能让服务器效率提升随着信息技术的飞速发展,人们对数据处理和存储的需求越来越高,尤其对企业级服务器的性能要求也越来越苛刻。随着、大数据、云计算等诸多新技术的不断涌现,服务器的效率...

韩国将于2023年推出云服务器服务,助力云计算技术的发展。 (2023韩国云服务器)

韩国将于2023年推出云服务器服务,助力云计算技术的发展随着互联网的高速发展,云计算技术已经成为了数字化时代的一项重要技术。云计算技术是一种以互联网为基础,将不同的底层服务封装成云服务器,以便用户可...

取消回复欢迎 发表评论: