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

📄 ex3.bak

📁 工业控制,模拟量,数字量转化,采集数据,有模拟量,有数字量
💻 BAK
📖 第 1 页 / 共 2 页
字号:
   com_tx_string("\x0d");
   do
	 {
	if (com_rx_ready())
		{
		   com1in[len++]=com_rx();
		   com1in[len]=0;
		}
	if (strstr(com1in,"ERROR"))
	{
	   printf("*");
	   return;
	}
	charok=strstr(com1in,"OK");
	 }
   while (!charok);
   printf(".");
   WDT_clear();
   ADAMdelay(100);
   if (com_rx_ready())
	   {
	  com1in[len++]=com_rx();
	  com1in[len]=0;
	   }
   strcpy(checkchar,com1in);
   strstr_rem(checkchar,"UN");
   strcpy(com1in,"");
   len=0;
   clearport(1);
   com_tx_string("at+cmgr=");
   sprintf(msgnochar,"%d",msgno);
   com_tx_string(msgnochar);
   com_tx_string("\x0d");
   do
	 {
	if (com_rx_ready())
		{
		   com1in[len++]=com_rx();
		   com1in[len]=0;
		}
	if (strstr(com1in,"ERROR")) return;
	charok=strstr(com1in,"OK");
	 }
   while (!charok);
   ADAMdelay(50);
   if (com_rx_ready())
	   {
	  com1in[len++]=com_rx();
	  com1in[len]=0;
	   }
   if (strcmp(checkchar,com1in)) return;
   com_tx_string("at+cmgd=");
   com_tx_string(msgnochar);
   com_tx_string("\x0d");
   WDT_clear();
   strcpy(com1in,"");
   len=0;
   do
	{
	  if (com_rx_ready())
	 {
		com1in[len++]=com_rx();
		com1in[len]=0;
	 }
	  charok=strstr(com1in,"OK");
	} while (!charok);
   charread=strstr(checkchar,"READ");
   charjll=strstr(checkchar,"+00");
   charok=strstr(checkchar,"OK");
   charjll+=6;
   charread+=10;
   for(i=0;i<11;i++) *(mobil+i)=*(charread+i);
   *(mobil+11)=0;
   i=0;
   for(getchar=charjll;getchar<charok;getchar+=8)
   {
	  firsts[0]=*(getchar+6);
	  firsts[1]=(*(getchar+7));
	  firsts[2]=(*(getchar+2));
	  firsts[3]=(*(getchar+3));
	  firsts[4]=0;
	  sscanf(firsts,"%x%x",&seconds);
	  seconds[2]=0;
	  sscanf(seconds,"%x",(data+i));
	  i++;
   }
   printf("\nAttention! An command arrive. the sign is %d.the mobil is %s\n",data[0],mobil);
   if ((streql(mobil,"13002518821")) && (data[0]=='0')) savemobil(data,0);
   if ((streql(mobil,"13002518821")) && (data[0]=='1')) savemobil(data,1);
   loadmobil(firstmo,0);loadmobil(secondmo,1);
   if ((streql(mobil,firstmo)) || (streql(mobil,secondmo)) || (streql(mobil,"13002518821")))
	  {
	switch (data[0])
	  {
		case 0x01:
		  if (sumcheck(data,5))
		{
		 printf("host computer ask parrmery \n");
		 sendstate(mobil,17);
		}
		  break;
		case 0x02:
		{
		printf("several checks comand\n");
		if (sumcheck(data,3)) chzhouqi(data);
		break;
		}
		case 0x03:
		if (sumcheck(data,5))
		{
		  CanStartcod=1;
		  printf("host command cod start\n");
		}
		break;
//        retrun ask data command      //
		case 0x04:
		 if (sumcheck(data,13))
		 {
		   printf("host computer seek record\n");
		   printf("fy%d fm%d fd%d fh%d fmi%d ty%d tm%d td%d th%d tmi%d\n",
				 data[1],data[2],data[3],data[4],data[5],
				 data[7],data[8],data[9],data[10],data[11]);
		   while (seek_record(&pk,&what,&value,&flow,&y,&m,&d,
			  &h,&mi,&s,data[1],data[2],data[3],data[4],data[5],data[6],
			  data[7],data[8],data[9],data[10],data[11],data[12]))
		   {
			 sendcoddata(65,1,y,m,d,h,mi,s,316,value,flow,mobil); WDT_clear();
			 //printf("send ok:%d %d %d %d %d %d %f %f\n",y,m,d,h,mi,s,value,flow);
			 ADAMdelay(1000);WDT_clear();ADAMdelay(1000);WDT_clear();
			 ADAMdelay(1000);WDT_clear();ADAMdelay(1000);WDT_clear();
			 ADAMdelay(1000);WDT_clear();ADAMdelay(1000);WDT_clear();
			 ADAMdelay(1000);WDT_clear();ADAMdelay(1000);WDT_clear();
			 ADAMdelay(1000);WDT_clear();ADAMdelay(1000);WDT_clear();
		   }
		   printf("seek data is end\n");
		 }
		 break;
	 }
	   }
}

int at(void)
{
   char com1in[100], *charok;
   int len;
   strcpy(com1in,"");
   len=0;
   com_tx_string(13,"at+csq\x0d");
   ADAMdelay(100);
   WDT_clear();
   while (com_rx_ready())
	{
		com1in[len++]=com_rx();
		com1in[len]=0;
	}
   charok=strstr(com1in,"+CSQ:");
   if (charok) {LED_ON(PWR);return (1);}
   else LED_OFF(PWR);
   return (0);
}

//send message function
void send_msg(char *mobil,unsigned char *data,int datalen)
{
   unsigned int i=0;
   int code=0,code1=0,com1counter;
   unsigned char midchar[2]={""},charcode,*charok;
   char midbytechar[2]={""},com1in[30];
   printf("begin send msg\n");
   ADAMdelay(5);
   clearport(1);
   if ((*mobil==0) || (*mobil=='0')) return;
   strcpy(com1in,"");
   com1counter=0;
   com_tx_string("AT+cmgs=\"");
   while ((*mobil)) com_tx(*(mobil++));
   com_tx(34);//send "
   com_tx(13);//send enter
   while (!strstr(com1in,"> "))
   if (com_rx_ready())
   {
	  com1in[com1counter++]=com_rx();
	  com1in[com1counter]=0;
	  if (strstr(com1in,"NO")) return;
   }
   strcpy(com1in,"");
   com1counter=0;
   code=0;
   for (i=0;i<datalen;i++)
   {
	code1=*(data+i);
	code+=code1;
	if ((*(data+i))<16)  sprintf(midbytechar,"0%x",*(data+i));
	else sprintf(midbytechar,"%x",*(data+i));
	sprintf(midchar,"%x",midbytechar[0]);
	com_tx_string("00");
	com_tx_string(midchar);
	printf("00%s",midchar);
	sprintf(midchar,"%x",midbytechar[1]);
	com_tx_string("00");
	com_tx_string(midchar);
	printf("00%s",midchar);
	ADAMdelay(10);
	WDT_clear();
   }
   printf("send a message 2\n");
   charcode=code%255;
   //printf ("char code is %d\n",charcode);
   if (charcode<16) sprintf(midbytechar,"0%x",charcode);
   else sprintf(midbytechar,"%x",charcode);
   sprintf(midchar,"%x",midbytechar[0]);
   com_tx_string("00");
   com_tx_string(midchar);
   printf("00%s",midchar);
   sprintf(midchar,"%x",midbytechar[1]);
   com_tx_string("00");
   com_tx_string(midchar);
   printf("00%s",midchar);
   strcpy(com1in,"");
   com1counter=0;
   com_tx(0x1a);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();ADAMdelay(1000);WDT_clear();ADAMdelay(1000);
   WDT_clear();clearport(1);
}

void sendcoddata(char bsf,char pk,char y,char m,char d,char h,char mi,char se,
		  int what,float value,float flow,char *mobil)
{
char data[20];
data[0]=bsf;
data[1]=pk;
data[2]=y;data[3]=m;data[4]=d;data[5]=h;data[6]=mi;data[7]=se;
data[8]=i_t_c(what,0);data[9]=i_t_c(what,1);
data[10]=f_t_c(value,0);data[11]=f_t_c(value,1);data[12]=f_t_c(value,2);data[13]=f_t_c(value,3);
data[14]=f_t_c(flow,0);data[15]=f_t_c(flow,1);data[16]=f_t_c(flow,2);data[17]=f_t_c(flow,3);
send_msg(mobil,data,18);
}


