Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下~

两个实用的函数~

/**

*@brief 设置串口通信速率

*@param fd 类型 int 打开串口的文件句柄

*@param speed 类型 int 串口速度

*@return void

*/

int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,

B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,

115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed){

int i;

int status;

struct termios Opt;

tcgetattr(fd, &Opt);

for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {

if (speed == name_arr[i]) {

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

status = tcsetattr(fd, TCSANOW, &Opt);

if (status != 0) {

perror("tcsetattr fd1");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

/**

*@brief 设置串口数据位,停止位和效验位

*@param fd 类型 int 打开的串口文件句柄

*@param databits 类型 int 数据位 取值 为 7 或者8

*@param stopbits 类型 int 停止位 取值为 1 或者2

*@param 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 &= ~CSIZE;

switch (databits) /*设置数据位数*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size

"); 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*/

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;break;

default:

fprintf(stderr,"Unsupported parity

");

return (FALSE);

}

/* 设置停止位*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits

");

return (FALSE);

}

/* 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);

}

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/

options.c_oflag &= ~OPOST; /*Output*/

return (TRUE);

}

调用的方法比較的简单,例如以下。fd是打开的tty设备的文件句柄

set_speed(fd,115200);

if (set_Parity(fd,8,1,'N') == FALSE) {

printf("Set Parity Error

");

}

总的測试代码例如以下。

#include

#include

#include

#include

#include

#define BAUDRATE B115200

#define UART_DEVICE "/dev/ttyS3"

#define FALSE -1

#define TRUE 0

/**

*@brief 设置串口通信速率

*@param fd 类型 int 打开串口的文件句柄

*@param speed 类型 int 串口速度

*@return void

*/

int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,

B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,

115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed){

int i;

int status;

struct termios Opt;

tcgetattr(fd, &Opt);

for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {

if (speed == name_arr[i]) {

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

status = tcsetattr(fd, TCSANOW, &Opt);

if (status != 0) {

perror("tcsetattr fd1");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

/**

*@brief 设置串口数据位,停止位和效验位

*@param fd 类型 int 打开的串口文件句柄

*@param databits 类型 int 数据位 取值 为 7 或者8

*@param stopbits 类型 int 停止位 取值为 1 或者2

*@param 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 &= ~CSIZE;

switch (databits) /*设置数据位数*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size

"); 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*/

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;break;

default:

fprintf(stderr,"Unsupported parity

");

return (FALSE);

}

/* 设置停止位*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits

");

return (FALSE);

}

/* 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);

}

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/

options.c_oflag &= ~OPOST; /*Output*/

return (TRUE);

}

int main(int argc, char *argv[])

{

int fd, c=0, res;

char buf[256];

printf("Start...

");

fd = open(UART_DEVICE, O_RDWR);

if (fd < 0) {

perror(UART_DEVICE);

exit(1);

}

printf("Open...

");

set_speed(fd,115200);

if (set_Parity(fd,8,1,'N') == FALSE) {

printf("Set Parity Error

");

exit (0);

}

printf("Reading...

");

while(1) {

res = read(fd, buf, 255);

if(res==0)

continue;

buf[res]=0;

printf("%s", buf);

if (buf[0] == 0x0d)

printf("

");

if (buf[0] == '@') break;

}

printf("Close...

");

close(fd);

return 0;

}

Linux uart寄存器读写,Linux下读写UART串口的代码相关推荐

  1. linux内核态串口读写程序,linux 0.11 内核学习 -- rs_io.s,串口汇编代码

    /* *  该文件实现rs232 串行通信中断处理 */ /* *  linux/kernel/rs_io.s * *  (C) 1991  Linus Torvalds */ /* *rs_io.s ...

  2. 通过mtd读写flash_Linux下读写FLASH驱动——MTD设备分析

    最近在学习驱动读写flash的代码部分.经历了可笑的过程:开始我知道flash用通过spi口来读写.所以就到了driver/spi 下面看相关代码.发现有个spidev.c里面有read/write/ ...

  3. emacs Linux Java编程环境_Linux下搭建用emacs查看代码的开发环境

    在windows下面我们有source in sight可以方便的浏览大工程中的代码,切换到linux环境下开发时,我们也可以搭建一个这样的环境.下面的内容将介绍如何搭建这样一个开发环境(这里我们假设 ...

  4. linux 查看寄存器信息,linux获取系统信息的常用命令

    最近看了一些Linux命令行的文章,在系统信息查看方面学到不少命令.想起以前写过的一篇其实Linux这样用更简单,发现这些系统信息查看命令也可以总结出一篇小小的东西来了. # cat /proc/mt ...

  5. linux设备寄存器映射,linux LCD驱动 及 ARM 寄存器映射

    折腾了2天LCD驱动程序才发现系统其实已经移植了LCD驱动.设备名为/dev/fb0. 白折腾. 不过也有好处,搞了一遍基本了解LCD驱动的实现方法. 这里涉及到一个问题是如何把ARM寄存器地址空间映 ...

  6. linux 0.11 内核学习 -- rs_io.s,串口汇编代码

    /* *  该文件实现rs232 串行通信中断处理 */ /* *  linux/kernel/rs_io.s * *  (C) 1991  Linus Torvalds */ /* * rs_io. ...

  7. java 安卓蓝牙程序_求Java大神帮忙,简单修改下安卓蓝牙串口输出程序代码!!急...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 附上蓝牙串口通讯代码, package com.test.BTClient; import java.io.File; import java.io.Fi ...

  8. linux i2c 读写函数,Linux下读写芯片的I2C寄存器

    要想在Linux下读写芯片的I2C寄存器,一般需要在Linux编写一份该芯片的I2C驱动,关于Linux下如何编写I2C驱动,前一篇文章<手把手教你写Linux I2C设备驱动>已经做了初 ...

  9. Linux下读写芯片的I2C寄存器

    Linux下读写芯片的I2C寄存器 2012-01-10 11:40:18 标签:Linux 寄存器 驱动 读写 I2C 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本 ...

最新文章

  1. 实例 - 购物车 (列表、循环)
  2. [20170420]关于延迟块清除3.txt
  3. 【SICP练习】3 练习1.7
  4. UVA - 11846 Finding Seats Again (DFS搜索对象)
  5. 谁说Python慢来着?不用Python,这个问题难倒了无数的程序员
  6. Log4j日志使用记录
  7. java 加载java文件_如何用JAVA实现加载一个文件?
  8. 吉林大学超星学习通05
  9. 凸透镜成像实验软件_凸透镜成像模拟实验软件介绍
  10. mysql计算同比和环比的区别_MySQL实践之同比环比
  11. 自定义View中Canvas之Path的详解
  12. C# 二维码 生成、解析
  13. gitbook安装中installing gitbook xxx 时间过长的问题
  14. stm32 + ESP8266 wifi获取网络时间和天气 stm32天气预报
  15. Web测试的各个测试点
  16. Android短视频开发中的sdk接入方案
  17. Flutter中解决AndroidX包与Support包冲突问题
  18. fabric使用配置文件configtx.yaml生成创世区块时遇到的坑
  19. 【STM32F407】第8章 ThreadX NetXDUO之TCP服务器
  20. python系列3(list tupe dict )

热门文章

  1. Swift 4正式发布,新功能概览
  2. 关于NodeJS配置HTTPS服务、阿里云申请HTTPS证书
  3. [Java 安全]加密算法
  4. ITIL的一些简单感受
  5. 论逗逼的自我修养——BZOJ第一页计划
  6. poj_2182 线段树/树状数组
  7. nyoj 47 江 河问题 【贪婪】
  8. postman-持续更新
  9. 科技日报头版显要位置报道国内多家企业投融资给力永中软件
  10. C#多线程学习6——互斥对象