📄 main.c
字号:
ByteRecvUART2 = 0;
if(ModulFlags.Bit.EchoModule == 1)
ModulFlags.Bit.EchoModule = 0;
}
}
else
{//收到的是?
ModulFlags.Bit.EchoModule = 0 ;
// ClearGPRSREG();
}
}//END of if(ModulFlags.Bit.EchoModule)
//********************************************************************
//改成:cRet = strncmppgm2ram()
if(GPRSComm.Bit.CMD_Frame == 0x01)
{
PassSeconds(1);
for(j=0;j<Length;j++)
{
BUF_METER[j] = RecvFromMS[IPDHead + j]; //将主站的接收缓冲区的数据
} //转送到电表的通讯缓冲区
//
for(tt=0;tt<4;tt++) //找到第一个68H,定位帧头
{
if(BUF_METER[tt] == 0x68)
break;
}
if(BUF_METER[tt] != 0x68)
{//威胜规约,直接转给电表
GreenLed = LIGHT;
SendCMDtoMeter(j);
ClearGPRSREG();
}
else if(BUF_METER[tt+8]==0x09 && BUF_METER[tt+0x0A]==0x01 ) //BUF_METER[0x0A] = DI0
{//主站专门发给DTU的命令处理
GPRSComm.Bit.CMD_Frame = 0;
CMDtoDTU();
ClearGPRSREG();
}
else
{//国标规约,转给电表
GreenLed = LIGHT;
SendCMDtoMeter(j);
ClearGPRSREG();
}
}
//********************************************************************
if(MeterComm.Bit.aFrame == 0x01)
{
if(BUF_METER[8] == 0x09 && BUF_METER[0x0A] == 0x00) //掌机命令
{ //BUF_METER[0x0A] = DI0
HanderCMD();
ClearMeterREG(); //清除电表标志
}
else
{
LoadCMD_CIPSEND();
WaitReturn = 0;
dLength = ByteReceived;
ByteReceived = 0;
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);
}
delay20ms ();
for(k=0;k<dLength;k++)
{
TranToMS[k] = BUF_METER[k];
}
WaitReturn = 0;
SendToModule (dLength); //把从电表那收到的回送上传给主站
DelaySeconds(10);
GreenLed = CLOSE; //发送完关绿灯
ClearGPRSREG();
ClearMeterREG(); //清除电表标志
MinCounter = 0;
}//END of else
}//END if(MeterComm.Bit.aFrame == 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;
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);
DelaySeconds(4);
if(ModulFlags.Bit.EchoModule == 1)
{
ClearGPRSREG();
}
else
{
ErrorMSG();
return;
}
ErrorMSG(); //删除该短信
//加上超时判断,除法改为移位
//取出 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
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
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
IndexOfTP_DCS = IndexOfTP_UDL - 16;
//解析出TP_DCS:=00 text mode;=08:PDU mode
temp1 = RecvFromMS[IndexOfTP_DCS]-0x30;
temp2 = RecvFromMS[IndexOfTP_DCS + 1]-0x30;
TP_DCS = temp1 << 4;
TP_DCS = TP_DCS + temp2;
//解析出TP_UDL
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
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
return;
TP_UDL = temp1 << 4; // (*16+temp2)/2;
TP_UDL = TP_UDL + temp2;
if(TP_DCS == 0x08) //PDU mode
{
TP_UDL = TP_UDL >> 1;
}
DataAddr = IndexOfTP_UDL + 2;
//15个字节长的IP地址+port=2020(9个字节长)
/*收到 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 == 0x01 || TP_UDL == 0x0D)
if(TP_UDL == 0x01)
{//短信采集数据:当前电量:DL
if(TP_UDL == 0x0D)
{
DataAddr = 0x64;
}
if(TP_DCS == 0x08)
{
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,所以高位是数字
return;
ASC2B();
temp3 = (temp1-0x30)<<4 ;
SMS_CMD = temp3 + temp2; //D
if(isalpha(SMS_CMD)) //如果不是字母
{
Nop();
}
else
{
Nop();
return;
}
//命令解析
// ---------------------------------------------------
if(SMS_CMD == 'C' || SMS_CMD == 'c') //C:主站请求连接 {
OK_Data();
Close_Shut();
PassSeconds(10);
OtherFlags.Bit.SysCancel = 0;
ConnectDNS();
SendSMS();
}
else if(SMS_CMD == 'D' || SMS_CMD == 'd') //D:当前有功电量采集
{
Load645Header();
BUF_METER[13] = 0x1F + 0x33; //DI0
BUF_METER[14] = 0x90 + 0x33; //DI1,0x90+0x33=0xC3
EnergyOfMonth = 0;
CHECKSUM();
SendCMDtoMeter(17);
for(ii = 80;ii > 0;ii--) //延时4秒
{
if(MeterComm.Bit.aFrame == 0x01)
break;
delay50ms ();
}
if(MeterComm.Bit.aFrame == 0x00)
return;
MeterComm.Bit.aFrame = 0x00;
SMS_Energy();
}
else if(SMS_CMD == 'J' || SMS_CMD == 'j') //J:用短信检测信道
{
OK_Data();
//发送短信
SendSMS();
}
else if(SMS_CMD == 'Q' || SMS_CMD == 'q') //Q:前月电量
{
Load645Header();
BUF_METER[13] = 0x1F + 0x33; //DI0
BUF_METER[14] = 0x98 + 0x33; //DI1,0x94+0x33=0xCB
EnergyOfMonth = 2;
CHECKSUM();
SendCMDtoMeter(17);
for(ii = 80;ii > 0;ii--) //延时4秒
{
if(MeterComm.Bit.aFrame == 0x01)
break;
delay50ms ();
}
if(MeterComm.Bit.aFrame == 0x00)
return;
MeterComm.Bit.aFrame = 0x00;
SMS_Energy();
}
else if(SMS_CMD == 'R' || SMS_CMD == 'r') //R:module复位命令
{
OtherFlags.Bit.TCPLink = 0;
OK_Data();
Close_Shut();
ReInitial();
SendSMS(); //发送短信
//
}
else if(SMS_CMD == 'S' || SMS_CMD == 's') //S:上月电量
{
Load645Header();
BUF_METER[13] = 0x1F + 0x33; //DI0
BUF_METER[14] = 0x94 + 0x33; //DI1,0x94+0x33=0xC7
EnergyOfMonth = 1;
CHECKSUM();
SendCMDtoMeter(17);
for(ii = 80;ii > 0;ii--) //延时4秒
{
if(MeterComm.Bit.aFrame == 0x01)
break;
delay50ms ();
}
if(MeterComm.Bit.aFrame == 0x00)
return;
MeterComm.Bit.aFrame = 0x00;
SMS_Energy();
}
else if(SMS_CMD == 'T' || SMS_CMD == 't') //T:读module时间
{
LoadCMD_CCLK();
WaitReturn = 2;
SendToModule (CCLK_SIZE);
DelaySeconds(2);
//
if(ModulFlags.Bit.EchoModule == 1)
{
ModulFlags.Bit.EchoModule = 0 ;
//RecvFromMS[0x0A + j]:05/05/23,14:49:00
NOAddr = SMS_Start + 0x18;
for(tt=0;tt<Address_Length;tt++)
{
//对方号码
SMS_Return[tt] = RecvFromMS[NOAddr+tt];
}
for(tt=0;tt<SMS_Header2_SIZE;tt++)
{
SMS_Return[Address_Length+tt] = SMS_Header2[tt];
}
UDLAddr = Address_Length+SMS_Header2_SIZE;
// ----- 格式转换
EnergyDataAddr = UDLAddr+2;
TimeDataToUCS2();
UDLLength = 34; //17*2
//结束符
AllLength = EnergyDataAddr + UDLLength * 2;
SMS_Return[AllLength] = _CTRLZ;
SendSMS();
}
WaitReturn = 0;
}
else if(SMS_CMD == 'W' || SMS_CMD == 'w') //W:当前无功电量采集
{
Load645Header();
BUF_METER[13] = 0x1F + 0x33; //DI0
BUF_METER[14] = 0x91 + 0x33; //DI1,0x90+0x33=0xC3
EnergyOfMonth = 3;
CHECKSUM();
SendCMDtoMeter(17);
for(ii = 80;ii > 0;ii--) //延时4秒
{
if(MeterComm.Bit.aFrame == 0x01)
break;
delay50ms ();
}
if(MeterComm.Bit.aFrame == 0x00)
return;
MeterComm.Bit.aFrame = 0x00;
SMS_Energy();
}
}//END of if(TP_UDL == 1)
// ******************************************************************************
else if(TP_UDL == 0x03)
{//短信采集数据:当前电量:D79 old WEISHENG multimeter
if(TP_DCS == 0x08) //UCS2 mode
{
temp1 = RecvFromMS[DataAddr+2]; //+2:跳过30 30
temp2 = RecvFromMS[DataAddr+3];
if(temp1 < '0' || temp1 > '9') //发过来的是字母:41H~5AH,所以高位是数字
return;
ASC2B();
temp3 = (temp1-0x30)<<4 ;
SMS_CMD = temp3 + temp2; //D
if(isalpha(SMS_CMD) == 0) //如果不是字母
return;
}
else //GSM模式
{
temp2 = RecvFromMS[DataAddr]; //GSM模式没30
ASC2B();
temp1 = temp2;
temp2 = RecvFromMS[DataAddr+1];
ASC2B();
temp3 = temp1 << 4 ;
Text1 = temp3 + temp2; //D
temp2 = RecvFromMS[DataAddr+2]; //GSM模式没30
ASC2B();
temp1 = temp2;
temp2 = RecvFromMS[DataAddr+3];
ASC2B();
temp3 = temp1 << 4 ;
Text2 = temp3 + temp2; //D
temp2 = RecvFromMS[DataAddr+4]; //GSM模式没30
ASC2B();
temp1 = temp2;
temp2 = RecvFromMS[DataAddr+5];
ASC2B();
temp3 = temp1 << 4 ;
Text3 = temp3 + temp2; //D
SMSText();
SMS_CMD = Text1;
if(isalpha(SMS_CMD) == 0) //如果不是字母
return;
}
if(TP_DCS == 0x08)
{
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);
}
else
{
DepNOHigh = Text2;
DepNOLow = Text3;
}
temp3 = (DepNOHigh-0x30)<<4;
BUF_METER[0] = temp3 +(DepNOLow-0x30);
BUF_METER[1] = 0xC7;
BUF_METER[2] = 0x9A;
//命令解析
// ---------------------------------------------------
if(SMS_CMD == 'D' || SMS_CMD == 'd') //D:当前电量采集
{
BUF_METER[3] = 0x40; //总电量数据位
OtherFlags.Bit.WS_NowLastE = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -