Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536064
  • 博文数量: 67
  • 博客积分: 1625
  • 博客等级: 上尉
  • 技术积分: 1053
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-04 14:40
文章分类

全部博文(67)

文章存档

2012年(67)

分类: C/C++

2012-06-14 22:53:10


  1. /***************************************************************************
  2.     zhushi : (C) by kangear ^_^
  3.     email : kangear@163.com

  4.  ***************************************************************************/
  5. /***************************************************************************
  6.  * *
  7.  * This program is free software; you can redistribute it and/or modify *
  8.  * it under the terms of the GNU General Public License as published by *
  9.  * the Free Software Foundation; either version 2 of the License, or *
  10.  * (at your option) any later version. *
  11.  * *
  12.  ***************************************************************************/
  13. #include <stdio.h> //sterr sscanf(存储的数据,格式控制字符串,选择性设定字符串)
  14. #include <unistd.h> //鸡肋
  15. #include <stdlib.h>    //鸡肋
  16. #include <sys/types.h>    //鸡肋
  17. #include <sys/stat.h>    //鸡肋
  18. #include <sys/ioctl.h>    //鸡肋
  19. #include <fcntl.h> //open();read();close;
  20. #include <linux/fs.h>    //鸡肋
  21. #include <errno.h>    //鸡肋
  22. #include <string.h>    //鸡肋    

  23. int main(void)
  24. {
  25.     fprintf(stderr, "press Ctrl-C to stop\n");        //相当于printf;
  26.     int fd = open("/dev/adc", 0); //文件编程中的 文件打开
  27.     if (fd < 0) //文件打开失败
  28.     {
  29.         perror("open ADC device:"); //错误信息打印出来
  30.         return 1;
  31.     }
  32.     for(;;) //呃……这个从功能上说是循环,但是有点不懂……!-_-!
  33.   {
  34.         char buffer[30];         //定义一数组
  35.         int len = read(fd, buffer, sizeof buffer -1);        //文件编程中的 文件读 成功返回长度
  36.         if (len > 0) {         //读成功
  37.             buffer[len] = '\0';             //在末尾添加“\0”结束符
  38.             int value = -1;
  39.             sscanf(buffer, "%d", &value);             //将得到的信息中提取整型数
  40.             printf("ADC Value: %d\n", value);             //打印
  41.         }
  42.         else         //读失败
  43.             {
  44.             perror("read ADC device:");             //输出错误信息
  45.             return 1;
  46.          }
  47.         usleep(500* 1000);         //和sleep()功能一样不过是μ秒(相当于睡眠0.5秒)
  48.      }
  49.     
  50.     close(fd); //文件编程中的 文件关闭
  51. }

阅读(2037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~