免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16320 | 回复: 9
打印 上一主题 下一主题

[WebServer] 求救boa+ajax+cgi(c)解决方案 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-20 14:35 |只看该作者 |倒序浏览
想实现在网页中每隔两秒钟刷新一次温度值,而不去刷新整个网页,应该用怎样的方式去实现呀,用javascript定时触发XMLHttpRequest的onreadystatechange方法吗?求高手指点了。

论坛徽章:
0
2 [报告]
发表于 2011-05-20 15:16 |只看该作者
补充下,是不需要任何触发的定时刷新温度值

论坛徽章:
0
3 [报告]
发表于 2011-05-26 08:10 |只看该作者
js里面有定时器

论坛徽章:
0
4 [报告]
发表于 2011-05-26 08:52 |只看该作者
ajax

就是 javascript+GET method

论坛徽章:
0
5 [报告]
发表于 2011-05-26 10:46 |只看该作者
ajaxtest.html文件
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
  4. <title>Ajax + CGI Test</title>
  5. <script language="JavaScript" src="ajaxtest.js"></script>
  6. </head>
  7. <body>
  8. <h3>获取服务器当前温度</h3>
  9. <p>服务器当前温度 是:<div id="current_time"></div></p>
  10. <input type="button" value="获取" onclick="sender()"/>
  11. </body>
  12. </html>
复制代码
ajaxtest.js文件

  1. var xmlhttp;

  2. /*
  3. *创建异步访问对象
  4. */
  5. function createXHR()
  6. {
  7.         var xhr;

  8.         try
  9.         {
  10.                 xhr = new ActiveXObject("Msxml2.XMLHTTP");
  11.         }
  12.         catch(e)
  13.         {
  14.                 try
  15.                 {
  16.                         xhr = new ActiveXObject("Microsoft.XMLHTTP");
  17.                 }
  18.                 catch(e2)
  19.                 {
  20.                         xhr = false;
  21.                 }
  22.         }

  23.         if(!xhr && typeof XMLHttpRequest !='undefined')
  24.         {
  25.                 xhr = new XMLHttpRequest();
  26.         }

  27.         return xhr;
  28. }

  29. /*
  30. *异步访问提交处理
  31. */
  32. function sender()
  33. {
  34.         xmlhttp = createXHR();

  35.         if(xmlhttp)
  36.         {
  37.                 xmlhttp.onreadystatechange=callbackFunction;

  38.                 /*test.cgi后面跟个cur_time参数是为了防止Ajax页面缓存*/
  39.                 xmlhttp.open("GET","ajaxtest.cgi?cur_time="+new Date().getTime(),true);

  40.                 xmlhttp.send(null);
  41.         }
  42.         else
  43.         {
  44.                 /*XMLHttpRequest对象创建失败*/
  45.                 alert("浏览器不支持,请更换浏览器!");
  46.         }
  47. }

  48. /*
  49. *异步回调函数处理
  50. */
  51. function callbackFunction()
  52. {
  53.         if(xmlhttp.readyState == 4)
  54.         {
  55.                 if(xmlhttp.status == 200)
  56.                 {
  57.                         var returnValue = xmlhttp.responseText;

  58.                         if(returnValue != null && returnValue.length > 0)
  59.                         {
  60.                                 document.getElementById("current_time").innerHTML = returnValue;
  61.                         }
  62.                         else
  63.                         {
  64.                                 alert("结果为空!");
  65.                         }
  66.                 }
  67.                 else
  68.                 {
  69.                         alert("页面出现异常!");
  70.                 }
  71.         }
  72. }
复制代码
服务器上应该有ajaxtest.cgi程序更新温度并返回温度

以上html稍微修改下 放到定时器定时调用应该就可以

评分

参与人数 1可用积分 +6 收起 理由
bitmilong + 6 我很赞同

查看全部评分

