Linux 串口编程
今天对应用层串口编程进行了验证。程序来源于以下参考链接,自己进行了一些注释和更改,记录于此。
Tony Liu, 2016-6-17, Shenzhen
参考链接
https://www.ibm.com/developerworks/cn/linux/l-serials/
http://digilander.libero.it/robang/rubrica/serial.htm
http://blog.csdn.net/querdaizhi/article/details/7436722
程序
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <string.h>
#define FALSE -1
#define TRUE 0
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt); //读取当前串口的配置, 后面会定其中的一些配置更改再写回去。
tcflush(fd, TCIOFLUSH); //清空输入输出队列
cfsetispeed(&Opt, speed); //设置输入速度
cfsetospeed(&Opt, speed); //设置输出速度
status = tcsetattr(fd, TCSANOW, &Opt); //写给底层寄存器
if (status != 0) {
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
/**
* 设置串口数据位,停止位和效验位
* fd 类型 int 打开的串口文件句柄
* databits 类型 int 数据位 取值 为 7 或者8
* stopbits 类型 int 停止位 取值为 1 或者2
* 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 |= CLOCAL | CREAD;
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS; //无硬件流控
options.c_oflag = 0; //输出模式
options.c_lflag = 0; //不激活终端模式
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*/ //空闲(space)校验和不校验的设置一样
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
/* 设置停止位, 这里为什么没有设置1.5的停止位 */
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);
}
//总感觉这里的判断不是很合理,只是判断‘n’,“N”难道就不判断了?
/* Set input parity option */
// if (parity != 'n')
// options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR | O_NOCTTY); //阻塞
// int fd = open(Dev, O_RDWR | O_NOCTTY | O_NDELAY); // 非阻塞
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main(int argc, char **argv){
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttymxc2"; //串口,根据自己板子的节点名称进行更改
fd = OpenDev(dev);
set_speed(fd, B9600); //根据系统定义的波特率的宏进行设置
if (set_Parity(fd,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
exit (0);
}
//循环读取数据
while (1) {
// nread = write(fd, "hello", 6);
// printf("write count: %d\n", nread);
//每次就只能读到一字节, 所以输出的Len为1,buff也只是单个字节
while ((nread = read(fd, buff, 512))>0)
{
printf("Len %d\n",nread);
buff[nread+1] = '\0';
printf( "%s\n", buff);
}
}
//close(fd);
// exit (0);
}
Linux 串口编程的更多相关文章
- storysnail的Linux串口编程笔记
storysnail的Linux串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据Ge ...
- Linux串口编程详解(转)
串口本身,标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口曾经被广泛用于连接计算机和终端设备和各种外部设备.虽然以太网接口和USB接口也是以一个串行流进行数据传送的,但是串口连接 ...
- linux串口编程总结
串口本身.标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口以前被广泛用于连接计算机和终端设备和各种外部设备.尽管以太网接口和USB接口也是以一个串行流进行数据传送的.可是串口连接 ...
- linux串口编程参数配置详解(转)
1.linux串口编程需要的头文件 #include <stdio.h> //标准输入输出定义#include <stdlib.h> //标准函数 ...
- linux串口编程参数配置详解
1.linux串口编程需要的头文件 #include <stdio.h> //标准输入输出定义 #include <stdlib.h> //标准函 ...
- linux串口编程
按照对linux系统的理解,串口编程的顺序无非就是open,read,write,close,而串口有波特率.数据位等重要参数需要设置,因此还应该用到设置函数,那么接下来就带着这几个问题去学习linu ...
- Linux串口编程进阶
在<Linux串口编程>编程一文中介绍了串口应用中常用的基本操作,如:串口打开关闭.串口设置.数据收发等.本篇文章主要基于常规串口操作进行了扩充,主要介绍如下操作: Linux系统使用非标 ...
- Linux串口编程(转载)
在嵌入式Linux中,串口是一个字设备,访问具体的串行端口的编程与读/写文件 的操作类似,只需打开相应的设备文件即可操作.串口编程特殊在于串 口通信时相关参数与属性的设置.嵌入式Linux的串口编程时 ...
- linux串口编程设置(转载)
(转载)在嵌入式Linux中,串口是一个字设备,访问具体的串行端口的编程与读/写文件 的操作类似,只需打开相应的设备文件即可操作.串口编程特殊在于串 口通信时相关参数与属性的设置.嵌入式Linux的串 ...
随机推荐
- JavaScript 数组方法处理字符串 prototype
js中数组有许多方法,如join.map,reverse.字符串没有这些方法,可以“借用”数组的方法来处理字符串. <!doctype html> <html lang=" ...
- 自己定义UIView以实现自绘
有时候我们须要自绘uiview以实现自己的需求,比方依据坐标点绘制出连续的曲线(股票走势图),就须要自绘uiview了. 原理:继承uiview类(customView),并实现custom view ...
- DBCP(一)数据源配置文件
DBCP是Apache开发的数据源API,使用的话需要导入dbcp jar包.collections jar包.pool jar包. 其数据源匹配的配置文件格式如下: #连接设置 driverCl ...
- Linux-软件包管理-yum在线管理-yum命令
yum list 查看所有可用软件包列表 vim /etc/yum.repos.d/CentOS-Base.repo 查看当前linux系统默认的网络yum源信息 yum search httpd 搜 ...
- 不止是联网!教你玩转PC自带Wi-Fi网卡
前言:Wi-Fi对于现在的智能手机来说已经是再熟悉不过的配置了,而主板自带Wi-Fi网卡的设计也越来越普及,但有些玩家可能思维还停留在“Wi-Fi网卡 = 连无线网络用的网卡,我用有线就不需要”的层次 ...
- EntityFramework Data Annotations
详细的实体映射介绍(Data Annotation) http://msdn.microsoft.com/en-us/data/jj591583
- tomcat的web项目的远程热发布
已经发布的项目war包更改了怎么办?我常用的做法是: .把war包上传服务器 .远程登录服务器后台 .停止tomcat服务 .用新的war包替换老的war包 .启动tomcat服务 后来才知道原来to ...
- windows上通过secureCRT和putty创建密钥登录
前面介绍了linux的ssh远程登录协议和ssh无password登录方式.这里在windows下通过secureCRT和putty登录linux来看一下详细的密钥创建,配置和登录.也算做个备忘录吧. ...
- AutoFac文档12(转载)
目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 Resolve的参数 ...
- MySQL中多表删除方法
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...