ds18b20采集温度并上报服务器

------------------------------------------------------------------------------------------------------------------------------

交叉编译器:arm-linux-gcc-4.5.4

Linux内核版本:Linux-3.0

主机操作系统:Centos 6.5

开发板:FL2440

温度传感器:ds18b20

注:此程序的客户端是在装有ds18b20模块并有ds18b20驱动的系统上跑的,本人写的ds18b20的驱动

------------------------------------------------------------------------------------------------------------------------------

一、socket图解

socket是“open—write/read—close”模式的一种实现,而在socket编程里面,socket()就是open()函数了。socket编程的流程图示已经很清楚,各个函数的用法这里就懒得说了,也说不好,网上有一堆一堆的专业人士的解释,非专业人士还是直接上代码的比较好。

 

二、客户端代码:

  1 /*********************************************************************************
  2  *      Copyright:  (C) 2016 2013dianxin_3
  3  *                  All rights reserved.
  4  *
  5  *       Filename:  mclient.c
  6  *    Description:  This file 
  7  *                 
  8  *        Version:  1.0.0(08/16/2016)
  9  *         Author:  xiaohexiansheng <1392195453@qq.com>
 10  *      ChangeLog:  1, Release initial version on "08/16/2016 07:16:44 PM"
 11  *                 
 12  ********************************************************************************/
 13 #include <sys/types.h>
 14 #include <sys/ioctl.h>
 15 #include <termios.h>
 16 #include <sys/stat.h>
 17 #include <fcntl.h>
 18 #include <sys/time.h>
 19 #include <stdio.h>
 20 #include <stdlib.h>
 21 #include <string.h>
 22 #include <errno.h>
 23 #include <sys/types.h>
 24 #include <sys/socket.h>
 25 #include <netinet/in.h>
 26 
 27 #define MAXLINE                 4096
 28 /********************************************************************************
 29  *  Description:
 30  *   Input Args:
 31  *  Output Args:
 32  * Return Value:
 33  ********************************************************************************/
 34 double get_tem(void)        //获取温度的函数
 35 {
 36     int fd;
 37     double result = 0;
 38     unsigned char buff[2];
 39     unsigned short temp = 0;
 40     int flag = 0;
 41 
 42     if ((fd=open("/dev/ds18b20",O_RDWR | O_NDELAY | O_NOCTTY)) < 0)
 43     {
 44         perror("open device ds18b20 failed.\r\n");
 45         exit(1);
 46     }
 47 
 48     printf("open device ds18b20 success.\r\n");
 49 
 50     read(fd, buff, sizeof(buff));
 51     temp=((unsigned short)buff[1])<<8;
 52     temp|=(unsigned short)buff[0];
 53     result=0.0625*((double)temp);
 54     close(fd);
 55     printf("temperature is %4f \r\n", result);
 56     return result;
 57 }
 58 
 59 int main (int argc, char **argv)
 60 {
 61     double      temperature;
 62     int         sockfd;
 63     char        temp[25];
 64     char        sendline[MAXLINE];
 65     struct sockaddr_in servaddr;
 66     
 67     if(argc != 2)        //主函数中需要获得服务器的地址作为第二参数
 68     {
 69         printf("usage: ./client <ipaddress>.\n");
 70         return 0;
 71     }
 72 
 73     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)        //如果打开失败则打印错误并返回
 74     {
 75         printf("create socket error: %s(errno: %d).\n", strerror(errno), errno);
 76         return 0;
 77     }
 78 
 79     memset(&servaddr, 0, sizeof(servaddr));
 80     servaddr.sin_family = AF_INET;
 81     servaddr.sin_port = htons(6666);
 82     if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)        //将点分十进制ip转换为点分二进制ip
 83     {
 84         printf("inet_pton error for %s.\n", argv[1]);
 85         return 0;
 86     }
 87 
 88     if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)        //建立连接
 89     {
 90         printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
 91         return 0;
 92     }
 93 
 94     temperature = get_tem();        //获取温度
 95     gcvt(temperature, 4, temp);        //将浮点型数转换为字符串
 96     memset(sendline, 0, sizeof(sendline));
 97     strcpy(sendline, temp);
 98     if(send(sockfd, sendline, strlen(sendline), 0) < 0)
 99     {
100         printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
101         return 0;
102     }
103 
104     close(sockfd);
105     return 0;
106 } /* ----- End of main() ----- */

 

三、服务器端代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<errno.h>
 5 #include<sys/types.h>
 6 #include<sys/socket.h>
 7 #include<netinet/in.h>
 8 #include <sys/time.h>
 9 #include<time.h>
10 
11 #define MAXLINE 4096
12 
13 int main(int argc, char** argv)
14 {
15     int         n;
16     int         listenfd;
17     int         connfd;
18     char        buff[4096];
19     struct  sockaddr_in     servaddr;
20     
21     if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )        //打开socket,若失败则打印失败信息并返回
22     {
23         printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
24         exit(0);
25     }
26 
27     memset(&servaddr, 0, sizeof(servaddr));
28     servaddr.sin_family = AF_INET;
29     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
30     servaddr.sin_port = htons(6666);
31 
32     if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
33     {
34         printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
35         exit(0);
36     }
37 
38     if(listen(listenfd, 10) == -1)        //监听串口
39     {
40         printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
41         exit(0);
42     }
43 
44     printf("======waiting for client's request======\n");
45     while(1)
46     {
47         memset(buff, 0, sizeof(buff));
48         time_t rawtime;
49         struct tm* timeinfo;
50         char timE[80];
51         time(&rawtime);
52         timeinfo=localtime(&rawtime);
53         strftime(timE,80,"%y-%m-%d %I:%M:%S",timeinfo);        //获取系统时间
54         
55         if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1)
56         {
57             printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
58             continue;
59         }
60         n = recv(connfd, buff, MAXLINE, 0);        //从客户端获取数据,并返回数据大小
61         strcat(buff, "℃\0");
62         printf("the degrees is: %s %s\n", buff, timE);
63         close(connfd);
64     }
65     close(listenfd);
66 }
posted @ 2016-08-17 02:47  小禾先生  阅读(866)  评论(0编辑  收藏  举报