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

📄 pdu手机采老威胜电量成功 main.c

📁 PIC18F6520+SIM300做的透明传输的DTU
💻 C
📖 第 1 页 / 共 5 页
字号:
//比较收到的是否短信通知 +CMTI: 	  
		else if(strncmppgm2ram(RecvFromMS,PLUSCMTI,L_PLUSCMTI) == 0)
  	  	{
  	    	Proc_SMS();
			FrameFlag.Bit.EchoModule = 0 ;
	  		Flags.fCOMM = 0;
  			ByteRecvUART2 = 0;
  	  	}
		else if(strncmppgm2ram(RecvFromMS,Module_RDY,RDY_SIZE) == 0)
  	  	{//对module进行了复位
 			Module_Init();	//只复位不掉电,module是否还是UCS编码?
			ClearCommREG();
  	  	}
		else
	  	{//收到的不是短信,错误处理
			ClearCommREG();
	  	}
	}//END of if(FrameFlag.Bit.EchoModule)

//********************************************************************
//改成:cRet = strncmppgm2ram()

	if(FrameFlag.Bit.CMD_Frame == 1)
	{
		for(j=0;j<Length;j++)
		{
	  		BUF_METER[j] = RecvFromMS[IPDHead + j]; 	//将主站的接收缓冲区的数据
		}					//转送到电表的通讯缓冲区
		SendCMDtoMeter(j);			
		ClearCommREG();
	}

//********************************************************************
	if(FrameFlag.Bit.REV_Meter == 1)
	{
/*
  	    if(BUF_METER[8] == 0x04 && BUF_METER[9] == 0x0C)
  	    {//将多功能表的表号置入DTU
  	    	for(k=0;k<6;k++)
  	    	{
  	    		MeterNumber[k] = BUF_METER[k+0x10] - 0x33;
  	    	}
			W_eeprom(MeterNumber,MeterNO,6);
			Nop();
			FrameFlag.Bit.REV_Meter = 0;
  	    	
  	    }
*/
		Nop();
  	    if(TempFlag.Bit.SMS_GetData == 1)
  	    {
			if(TempFlag.Bit.isWEISHENG == 1)
				WS_RetSMS();
  	    	else 
  	    		RetSMS();
  	    }
  	    if(TempFlag.Bit.GPRS_CMD ==1)
  	    {//GPRS采数
			TempFlag.Bit.GPRS_CMD = 0;
			
  	    	LoadCMD_CIPSEND();
	  		WaitReturn = 0;
	  		dLength = BUF_METER[9]+12;
	  		if(dLength>=100)
	  		{
	  			tempArr[0] = dLength / 100;						//求百位
	  			temp2 = dLength-(dLength / 100) * 100;	
	  			tempArr[1] = temp2 / 10;						//求十位
	  			tempArr[2] = temp2-(temp2 / 10) * 10;			//求个位
	  			TranToMS[CIPSEND_SIZE] = _EQUAL;
	  			TranToMS[CIPSEND_SIZE + 1] = tempArr[0]+0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 2] = tempArr[1]+0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 3] = tempArr[2]+0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 4] = _CR;	
  	  			SendToModule (CIPSEND_SIZE + 5);
	  		}  
	  		else if(dLength >= 10)
	  		{
	  			tempArr[0] = dLength/10;						//求十位
	  			tempArr[1] = dLength-(dLength/10)*10;			//求个位
	  			TranToMS[CIPSEND_SIZE] = _EQUAL;
	  			TranToMS[CIPSEND_SIZE + 1] = tempArr[0]+0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 2] = tempArr[1]+0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 3] = _CR;	
  	  			SendToModule (CIPSEND_SIZE + 4);
 	  		}	
	  		else		//dLength<10
	  		{
	  			TranToMS[CIPSEND_SIZE] = _EQUAL;
	  			TranToMS[CIPSEND_SIZE + 1] = dLength + 0x30;	//转换成字符
	  			TranToMS[CIPSEND_SIZE + 2] = _CR;	
  	  			SendToModule (CIPSEND_SIZE + 3);
 	  		}	
  	  		delay10ms ();
	  		for(k=0;k<dLength;k++)
	  		{
	  			TranToMS[k] = BUF_METER[k]; 
	  		}				
	  		WaitReturn = 0;
  	  		SendToModule (dLength);		//把从电表那收到的回送上传给主站

  			for(k = 40;k>0;k--) 	//延时2秒
  			{
	  			if(FrameFlag.Bit.EchoModule == 1)
	  				break;
	  			delay50ms ();
  			}
 	  		while(FrameFlag.Bit.EchoModule == 0)	//等待返回SEND OK
	  		{
		    	Nop();
		    	RWDTemp = FiveSeconds;		//等待FiveSeconds秒
	    		if(RevWaitDelay>RWDTemp) 		
	    		{
		  		//下电、上电、延时、初始化,重发cipstart
  	  				SendToModule (dLength);		//重发
  	  				RevWaitDelay = 0;
	    		}//END if(RevWaitDelay>FiveSeconds)
	  		}//END while(FrameFlag.Bit.EchoModule == 0)
	  		Nop();
  			FrameFlag.Bit.REV_Meter = 0;	//清除“电表回送一帧”标志
  	  		cRet = strncmppgm2ram(RecvFromMS,SEND_OK,L_SEND_OK);
  	  		if(cRet == 0)
  	  		{
				MinCounter = 0;
  	  		}
  	  		else
  	  		{
  	  			Proc_err();
  	  		}
  	  	}
	}//END if(FrameFlag.Bit.REV_Meter == 1)
  }//END while (1)
}//END of main()

