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.关闭数据库文件的 ...
随机推荐
- C#:使用MD5对用户密码加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...
- Leetcode-189 Rotate Array
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- HU 参考错误修正:/SCWM/RCORR_HUREF
HU 参考错误修正:report: /SCWM/RCORR_HUREF HU 参考的ODO/ID的凭证号及行项目号不正确的修正程序.
- mybatis connection error Cannot create PoolableConnectionFactory (Access denied for user 'root '@'local
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Persiste ...
- HTTP状态码(HTTP Status Code)及常用场景
常见的状态码: HTTP: Status 200 – 服务器成功返回网页HTTP: Status 3xx - 表示要完成请求,需要进一步操作. 通常,这些状态代码用来重定向HTTP: Status 4 ...
- Jquery 实现input回车时跳转到下一个input元素
/** * 回车时跳转到下一个元素 * @Author HTL * @DateTime 2016-12-30T11:33:25+0800 * @param {[type]} $input [INP ...
- (String) | String.valueOf()
Map m = new HashMap(); Integer i = 5; String s = null; m.put("val1", i); m.put("val2& ...
- CSRF - 空Referer绕过
在实际环境中,服务器很多CGI由于一些历史原因,必须允许空Referer的请求.比如:老的客户端版本请求的时候就没有Referer,总不能在服务端一刀切,让老版本的用户都无法正常使用了吧. 这样的CG ...
- 记录javascript 验证字符串布尔类型 及url 参数获取
/^true$/i.test("false");false/^true$/i.test("true");true //获取请求参数的值 function Req ...
- C#集合 -- Lists,Queues, Stacks 和 Sets
List<T>和ArrayList Generic的List和非Generic的ArrayList类支持可变化大小的对象数组,它们也是最常见的集合类.ArrayList实现了IList接口 ...