#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. STM32伺服编码器接口

    在STM32的高级定时器和一般定时器中有Encoder interface mode(编码器接口),TI1和TI2分别对应TIM_CH1 和TIM_CH2 通道. 一.计数规则如下: 表55的是编码器 ...

  2. 网络编程应用:基于TCP协议【实现文件上传】--练习

    要求: 基于TCP协议实现一个向服务器端上传文件的功能 客户端代码: package Homework2; import java.io.File; import java.io.FileInputS ...

  3. 基于查表的整数霍夫变换方法实现(matlab)

    暂时先用matlab把算法弄一下,这是基于查表的整数霍夫变换方法实现及解释. 接着再实现FPGA的霍夫变换. 霍夫变换原理和算法这里不多说,可参考以下链接: http://blog.csdn.net/ ...

  4. 排序算法 - 插入排序(Insertion sort)

    插入排序对于少量元素的排序是很高效的,而且这个排序的手法在每个人生活中也是有的哦. 你可能没有意识到,当你打牌的时候,就是用的插入排序. 概念 从桌上的牌堆摸牌,牌堆内是杂乱无序的,但是我们摸上牌的时 ...

  5. DNS域名解析的过程

    一.主机解析域名的顺序 1.找缓存 2.找本机的hosts文件 3.找DNS服务器 注意: 配置IP和主机名时,要记得修改/etc/hosts文件,因为有些应用程序在主机内的进程之间通信的时候,会本机 ...

  6. 基于C#的接口自动化测试(二)

    Json多重数据处理方法II:jobj直接向后调用即可 ]["GoodsName"]; 用:分隔的字符串转字典: public static Dictionary<strin ...

  7. ecshop收货地址货到付款修改

    用户选择某些地址时,支付方式里则可以依据此地址来对货到付款选项进行显示或隐藏.目前仅与顺丰合作,以顺丰提供的数据为准. 使用到的数据库分别如下: ecs_region//地方数据,PRIMARY KE ...

  8. 东北育才 DAY2组合数取mod (comb)

    组合数取模(comb) [问题描述] 计算C(m,n)mod 9901的值 [输入格式] 从文件comb.in中输入数据. 输入的第一行包含两个整数,m和n [输出格式] 输出到文件comb.out中 ...

  9. 什么是nginx?

    1.nginx是一款服务器软件 2.nginx是一个高性能的HTTP和反向代理服务器: 3.nginx是一个代理邮件服务器: 4.nginx还可以实现负载均衡: nginx的优缺点: 优点:可以实现高 ...

  10. 【RAC】RAC相关基础知识

    [RAC]RAC相关基础知识 1.CRS简介    从Oracle 10G开始,oracle引进一套完整的集群管理解决方案—-Cluster-Ready Services,它包括集群连通性.消息和锁. ...