#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. 常见的几种Flume日志收集场景实战

    这里主要介绍几种常见的日志的source来源,包括监控文件型,监控文件内容增量,TCP和HTTP. Spool类型 用于监控指定目录内数据变更,若有新文件,则将新文件内数据读取上传 在教你一步搭建Fl ...

  2. .NET MVC页面生命周期及传统ASP.NET页面周期

    目前我主要使用.Net MVC框架进行网页创建,数据库是MSSQL Server.所以,我就用.NET MVC框架的web页面周期来说明页面的生命周期,但是我觉着其他MVC框架也是大同小异的. 本文主 ...

  3. R语言重要数据集分析研究——R语言数据集的字段含义

    R语言数据集的字段含义 作者:马文敏 选择一种数据结构来储存数据 将数据输入或导入到这个数据结构中 数据集的概念 数据集通常是有数据结构的一个矩形数组,行表示规则,列表示变量. 不同的行业对数据集的行 ...

  4. .NET Core程序中使用User Secrets存储敏感数据

    前言 在开发中经常会用到一些敏感数据,比如AppSecret或数据库连接字符串,无论是硬编码还是写在配置文件中,最终都要push到svn或git上.对于开源项目,这些敏感数据就无隐私可言了,对于私有项 ...

  5. 身份证识别OCR,开启视频模式扫一扫即可识别身份证信息

    文章摘要:身份证识别等证件识别OCR技术在各个行业得到广泛应用,例如:车险移动查勘会用到身份证识别.行驶证识别.车架号识别: 寿险移动展业会用到名片识别.银行卡识别:电信实名制代理网点采集身份证信息会 ...

  6. excel转html 实现在线预览

    首先说一下,本人发布的代码都是经过本人亲测,并且用在实际项目中.如果觉得可以,希望大家点个赞,谢谢大家. 有什么问题,大家评论出来,一起交流.好了,不废话了,下面来说一说这个东西怎么做. 网上也有许多 ...

  7. Socket实现

    网络实现架构 4.4BSD通过同时对多种通信协议的支持来提供通用的底层基础服务.4.4BSD支持四种不同的通信协议簇: TCP/IP(互联网协议簇) XNS(Xerox网络系统) OSI协议 Unix ...

  8. mkdirs自动创建文件夹

    //保存路径 如file路径是E:\filer\a.jpg File toSaveFile=new File("filePath"); if(toSaveFile!=null){ ...

  9. js中数学运算的处理

    connum = Number(connum) + Number($(this).parents('.123').find(".views_core_hidden").val()) ...

  10. JS初步学习

    [使用JS的三种方式] 1.HTML标签中内嵌JS(不提倡使用): <button onclick="javascript:alert('小碧池!你真点啊!')">有本 ...