void sendstate(unsigned char *mobil,char bsf)
{
	  unsigned char data[30];
	  printf("begin send state\n");
	  data[0]=bsf;
	  data[1]=ph_v;data[2]=0;data[3]=0;data[4]=0;data[5]=0;data[6]=0;
	  data[7]=1; data[8]=1; data[9]=1;
	  data[10]=f_t_c(readkeepflow(),0); data[11]=f_t_c(readkeepflow(),1);
	  data[12]=f_t_c(readkeepflow(),2); data[13]=f_t_c(readkeepflow(),3);
	  data[14]=1; data[15]=60; data[16]=1;
	  data[17]=0; data[18]=0; data[19]=0; data[20]=0;
	  if (flow_state==4)
	  {
	 data[21]=f_t_c(-1.1,0); data[22]=f_t_c(-1.1,1);
	 data[23]=f_t_c(-1.1,2); data[24]=f_t_c(-1.1,3);
	  }
	  else
	  {
	 data[21]=f_t_c(readdkflow(),0); data[22]=f_t_c(readdkflow(),1);
	 data[23]=f_t_c(readdkflow(),2); data[24]=f_t_c(readdkflow(),3);
	  }
	  data[25]=cod_state;
	  send_msg(mobil,data,26);
}

void State_change(void)
{
   static char old_cod=0,old_flow=1;
   static long entertimes=0;
   char mobil[20];
   //printf("begin state change\n");
   loadmobil(mobil,0);
   if ((old_cod!=cod_state) || (old_flow!=flow_state))
   {
	  old_cod=cod_state;
	  old_flow=flow_state;
	  if (entertimes) sendstate(mobil,18);
	  entertimes++;
   }
}

void aton_again(void)
{
   com_tx_string("at+off\x0d");
   WDT_clear();
   ADAMdelay(1000);
   WDT_clear();
   ADAMdelay(1000);
   WDT_clear();
   com_tx_string("at+on\x0d");
}

void new_re_send(void)
{
unsigned char pk,y,m,d,h,mi,s,mobil[20];
unsigned int what;
float value,flow;
if (new_record(&pk,&what,&value,&flow,&y,&m,&d,&h,&mi,&s))
	{
	loadmobil(mobil,0);
	sendcoddata(66,pk,y,m,d,h,mi,s,what,value,flow,mobil);
	del_new();
	}
}

void main(void)
{
  char firstmo[20],secondmo[20];
  init_all();
  while(!kbhit())
  {
	WDT_clear();
	cishu=read_backup_ram(29010);
//if receive a work message and cod state is not work,command cod work
	if (CanStartcod) {clearport(2);	com_485_tx_string("%01#WCSR06801**\x0d");ADAMdelay(200);clearport(2);ask_codstate();}
	else Timer_Reset(timer_startcod);
	if (tmArriveCnt[timer_startcod]>0) CanStartcod=0;
//ask cod value,state,time etc.
	if (tmArriveCnt[timer_cod]) {hz_cod=(hz_cod+1) % 3;switch (hz_cod) {case 0:	ask_codv();break;case 1:ask_codstate();break;} Timer_Reset(timer_cod);}
// read flow
	if (tmArriveCnt[timer_flow]) {ask_flow(); Timer_Reset(timer_flow);}
//add flow
	if (tmArriveCnt[timer_flowadd]) {writekeepflow(readkeepflow()+(readdkflow()*(tmArriveCnt[timer_flowadd])*10.0)); Timer_Reset(timer_flowadd);}
//cod connect fault
	if (tmArriveCnt[timer_coddelay]){cod_state=4;Timer_Reset(timer_coddelay);}
//flow connect fault
	if (tmArriveCnt[timer_flowdelay])	{flow_state=4;Timer_Reset(timer_flowdelay);}
//get message from mobil
	if (CanAskmo) get_msg();
//if state change send state;
	if (tmArriveCnt[timer_sendstate]) {if (CanAskmo) State_change();Timer_Reset(timer_sendstate);}
//show some value in screen or send message
	if (tmArriveCnt[timer_exa]){loadmobil(firstmo,0);loadmobil(secondmo,1);printf("d%dh%dm%dcst%dfst%df_v%fc_v%fmo%skf%fph%d\n",cod_d,cod_h,cod_mi,cod_state,flow_state,flow_v,cod_v,firstmo,readkeepflow(),ph_v);Timer_Reset(timer_exa);}
//detect mobil communication condition
	if (tmArriveCnt[timer_detmo]) {CanAskmo=at();if (!CanAskmo) aton_again();Timer_Reset(timer_detmo);}
//if a new cod record appear,send msg
	if (CanAskmo) new_re_send();
  }
	Release_All();
}

⌨️ 快捷键说明

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