linux UART
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h> int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != )
{
perror("SetupSerial 1");
return -;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE; switch( nBits )
{
case :
newtio.c_cflag |= CS7;
break;
case :
newtio.c_cflag |= CS8;
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 )
{
case :
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case :
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case :
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case :
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == )
{
newtio.c_cflag &= ~CSTOPB;
}
else if ( nStop == )
{
newtio.c_cflag |= CSTOPB;
}
newtio.c_cc[VTIME] = ;
newtio.c_cc[VMIN] = ;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=)
{
perror("com set error");
return -;
}
printf("set done!\n");
return ;
} int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttySAC0","/dev/ttySAC1","/dev/ttySAC3","/dev/ttySAC4"};
long vdisable;
if (comport==)
{
fd = open( dev[comport - ], O_RDWR|O_NOCTTY|O_NDELAY);
if (- == fd)
{
perror("Can't Open Serial Port");
return(-);
}
else
{
printf("%s .....\n", dev[comport - ]);
}
}
else if(comport==)
{ fd = open( dev[comport - ], O_RDWR|O_NOCTTY|O_NDELAY);
if (- == fd)
{
perror("Can't Open Serial Port");
return(-);
}
else
{
printf("%s .....\n", dev[comport - ]);
}
}
else if (comport==)
{
fd = open( dev[comport - ], O_RDWR|O_NOCTTY|O_NDELAY);
if (- == fd)
{
perror("Can't Open Serial Port");
return(-);
}
else
{
printf("%s .....\n", dev[comport - ]);
}
}
if(fcntl(fd, F_SETFL, )<)
{
printf("fcntl failed!\n");
}
else
{
printf("fcntl=%d\n",fcntl(fd, F_SETFL,));
}
if(isatty(STDIN_FILENO)==)
{
printf("standard input is not a terminal device\n");
}
else
{
printf("isatty success!\n");
}
printf("fd-open=%d\n",fd);
return fd;
} int main(void)
{
int fd;
int nread,i;
char buff[]="Hello\n"; if((fd=open_port(fd,))<)
{
perror("open_port error");
return;
}
if((i=set_opt(fd,,,'N',))<)
{
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);
while(){
errno = ;
nread=read(fd,buff,);
if( < nread){
printf("nread=%d,%s\n",nread,buff);
}
}
close(fd);
return;
}
linux UART的更多相关文章
- I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo
/******************************************************************************************* * I.MX6 ...
- linux UART串口驱动开发文档
转:http://www.360doc.com/content/10/0417/18/829197_23519037.shtml linux UART串口驱动开发文档时间:2010-01-09 14: ...
- Linux uart程序
我用的是jetson tx1 开发板 都是linux系统出了串口文件可能不同其他的没有什么不同都能用. 我安装的是qt5 新建一个none qt c工程,用c 语言开发 期间调试了两天结果还是发送和 ...
- linux uart驱动——相关数据结构以及API(二)
一.核心数据结构 串口驱动有3个核心数据结构,它们都定义在<#include linux/serial_core.h>1.uart_driver uart_driver包 ...
- Linux UART驱动分析
1. 介绍 8250是IBM PC及兼容机使用的一种串口芯片; 16550是一种带先进先出(FIFO)功能的8250系列串口芯片; 16550A则是16550的升级版本, 修复了FIFO相关BUG, ...
- Linux UART介绍
1. UART介绍 UART是一类tty设备, 是一种串行端口终端, 具体可参考<UART接口介绍>在Linux中UART属于tty驱动的一部分, 具体实现包括驱动抽象层和硬件实现层 本文 ...
- linux uart驱动——uart platfrom 注册(三)
一:注册platform device 注册一个platfrom device一般需要初始化两个内容,设备占用的资源resource和设备私有数据dev.platfrom_data.设备的resour ...
- linux uart驱动——uart原理(一)
UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232.RS499.R ...
- [uart]linux uart应用层配置
http://www.raviyp.com/embedded/189-serial-port-programming-in-linux-using-c-working-code
随机推荐
- Seafile安装踩坑
欢迎大家访问我的博客新地址: 博客:http://monsterlin.com/ 原文地址:http://monsterlin.com/archives/HowToInstallSeafile.htm ...
- Java中设计模式之单例设计模式-1
单例作用 1 节省内存 2 可以避免多种状态导致状态冲突 单例的创建步骤 1 私有化构造方法 2 私有化声明的属性 3 getInstance 4 方法需要静态 单例分类 1.懒汉式 2.饿汉式 两种 ...
- bootstrapValidator 使用(包含入门demo,常用方法,以及常用的规则)
一 什么是bootstrapValidator? -- 一个基于 jquery,boostrap 的表单验证框架....简单实用上手快,页面美观还过得去,不废话了,直接撸. 二 boots ...
- python http长连接客户端
背景: 线上机器,需要过滤access日志,发送给另外一个api 期初是单进程,效率太低,改为多进程发送后,查看日志中偶尔会出现异常错误(忘记截图了...) 总之就是端口不够用了报错 原因: 每一条日 ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- angularjs应用prerender.io 搜索引擎优化实践
上一篇博文(http://www.cnblogs.com/ideal-lx/p/5625428.html)介绍了单页面搜索引擎优化的原理,以及介绍了两个开源框架的优劣.prerender框架的工作原理 ...
- Nmap脚本文件分析(AMQP协议为例)
Nmap脚本文件分析(AMQP协议为例) 一.介绍 上两篇文章 Nmap脚本引擎原理 编写自己的Nmap(NSE)脚本,分析了Nmap脚本引擎的执行过程,以及脚本文件的编写,这篇文章将以解析AMQ ...
- 使用EF连接Postgresql
环境: VS2017 Community Windows 10 Postgresql 9.6 安装Postgresql: https://www.postgresql.org/download/ 1. ...
- eclipse tomcat add时提示The Tomcat server configuration at \Servers\Tomcat v6.0 Server at localhost-
原因为左侧项目中把对于网络应用的servers项目给关闭或删除了,导致相应的localhost-config无法定位,所以才发生的错误. 解决办法:删除servers下的tomcat 重新添加serv ...
- Behavior的使用(一):页面跳转NavigateToPageAction
Behavior的使用,让UI设计师能够更加方便的进行UI设计,更高效地和开发进行合作.Behavior有三种触发方式:EventTriggerBehavior事件触发,DataTriggerBeha ...