友善示例分析--adc-test

2114阅读 0评论2012-06-14 kangear
分类:C/C++


  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. }

上一篇:Qt出现:无法启动终端模拟器'xterm'
下一篇:Qt出现undefined refernce to 'pthread_create'解决方法