// *******    子函数   ***************
void B2ASC(void)
{
		if(temp2>=0 && temp2<=9)	//如果字符是十进制数字,返回非0
		{
			temp2 += 0x30;						
		}
		else
		{
			temp2 += 55;
		}
}

void Load645Header(void)
{
	BUF_METER[0] = 0xFE;
	BUF_METER[1] = 0xFE;
	BUF_METER[2] = 0xFE;

	BUF_METER[3] = 0x68;
	BUF_METER[4] = 0x99;
	BUF_METER[5] = 0x99;
	BUF_METER[6] = 0x99;
	BUF_METER[7] = 0x99;
	BUF_METER[8] = 0x99;
	BUF_METER[9] = 0x99;
/*	
FE FE FE FE 68 70 54 00 72 00 00 68 81 16 52 C3 B3 79 34 33 33 33 33 33 86 7C 33 33 AC 87 33 33 7B 75 33 33 68 16 
//读出表号

  EEADRH = 0;
  EEADR = MeterNO;
  EECON1bits.EEPGD = 0;	// Point to DATA memory
  EECON1bits.CFGS = 0;
  LengthIP = 0;
  for (k = 0; k < 6; k++)
  {
  	EECON1bits.RD=1;
	temp1 = EEDATA;
//	if(temp1 == 0x00) break;

	BUF_METER[k + 4] = temp1;
	EEADR++;
  }
*/	
	BUF_METER[10] = 0x68;
	BUF_METER[11] = 0x01;
	BUF_METER[12] = 0x02;
	
}

