#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的更多相关文章

  1. I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo

    /******************************************************************************************* * I.MX6 ...

  2. linux UART串口驱动开发文档

    转:http://www.360doc.com/content/10/0417/18/829197_23519037.shtml linux UART串口驱动开发文档时间:2010-01-09 14: ...

  3. Linux uart程序

    我用的是jetson tx1 开发板 都是linux系统出了串口文件可能不同其他的没有什么不同都能用. 我安装的是qt5 新建一个none  qt c工程,用c 语言开发 期间调试了两天结果还是发送和 ...

  4. linux uart驱动——相关数据结构以及API(二)

    一.核心数据结构      串口驱动有3个核心数据结构,它们都定义在<#include linux/serial_core.h>1.uart_driver     uart_driver包 ...

  5. Linux UART驱动分析

    1. 介绍 8250是IBM PC及兼容机使用的一种串口芯片; 16550是一种带先进先出(FIFO)功能的8250系列串口芯片; 16550A则是16550的升级版本, 修复了FIFO相关BUG, ...

  6. Linux UART介绍

    1. UART介绍 UART是一类tty设备, 是一种串行端口终端, 具体可参考<UART接口介绍>在Linux中UART属于tty驱动的一部分, 具体实现包括驱动抽象层和硬件实现层 本文 ...

  7. linux uart驱动——uart platfrom 注册(三)

    一:注册platform device 注册一个platfrom device一般需要初始化两个内容,设备占用的资源resource和设备私有数据dev.platfrom_data.设备的resour ...

  8. linux uart驱动——uart原理(一)

    UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232.RS499.R ...

  9. [uart]linux uart应用层配置

    http://www.raviyp.com/embedded/189-serial-port-programming-in-linux-using-c-working-code

随机推荐

  1. 利用wget检测网页是否正常访问

    #!/bin/bash function CheckUrl() { timeout=5 fails=0 success=0 while true do wget --timeout=5 --tries ...

  2. MySQL数据库安全策略

    1. 删除所有MySQL中的用户(包括root用户): mysql> delete from mysql.user root可以保留,然后修改为其他用户 2. 为管理员root用户设置密码 : ...

  3. 生成JSON数据--fastjson(阿里)方法

    fastjson(阿里)方法生成JSON数据: 与Gson类似,创建相应类,再使用JSON.toJSONString()添加对象 要求:生成如下JSON数据 1.{"age":3, ...

  4. a标签点击之后有个虚线边框,怎么去掉

    1.行内处理方式1 <a hidefocus="true" href="#"></a> 2.行内处理方式2,让a标签获得焦点就失去焦点, ...

  5. Go从入门到精通(一)go语言初始

    一.第一个go程序 package main import ( "fmt" ) func main(){ fmt.Println("hello world") ...

  6. 小白审计JACKSON反序列化漏洞

    1. JACKSON漏洞解析 poc代码:main.java import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.or ...

  7. ionic 项目中创建侧边栏的具体流程分4步简单学会

    这是在学习ionic时,当时遇到的一些问题,觉得很难,就记笔记下来了,现在觉得如果可以拿来分享,有可能会帮助到遇到同样问题的人 ionic slidemenu 项目流程: cd pretices(自己 ...

  8. IBM芯片新功能:诊断癌症

    治愈任何疾病的最好方法是什么?-------------- 当然是把它扼杀在摇篮里. "早发现早治疗"已经是老生常谈,但要准确监测疾病又谈何容易?不过现在,IBM这枚小小的芯片就能 ...

  9. Linux网络编程“惊群”问题总结

    1.前言 我从事Linux系统下网络开发将近4年了,经常还是遇到一些问题,只是知其然而不知其所以然,有时候和其他人交流,搞得非常尴尬.如今计算机都是多核了,网络编程框架也逐步丰富多了,我所知道的有多进 ...

  10. css3特效详解

    好吧,CSS3 3D transform变换,不过如此! 这篇文章发布于 2012年09月7日,星期五,01:05,归类于 css相关. 阅读 408042 次, 今日 34 次 一.写在前面的秋裤 ...