赞
踩
目 录
摘 要 I
Abstract II
前 言 1
/* 各个模块线程函数的接口 */ void *temperature_thread(){ voice_deal(); } void *invade_thread(){ invade_deal(); } void *soil_thread(){ soil_deal(); } void *light_thread(){ light_deal(); } void *rain_thread(){ rain_deal(); } void process_info(int s) { int recv_num; int send_num; char recv_buf[5]; char send_buf[5]; /* 各个模块的线程ID */ pthread_t temperature_pid; pthread_t soil_pid; pthread_t invade_pid; pthread_t light_pid; pthread_t rain_pid; int temperature_ret; int soil_ret; int invade_ret; int light_ret; int rain_ret; int *thread_ret = NULL; /* 主线程接收网络指令模块 */ while(1){ printf("begin recv:\n"); recv_num = recv(s,recv_buf,sizeof(recv_buf),0); if(recv_num <0){ perror("recv"); exit(1); } else { recv_buf[recv_num] = '\0'; printf("recv sucessful:%s\n",recv_buf); } if(!strcmp(recv_buf, temperatureOn)){ //温度相关模块操作 temperature_ret = pthread_create(&temperature_pid, NULL, (void*)temperature_thread, NULL); if(temperature_ret != 0){ printf("tempetature create pthread error\n"); return ; } printf("temperature pthread success\n"); }else if(!strcmp(recv_buf, temperatureOff)){ printf("temperature end work!\n"); if(0 != pthread_cancel(temperature_pid)){ printf("temperature cancel failed!\n"); } }else if(!strcmp(recv_buf, invadeOn)){ //入侵检测模块操作 printf("invade on!\n"); invade_ret = pthread_create(&invade_pid, NULL, (void *)invade_thread, NULL); if(invade_ret != 0){ printf("create invade pthread error\n"); return ; } printf("invade pthread success\n"); }else if(!strcmp(recv_buf, invadeOff)){ printf("invade off!\n"); if(0 != pthread_cancel(invade_pid)){ printf("invade cancel failed!\n"); } printf("invade cancel success!\n"); }else if(!strcmp(recv_buf, soilOn)){ //土壤湿度检测模块操作 printf("soil on!\n"); soil_ret = pthread_create(&soil_pid, NULL, (void *)soil_thread, NULL); if(soil_ret != 0){ printf("create soil pthread error!\n"); return ; } printf("soil pthread success!\n"); }else if(!strcmp(recv_buf, soilOff)){ printf("soil off!\n"); if(0 != pthread_cancel(soil_pid)){ printf("soil canael failed!"); } printf("soil cancel success!\n"); }else if(!strcmp(recv_buf, rainOn)){ //降雨检测模块操作 printf("rain on!\n"); rain_ret = pthread_create(&rain_pid, NULL, (void *)rain_thread, NULL); if(rain_ret != 0){ printf("create rain pthread error\n"); return ; } printf("rain pthread success\n"); }else if(!strcmp(recv_buf, rainOff)){ printf("rain off!\n"); if(0 != pthread_cancel(rain_pid)){ printf("rain cancel failed\n"); return ; } printf("rain cancel success\n"); }else if(!strcmp(recv_buf, lightVoiceOn)){ //光控开关模块操作 printf("light vodice on!\n"); light_ret = pthread_create(&light_pid, NULL, (void *)light_thread, NULL); if(light_ret != 0){ printf("create light pthread error\n"); return ; } printf("light pthread success\n"); }else if(!strcmp(recv_buf, lightVoiceOff)){ printf("light voice off!\n"); if(0 != pthread_cancel(light_pid)){ printf("light cancel failed\n"); return ; } printf("light cancel success\n"); }else{ continue; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。