⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c~

📁 基于GPRS的远程温度采集控制系统, 嵌入式系统, C源代码, 供做嵌入式开发及远程控制的朋友参考.
💻 C~
字号:
/************************************************ *using in the consideration room *to control motor and get temperature*************************************************/#define MWINCLUDECOLORS#include <termios.h>#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <time.h>#include <string.h>#include <pthread.h>#include "tty.h"#include "gprs.h"#include "motor.h"#include "ds18b20.h"//#include "draw.h"#include"nano-X.h"/*--------------------------------------------------------*/#define ENDMINITERM 27 /* ESC to quit miniterm */#define FALSE		0#define TRUE  		1/*--------------------------------------------------------*/static GR_WINDOW_ID w;//声明根窗口static GR_GC_ID gc;//声明根窗口上下文volatile int STOP=FALSE;int GET_GPRS_OK=FALSE;int baud=B115200;int mflag=0;int get_baudrate(int argc,char** argv);char numbuf[11];char cnumbuf[11];char tempstr[]="Temperature=";void * keyshell() ;float T_extraction(char* str){    float temprature;    sscanf(str,"Temperature=%f",&temprature);//发送温度是的格式为Temperature=36.625    return temprature;}int M_extraction(char* str){    int flag;    sscanf(str,"MOTOR:%d",&flag);    return flag;}char* Num_extraction(char* str){       //+CMGR: "REC UNREAD","+8615054117344",,"08/07/02,08:25:22+32";    int i=0;    int j=0;    for(i=24;i<35;i++)     {         numbuf[j]=str[i];       j++;     }    return numbuf;}char* Numc_extraction(char* str){       //+CLIP: "13864119870",129OK!    int i=0;    int j=0;    for(i=8;i<19;i++)     {         cnumbuf[j]=str[i];        j++;     }    return cnumbuf;}int analyze_str(char *str)  {	     char num[3]="";	     int flag=0;        int i;        float rtemperature=0.0;        char* text="";	if(strstr(str,"+CMTI"))//read message	{		while(*str !='\0')	        {         if(!flag && *str>='0' && *str<='9')			{				num[0]=*str ;				flag = 1 ;			}			else if(flag)			{            if(*str>='0' && *str<='9')				{					num[1]=*str ;					flag = 2 ;					}            else               num[1]=' ' ;				break ;			}			str++ ;        	}		num[2]='\0';		            tty_writecmd("AT+CMGF=1",strlen("AT+CMGF=1"));		tty_write("AT+CMGR=",strlen("AT+CMGR="));		tty_writecmd(num,flag);		tty_writecmd("AT+CMGD=1",strlen("AT+CMGD=1"));      usleep(5000);		usleep(5000);            	}	else if(strstr(str,"+CMGR"))	{	    Num_extraction(str);       printf("\n");	}	else if(strstr(str,"MOTOR:"))	{	     printf("\nMessage:%s\n",str);        mflag=M_extraction(str);        printf("\ncontrol flag:%d\n",mflag);        run(mflag);        	}   else if(strstr(str,"+CLIP:"))         {       printf("\n!!!!!CCCC!!!!!");       gprs_hold();       Numc_extraction(str);       printf("\n%s is calling in....\n",cnumbuf);       GrText(w,gc,140,120,"     ",-1,GR_TFASCII);       GrText(w,gc,40,40,"                                 ",-1,GR_TFASCII);       GrText(w,gc,40,70,cnumbuf,-1,GR_TFASCII);       if(strncmp(cnumbuf,"13706414710",11)==0)                 {                       	if(mflag==0)               			    {         		 run(1);         		 mflag=1;                  		   }               else                	       {                run(0);                mflag=0;                               }                }      else  		  { 			    system("sound/wavplay sound/1.wav");             printf("playing <<<<>>>>>>>>>>\n");             GrText(w,gc,40,40,"unkown number call in! do nothing",-1,GR_TFASCII);             printf("unkown number call in! do nothing\n");             gprs_sendmsg(strcat("unkown number call in!",cnumbuf),"13706414710");       		 }            } else if(strstr(str,"REQ")) 	{	  GrText(w,gc,40,40,"                                 ",-1,GR_TFASCII);     GrText(w,gc,40,70,numbuf,-1,GR_TFASCII);	     GrText(w,gc,140,120,"     ",-1,GR_TFASCII);     if(strncmp(numbuf,"13706414710",11)==0)             {      	printf("\n This is a reqest to test T\n");			rtemperature=0.0;      	printf("----start----\n");      	rtemperature=ReadTemperature();      	sleep(1);      	rtemperature=ReadTemperature();      	if(rtemperature>24.0)             			 {		  			 system("sound/wavplay sound/1.wav");        			 printf("playing <<<<>>>>>>>>>>\n");                run(1);//start motor                mflag=1;              			}       	printf("*********************************\n");       	printf("Room Temperature=%f `C\n",rtemperature);       	printf("\n*********************************\n");       	printf("\n----end------\n");       	sprintf(text,"%.2f",rtemperature);      	gprs_sendmsg(strcat(tempstr,text),numbuf);			printf("The number is %s\n",numbuf);      	//show temperature      	GrText(w,gc,140,120,"     ",-1,GR_TFASCII);      	GrText(w,gc,140,120,text,-1,GR_TFASCII);         printf("Message sent!\n");     		 }		else  		 {      		system("sound/wavplay sound/1.wav");     		printf("playing <<<<>>>>>>>>>>\n");     		GrText(w,gc,40,40,"unkown number call in! do nothing",-1,GR_TFASCII);     		printf("unkown number send req! do nothing\n");         gprs_sendmsg(strcat("unkown number send req!",numbuf),"13706414710");       		} }	return 0 ;}/*--------------------------------------------------------*//* modem input handler */void* gprs_read(void * data){	int i=0;	char c;	char buf[1024];  	printf("\nread modem\n");  	while (STOP==FALSE) {		   	tty_read(&c,1);		buf[i]=c ;		i++ ;				if(c=='\n')		{	         buf[i]='\0' ;         analyze_str(buf);			i = 0 ;		}		printf("%c",c);		}  	printf("exit from reading modem\n");  	return NULL; }/*--------------------------------------------------------*/int main(int argc,char** argv){	int ok; 	pthread_t th_a, th_b, th_show; 	void * retval;			if(argc < 2) {     	printf ("Default baudrate is 115200 bps.\n");      printf (" If not, please enter the baudrate as a parameter\n");	}   else		baud=get_baudrate(argc, argv);		tty_init();   if (GrOpen()<0)             {       printf("Can't open graphics\n");       exit(1);             }    w=GrNewWindow(GR_ROOT_WINDOW_ID,10,10,300,200,3,GREEN,RED);//实例化根窗口    gc=GrNewGC();//实例化上下文    GrMapWindow(w);//显示窗口    GrSetGCForeground(gc,YELLOW);    GrSetGCBackground(gc,GREEN);    GrText(w,gc,30,15,"Room 1:",-1,GR_TFASCII);    //GrText(w,gc,40,40,"dfdfddf",-1,GR_TFASCII);    //GrText(w,gc,40,70,"hhttt",-1,GR_TFASCII);    GrText(w,gc,40,120,"Temperature=",-1,GR_TFASCII);    GrText(w,gc,190,120,"`C",-1,GR_TFASCII);   pthread_create(&th_a, NULL, keyshell, 0);  	pthread_create(&th_b, NULL, gprs_read, 0);	while(!STOP){		usleep(100000);	}  	tty_end();   run(0);   GrClose();  	exit(0); }int get_baudrate(int argc,char** argv){	int v=atoi(argv[1]);	 switch(v){		case 4800:			return B4800;					case 9600:			return B9600;					case 19200:			return B19200;					case 38400:			return B38400;					case 57600:			return B57600;					case 115200:			return B115200;				default:			return -1;				 } 	 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -