Linux下打开串口设置
给出打开串口函数
- int open_tty(char tty[])
- {
- int fd;
- char tty_path[32]={0};
- sprintf(tty_path,"/dev/%s",tty);
- fd=tty_open_port(tty_path);
- // PORT_SPEED是一个定义的宏,表示传输速率。数据位为8,无校验位,停止位为1
- tty_set_opt(fd,PORT_SPEED,8,'N',1);
- return fd;
- }
该函数接受一个参数,表示你要打开的串口名称,如“ttyS1”,返回串口操作描述符,在调用该函数后,可以根据返回值来判断是否设置成功,如果fd大于0,则返回成功。
该函数还依赖于两个函数,下面也给出(包括用到的头文件)。
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <error.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <termios.h>
- #include <fcntl.h>
- #include <stdlib.h>
- extern int tty_open_port(char *tty_num);
- extern int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
- int tty_open_port(char *tty_num)
- {
- int fd = open (tty_num, O_RDWR | O_NOCTTY | O_NDELAY );
- //阻塞 fcntl(fd,F_SETFL,0) ; //block
- //非阻塞 fcntl(fd,F_SETFL,FNDELAY) ;
- if (fd == -1)
- {
- printf ("Can't Open Serial Port %s !\n",tty_num);
- return -1;
- }
- else
- {
- //将串口设置成非阻塞的操作
- fcntl(fd,F_SETFL,FNDELAY);
- return fd;
- }
- }
- /**************************************************************************************
- * 功 能:set serial port speed
- * 修改历史: 2011.6.29.
- **************************************************************************************/
- int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
- {
- struct termios newtio,oldtio;
- if ( tcgetattr( fd,&oldtio)!=0) {
- perror("SetupSerial 1");
- return -1;
- }
- bzero(&newtio, sizeof( newtio ));
- newtio.c_cflag |= CLOCAL | CREAD;
- newtio.c_cflag &= ~CSIZE; //mask the character size bits
- switch( nBits )
- {
- case 7:
- newtio.c_cflag |= CS7; //data: 7bits
- break;
- case 8:
- newtio.c_cflag |= CS8; //data: 8bits
- break;
- }
- switch( nEvent )
- {
- case 'O':
- newtio.c_cflag |= PARENB;
- newtio.c_cflag |= PARODD;
- newtio.c_iflag |= (INPCK | ISTRIP);
- break;
- case 'E':
- newtio.c_iflag |= (INPCK | ISTRIP);
- newtio.c_cflag |= PARENB;
- newtio.c_cflag &= ~PARODD;
- break;
- case 'N':
- newtio.c_cflag &= ~PARENB;
- break;
- }
- switch( nSpeed ) //set the bps
- {
- case 2400:
- cfsetispeed(&newtio, B2400);
- cfsetospeed(&newtio, B2400);
- break;
- case 4800:
- cfsetispeed(&newtio, B4800);
- cfsetospeed(&newtio, B4800);
- break;
- case 9600:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- case 19200:
- cfsetispeed(&newtio, B19200);
- cfsetospeed(&newtio, B19200);
- break;
- case 115200:
- cfsetispeed(&newtio, B115200);
- cfsetospeed(&newtio, B115200);
- break;
- case 460800:
- cfsetispeed(&newtio, B460800);
- cfsetospeed(&newtio, B460800);
- break;
- default:
- cfsetispeed(&newtio, B9600);
- cfsetospeed(&newtio, B9600);
- break;
- }
- if( nStop == 1 ) //set the 1bit stop
- newtio.c_cflag &= ~CSTOPB;
- else if ( nStop == 2 ) //set the 2bit stop
- newtio.c_cflag |= CSTOPB;
- newtio.c_cc[VTIME] = 0;
- newtio.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH);
- if((tcsetattr(fd,TCSANOW,&newtio))!=0)
- {
- perror("com set error");
- return -1;
- }
- printf("Current serial speed is %d\n",nSpeed);
- return 0;
- }
代码中给出了可以设置成阻塞和非阻塞的操作。
// <![CDATA[ // ]]
true
Linux下打开串口设置的更多相关文章
- 详解linux下的串口通讯开发
串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统.调制解调 ...
- 具体解释linux下的串口通讯开发
串行口是计算机一种经常使用的接口,具有连接线少.通讯简单,得到广泛的使用.经常使用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统. ...
- 【转载】详解linux下的串口通讯开发
来源:https://www.cnblogs.com/sunyubo/archive/2010/09/26/2282116.html 串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使 ...
- Linux下的串口调试工具——Xgcom
Linux下的串口调试工具——Xgcom xgcom的下载网址:https://code.google.com/archive/p/xgcom/downloads (1)安装必须的库 apt-get ...
- [转载]linux下core文件设置与查看
转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...
- Linux下环境变量设置 (转)
Linux下环境变量设置 1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错 ...
- linux下查看串口信息
rs232串口通信接口:当通信距离较近时(<12m),可以使用电缆线直接连接,若距离较远,需附加调制解调器. 9个脚针的定义: CDC数据载波检测,RXD接收数据,TXD发送数据,DTR数据中断 ...
- linux下打开chm文件的方法
windows中,通常情况下,chm文件可以使用系统自带的程序打开,但是linux就没有那么幸运了,那么,如何在linux下打开chm 文件呢?有小编来为您介绍介绍,本篇,小编以ubuntu环境为例 ...
- MongoDB在Linux下常用优化设置
MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...
随机推荐
- Entity Framework 5.0基础系列
1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...
- 漫谈Puppet4
激动人心的改进 速度,速度,还是速度 稳定性和鲁棒性的提升 全新的Parser “不变"的agent 不兼容的改动 包管理方式的变化 配置文件/目录的路径变化 其他路径变化 Director ...
- 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))
作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...
- java/.net-常用工具下载地址&常用学习网址&快捷键
HTML5 HTML5:http://www.html5cn.org/ php常用网址 thinkphp框架:http://www.thinkphp.cn/ wampserver开发服务器:http: ...
- c++中typename和class的区别介绍
"typename"是一个C++程序设计语言中的关键字.相当用于泛型编程时是另一术语"class"的同义词.这个关键字用于指出模板声明(或定义)中的非独立名称( ...
- git的一些相关知识
1.配置多个git远程仓库的ssh-Key切换(转自) 目前的git仓库如github都是通过使用SSH与客户端连接,如果只是固定使用单个git仓库的单个用户 (first),生成生成密钥对后,将公钥 ...
- css blur 的兼容写法
出自:小tip: 使用CSS将图片转换成模糊(毛玻璃)效果 .blur { filter: url(blur.svg#blur); /* IE10, IE11 */ -webkit-filter: b ...
- 使用ueditor小结
1. 导入 js: ueditor.config.js ueditor.all.js css/images/plugin: themes lang dialog(可选) third-party(可选) ...
- ffmpeg安装的问题
php语音转换需要安装ffmpeg文件 参考地址: http://thierry-xing.iteye.com/blog/2017864 http://diogomelo.net/blog/11/en ...
- Kafka - SQL 代码实现
1.概述 上次给大家分享了关于 Kafka SQL 的实现思路,这次给大家分享如何实现 Kafka SQL.要实现 Kafka SQL,在上一篇<Kafka - SQL 引擎分享>中分享了 ...