void ASC2B(void)
{
		if(isdigit(temp2))	//如果字符是十进制数字,返回非0
		{
			temp2 -=0x30;						
		}
		else if(isalpha(temp2))
		{
    		switch (temp2)
    		{
      			case 'A':
      			case 'a':
              		temp2 = 0x0A;
              		break;
      			case 'B':
      			case 'b':
              		temp2 = 0x0B;
              		break;
      			case 'C':
      			case 'c':
              		temp2 = 0x0C;
              		break;
      			case 'D':
      			case 'd':
              		temp2 = 0x0D;
              		break;
      			case 'E':
      			case 'e':
              		temp2 = 0x0E;
              		break;
      			case 'F':
      			case 'f':
              		temp2 = 0x0F;
              		break;
      			default:  
              	return;
    		}    
		}
}
void Proc_SMS(void)
{
	unsigned char ii,DotCounter;
	unsigned int cSum = 0;

	SMS_Start = 0x10;
	
	Index0 = RecvFromMS[15];	//取得这个短信的INDEX
	Index1 = RecvFromMS[16];	//INDEX不能大于99
	LoadCMD_CMGR();
	WaitReturn = 4;
	SendToModule (CMGR_CMGD_SIZE);
  	delay50ms ();
  	delay50ms ();
  	TempFlag.Bit.f0 = 0;
	while(TempFlag.Bit.f0 == 0)
	{
		Nop();
  		if(FrameFlag.Bit.EchoModule == 1)
  		{
  			ClearCommREG();
  			TempFlag.Bit.f0 = 1;
	  	}
	  	else
	  	{
	  	  	RWDTemp = TenSeconds;		////
	  		if(RevWaitDelay > RWDTemp) 	//等待x秒
	  		{
				WaitReturn = 4;
				SendToModule (CMGR_CMGD_SIZE);
  				delay50ms ();
	  		}
	  	}

	}//END while(1)

//加上超时判断,除法改为移位    			
//取出 Address_Length
	temp3 = CMGR_Echo+MGHeader-2;
	if(RecvFromMS[temp3]>='A' && RecvFromMS[temp3]<'G')
	  temp1 = RecvFromMS[temp3]-0x41+0x0A;
	else if(RecvFromMS[temp3]>='0' && RecvFromMS[temp3]<='9')
	  temp1 = RecvFromMS[temp3]-0x30;
	else
	{
		ErrorMSG();							//删除该短信
	  	return;
	}
	  
	temp3 += 1;
	if(RecvFromMS[temp3]>='A' && RecvFromMS[temp3]<'G')
	  temp2 = RecvFromMS[temp3]-0x41+0x0A;
	else if(RecvFromMS[temp3]>='0' && RecvFromMS[temp3]<='9')
	  temp2 = RecvFromMS[temp3]-0x30;
	else
	{
		ErrorMSG();							//删除该短信
	  	return;
	}

	Address_Length = temp1 << 4;	//  *16+temp2
	Address_Length = Address_Length + temp2;
	temp3 = Address_Length & 0x01;
	if(temp3 == 1)	//奇数,0DH,要加填充位
	  Address_Length++;
	  
//取出 TP_UDL
	IndexOfTP_UDL = CMGR_Echo + MGHeader + 2 + Address_Length + 18;//18=(1+1+7)*2
//	temp3 = CMGR_Echo+MGHeader-2;
	if(RecvFromMS[IndexOfTP_UDL]>='A' && RecvFromMS[IndexOfTP_UDL]<'G')
	  temp1 = RecvFromMS[IndexOfTP_UDL]-0x41+0x0A;
	else if(RecvFromMS[IndexOfTP_UDL]>='0' && RecvFromMS[IndexOfTP_UDL]<='9')
	  temp1 = RecvFromMS[IndexOfTP_UDL]-0x30;
	else
	{
		ErrorMSG();							//删除该短信
	  	return;
	}
	  
	if(RecvFromMS[IndexOfTP_UDL + 1]>='A' && RecvFromMS[IndexOfTP_UDL + 1]<'G')
	  temp2 = RecvFromMS[IndexOfTP_UDL + 1]-0x41+0x0A;
	else if(RecvFromMS[IndexOfTP_UDL + 1]>='0' && RecvFromMS[IndexOfTP_UDL + 1]<='9')
	  temp2 = RecvFromMS[IndexOfTP_UDL + 1]-0x30;
	else
	{
		ErrorMSG();							//删除该短信
	  	return;
	}

	TP_UDL = temp1 << 4;	//  (*16+temp2)/2;
	TP_UDL = TP_UDL + temp2;
	TP_UDL = TP_UDL >> 1;
////15个字节长的IP地址+port=2020(9个字节长)
	DataAddr = IndexOfTP_UDL + 2;

/*收到 DL(电量),UDL=04,意指0044 004C,但实际发送过来是8个字节:
30 30 34 34 30 30 34 43

TP_UDL除以2后=2
RecvFromMS[72]、[73]是UDL:30 34
*/
	if(TP_UDL == 0 || TP_UDL == 1)	
	{//短信采集数据:当前电量:DL
	  if(TP_UDL == 1)
	  {
		temp1 = RecvFromMS[DataAddr+2];	//+2:跳过30 30
		temp2 = RecvFromMS[DataAddr+3];
	  }
	  else
	  {
			temp1 = RecvFromMS[DataAddr];	//GSM模式没30
		temp2 = RecvFromMS[DataAddr+1];
	  }
	 	if(temp1 < '0' || temp1 > '9')	//发过来的是字母:41H~5AH,所以高位是数字
		{
			ErrorMSG();					//删除该短信
	  		return;
		}
		ASC2B();
		temp3 = (temp1-0x30)<<4 ;
		SMS_CMD[0] = temp3 + temp2;		//D
//命令解析
// ---------------------------------------------------
		if(SMS_CMD[0] == 'R' && SMS_CMD[1] == 'r')	//module复位命令
		{
			ResetModule();
  			for(k = 70;k>0;k--) 	//延时14秒
  			{
	  			delay200ms ();
  			}
			ClearCommREG();
////重新初始化,因Reset 之后,CGATT、CIPHEAD等设置被改变
			Module_Init();
//清零标志字节
			Flags.fCOMM = 0;
			FrameFlag.FrameBYTE = 0;
			ModuleSta.ModuleBYTE = 0;
			TimeFlag.fTime = 0;
			TempFlag.tTemp = 0;
		}
		else if(SMS_CMD[0] == 'D' || SMS_CMD[0] == 'd')	//D:当前电量采集
		{
			Load645Header();
			BUF_METER[13] = 0x1F + 0x33;	//DI0
			BUF_METER[14] = 0x90 + 0x33;	//DI1
			
			CHECKSUM();
			TempFlag.Bit.SMS_GetData = 1;
			SendCMDtoMeter(16);
		}//END of "D"
		else if(SMS_CMD[0] == 'F' || SMS_CMD[0] == 'f')	//F:反向累计电量
		{
		}
		else if(SMS_CMD[0] == 'S' || SMS_CMD[0] == 's')	//S:上月电量
		{
		}
		else if(SMS_CMD[0] == 'T' || SMS_CMD[0] == 't')	//T:时间
		{
		}
	}
	
// ******************************************************************************
	else if(TP_UDL == 3)
	{//短信采集数据:当前电量:D79 old WEISHENG multimeter
		temp1 = RecvFromMS[DataAddr+2];	//+2:跳过30 30
		temp2 = RecvFromMS[DataAddr+3];

	 	if(temp1 < '0' || temp1 > '9')	//发过来的是字母:41H~5AH,所以高位是数字
		{
			ErrorMSG();					//删除该短信
	  		return;
		}
		ASC2B();
		temp3 = (temp1-0x30)<<4 ;
		SMS_CMD[0] = temp3 + temp2;		//D
//命令解析
// ---------------------------------------------------
		if(SMS_CMD[0] == 'R' && SMS_CMD[1] == 'r')	//module复位命令
		{
			ResetModule();
  			for(k = 70;k>0;k--) 	//延时14秒
  			{
	  			delay200ms ();
  			}
			ClearCommREG();
////重新初始化,因Reset 之后,CGATT、CIPHEAD等设置被改变
			Module_Init();
//清零标志字节
			Flags.fCOMM = 0;
			FrameFlag.FrameBYTE = 0;
			ModuleSta.ModuleBYTE = 0;
			TimeFlag.fTime = 0;
			TempFlag.tTemp = 0;
		}
		else if(SMS_CMD[0] == 'D' || SMS_CMD[0] == 'd')	//D:当前电量采集
		{
			temp1 = RecvFromMS[DataAddr+6];		//+2:跳过30 30
			temp2 = RecvFromMS[DataAddr+7];
			k = temp1-0x30;
			temp3 = k << 4;
			DepNOHigh = temp3 + (temp2-0x30);
			temp1 = RecvFromMS[DataAddr+10];	//+2:跳过30 30
			temp2 = RecvFromMS[DataAddr+11];
			k = temp1-0x30;
			temp3 = k << 4;
			DepNOLow = temp3 + (temp2-0x30);

			temp3 = (DepNOHigh-0x30)<<4;
			BUF_METER[0] = temp3 +(DepNOLow-0x30);
			BUF_METER[1] = 0xC7;
			BUF_METER[2] = 0x9A;
			BUF_METER[3] = 0x40;		//  总电量数据位
			BUF_METER[4] = 0x00;
			BUF_METER[5] = 0x00;

			BUF_METER[6] = BUF_METER[0]^BUF_METER[1]^BUF_METER[2]^BUF_METER[3]^BUF_METER[4]^BUF_METER[5];
			for(k=0;k<6;k++)
			{
				cSum = cSum + BUF_METER[k];
			}
			BUF_METER[7] = (unsigned char)cSum;
			BUF_METER[8] = _CR;
			TempFlag.Bit.SMS_GetData = 1;
			TempFlag.Bit.isWEISHENG = 1;
			SendCMDtoMeter(9);
		}//END of "D"
		else if(SMS_CMD[0] == 'F' || SMS_CMD[0] == 'f')	//F:反向累计电量
		{
		}
		else if(SMS_CMD[0] == 'S' || SMS_CMD[0] == 's')	//S:上月电量
		{
		}
		else if(SMS_CMD[0] == 'T' || SMS_CMD[0] == 't')	//T:时间
		{
		}
	}

//非短信采集
	else if(TP_UDL > 10)
//	else if(TP_UDL >13)	
	{//将DCS编码的IP地址解析出来
		LengthBUF2 = TP_UDL+3; 		//左引号,右引号,逗号
		
		
		LengthIP = 0;			//IP地址长度初始化为0
		DotCounter = 0;
  			
		for(k=0;k<TP_UDL;k++)
		{
	  		temp1 = RecvFromMS[DataAddr+k*4+2];
	  		temp2 = RecvFromMS[DataAddr+k*4+3];
	  		if(temp1 == '2' && temp2 == 'E')
		  		{
					temp3 = '.' ;
					DotCounter++;
				}
	  		else if(temp1 >= '0' && temp1 <= '9' && temp2 >= '0' && temp2 <= '9')
				temp3 = (temp1-0x30)*16 + (temp2-0x30);
	  		else if(DotCounter == 3)
	  			{break;}
	  		else
	  		{
	  			ErrorMSG();	
				return;
	  		}
  			  
	  		Ip_start_buf2[k] = temp3;
  	  		LengthIP++;
	  		if(LengthIP == 15)
				break;
		}//END for
		Ip_start_buf2[k] = 0x00;	//存入
  		//写入eeprom存储
		W_eeprom(Ip_start_buf2,IPAddress,LengthIP+1);
  			
///////////////////////////////////////////////////////////
 		IPAddressSeted = 0x33CC;
//读出看看。。。

///////////////////////////////////////////////////////////
		ErrorMSG();							//删除该短信

		LoadCMD_CIPSTATUS();
		WaitReturn = 0;
 		SendToModule (CIPSTATUS_SIZE);
  		for(ii=60;ii>0;ii--) 	//延时3秒
  		{
  			if(FrameFlag.Bit.EchoModule == 1)
				break;
    		delay50ms ();

⌨️ 快捷键说明

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