论坛徽章:
0
6 [报告]
发表于 2011-05-28 11:23 |只看该作者
谢谢大家,正如5楼所说,在sender()中,加上setTimeout,每隔两秒调用它本身就可以实现了。

论坛徽章:
0
7 [报告]
发表于 2012-04-24 09:16 |只看该作者
5楼大好人

论坛徽章:
0
8 [报告]
发表于 2012-12-18 16:39 |只看该作者
回复 5# armips
您好 我也在做这个 是通过串口时时采集温度 然后想显示在网页上面 一下是我的串口程序,麻烦你看看能不能实现
  1. #include <time.h>

  2. #include <fcntl.h>

  3. #include <stdio.h>

  4. #include <ctype.h>

  5. #include <errno.h>

  6. #include <stdlib.h>

  7. #include <string.h>

  8. #include <unistd.h>

  9. #include <signal.h>

  10. #include <termio.h>

  11. #include <syslog.h>

  12. #include <pthread.h>

  13. #include <sys/ipc.h>

  14. #include <sys/msg.h>

  15. #include <sys/wait.h>

  16. #include <sys/stat.h>

  17. #include <sys/time.h>

  18. #include <semaphore.h>

  19. #include <arpa/inet.h>

  20. #include <sys/types.h>

  21. #include <sys/socket.h>

  22. #include <bits/signum.h>

  23. #include <sys/resource.h>



  24. int uart_init(int arg, int baud)

  25. {

  26.     int fd;

  27.     char port[20];

  28.     struct termios Opt;

  29.     int uartbiit[50]= {B115200,B9600,B19200,B4800,B2400,B1200};

  30.     sprintf(port,"/dev/ttySAC%d",arg);  

  31.     //printf("Use port: %s \n", port);

  32.     fd = open(port, O_RDWR);     //打开串口

  33.     if (fd<0)

  34.     {

  35.         return -1;                 //没有打开返回

  36.     }



  37.     tcgetattr(fd,&Opt);      //初始化

  38.     tcflush(fd,TCIFLUSH);

  39.     cfsetispeed(&Opt,uartbiit[baud]);    //设置波特率

  40.     cfsetospeed(&Opt,uartbiit[baud]);



  41.     Opt.c_cflag |= CS8;                          //设置数据位

  42.     Opt.c_cflag &= ~PARENB;

  43.     Opt.c_oflag &= ~(OPOST);

  44.     Opt.c_cflag &= ~CSTOPB;

  45.     Opt.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);

  46.     Opt.c_iflag &= ~(INPCK|BRKINT|ICRNL|ISTRIP|IXON);



  47.     Opt.c_cc[VMIN] = 64;            //最大长度

  48.     Opt.c_cc[VTIME] = 1;            //超时时间



  49.     if (tcsetattr(fd,TCSANOW,&Opt) != 0)       //装载初始化参数

  50.     {

  51.         perror("SetupSerial!\n");

  52.         close(fd);

  53.         return -1;

  54.     }

  55.     return(fd);



  56. }



  57. int main()

  58. {

  59.     int fd, len = 0, i=0;

  60.     char buf[64];



  61.     if((fd = uart_init(1, 0)) <0)   //打开串口,波特率为115200;

  62.     {

  63.         printf("Open uart err \n");

  64.         return -1;

  65.     }

  66.     sprintf(buf, "Hello world !\n");   //输出内容

  67.           while(1)

  68.           {

  69.              memset(buf, 0 ,sizeof(buf));

  70.        while((len = read(fd,buf,64))>0)

  71.                    {

  72.             printf("%s\n",buf);

  73.                     }

  74.           }

  75.     return 0;

  76. }

复制代码

论坛徽章:
0
9 [报告]
发表于 2017-12-21 10:17 |只看该作者
楼主当年搞定了吗?能不能把源码分享下?感谢!

论坛徽章:
0
10 [报告]
发表于 2017-12-25 10:12 |只看该作者
请问当年楼主解决了吗?能否把源码分享下,感谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP