📄 hsm.c
字号:
return 0;
}
msDelay(500);
do
{
len = serCrdUsed();
msDelay(5);
}while(len != serCrdUsed());//notice len>250 condition
if(len > 249)
return 0;
serCread(temp,len,200);
/****************************确认短消息************************/
/* do
{
serCrdFlush();
msDelay(100);
}while(serCrdUsed() > 0);
ModemSend("at+cnma\r");
#ifdef DTU_DEBUG
ser485puts("\r\nat+cnma\r");
#endif
do //目的号码
{
if((SEC_TIMER - t) > 2) //time out
{
#ifdef DTU_DEBUG
ser485puts("\r\nat+cnma failed!\r");
#endif
break;
}
}while(!ModemExpect("OK", 2000));
#ifdef DTU_DEBUG
ser485puts("\r\nat+cnma succeed!\r");
#endif
/*********************************************************************/
temp[len] = 0;
if(temp[len - 3] == 'K')//除去数据中的 “OK”,
temp[len - 8] = 0;
strcpy(buf,temp);
return 1;
}
/********************************************************
/* 程序功能: 初始化短消息设置(TEXT_MODE)
/* 入口参数: 无
/* 返回参数: 无
/* 作者: 周志刚
/* 时间: 2003/11/19
********************************************************/
int InitSMS(void)
{//1
auto long t;
auto char ch;
auto int status;
auto unsigned char i;
ModemSetDTR(1);
if(shuttc35 > 3)//关闭模块电源
{
#ifdef DTU_DEBUG
serAputs("\r\nshut up mc35,please wait 15 seconds\r\n");
#endif
BitWrPortI(PEDR, &PEDRShadow, 1, 5);
OSTimeDlyHMSM(0,0,15,0);
BitWrPortI(PEDR, &PEDRShadow, 0, 5);
shuttc35 = 0;
}
for(i = 0; i < 3;i++)
{//2
ModemSend("at^ssync=1\r");
status = ModemExpect("OK", 2000); //等待ok返回
OSTimeDlyHMSM(0,0,0,150);//delay 100ms
if(status)
{//3
#ifdef DTU_DEBUG
serAputs("\r\nAT OK\r\n");
#endif
/******************text格式****************************/
ModemSend("AT+CMGF=1\r");
if(ModemExpect("OK", 4000) == 0)
continue;
OSTimeDlyHMSM(0,0,0,150);//delay 100ms
#ifdef DTU_DEBUG
serAputs("\r\n text mode set OK\r\n");
#endif
/******************gsm编码****************************/
ModemSend("AT+cscs=gsm\r");
if(ModemExpect("OK", 4000) == 0)
continue;
/* #ifdef DTU_DEBUG
serAputs("\r\n gsm OK\r\n");
#endif*/
OSTimeDlyHMSM(0,0,0,150);//delay 100ms
/******************text mode setup 1 ****************************/
ModemSend("AT+csmp=17,167,0,241\r");
if(ModemExpect("OK", 4000) == 0)
continue;
/* #ifdef DTU_DEBUG
serAputs("\r\n text setup AT+csmp=17,167,0,241 OK\r\n");
#endif*/
OSTimeDlyHMSM(0,0,0,150);//delay 100ms
/******************select message service phase2+****************************/
ModemSend("AT+csms=1\r"); //phase 2+
if(!ModemExpect("OK", 4000))
continue;
OSTimeDlyHMSM(0,0,0,150);//delay 100ms
#ifdef DTU_DEBUG
serAputs("\r\n service phase2+ OK!\r\n");
#endif
/******************setup send report****************************/
ModemSend("AT+cnmi=2,1,0,1,1\r");
if(!ModemExpect("OK", 4000))
continue;
else
return 1;
#ifdef DTU_DEBUG
serAputs("\r\n sms send reports setup OK!\r\n");
#endif
}//3
}//2
shuttc35++;
return 0;
}//1
/**********************************************************
程序功能: 短消息处理,分离出:
1、对方号码:
2、发送时间:
3、SM数据
入口参数:待处理数据缓冲
返回值: 1 :success
0 : fail
作者: 周志刚
时间: 2003/11/20
**********************************************************/
unsigned char Do_SMS(char *buf)
{
auto unsigned char i,j,flag,len;
auto char temp[30],input[30];
auto char data_buf[200];
for(i = 0;i < 30;i++)
{
input[i] = 0;
}
for(i = 0;i < 200;i++)
{
data_buf[i] = 0;
}
/***********************start to receive "REC UNREAD"*********************/
i = 0;
while(buf[i] != '"')//the first \",expect the start of "REC UNREAD"
{
i++;
if((buf[i] == 0)||(i > 250))//error
return 0;
}
flag = 0;
i++;
while(buf[i] != '"')//end of "REC UNREAD"
{
temp[flag++] = buf[i++];
if(flag > 20)
return 0;
}
temp[flag] = 0;
if(strcmp(temp,"REC UNREAD") != 0)//error
{
#ifdef DTU_DEBUG
serAputs("\r\ndo not expect REC UNREAD:");
serAputs(temp);
#endif
return 0;
}
/***********************start to receive "+8613632806944"*********************/
i++;
while(buf[i] != '"')//expect start of "+8613632806944"
{
i++;
if((buf[i] == 0)||(i > 250))//error
return 0;
}
/* while(buf[i] != '6')//expect start of "13632806944"
{
i++;
if((buf[i] == 0)||(i > 250))//error
return 0;
} */
flag = 0;
i++;
while(buf[i] != '"')//end of "+8613632806944"
{
temp[flag++] = buf[i++];
if(flag > 20)
return 0;
}
temp[flag] = 0;
#ifdef DTU_DEBUG
serAputs("\r\nthe phome number received is:");
serAputs(temp);
#endif
/* readUserBlock(input,Phone_Number,sizeof(input));
for(j = 0 ;j < 11;j++)
{
if(input[j] != temp[j])
{
#ifdef DTU_DEBUG
serAputs("\r\n错误的发送手机号码.\r\n");
#endif
return 0;
}
}
/***********************start to receive "03/11/20,16:57:13+00"*********************/
i++;
while(buf[i] != '"')//expect start of "03/11/20,16:57:13+00"
{
i++;
if((buf[i] == 0)||(i > 250))//error
return 0;
}
flag = 0;
i++;
while(buf[i] != '"')//end of "03/11/20,16:57:13+00"
{
temp[flag++] = buf[i++];
if(flag > 28)
return 0;
}
temp[flag] = 0;
#ifdef DTU_DEBUG
serAputs("\r\nsms send time is:");
serAputs(temp);
#endif
/***********************start to receive sms data*********************/
flag = 0;
i = i + 3;
while(buf[i] != 0)
{
data_buf[flag++] = buf[i++];
if(i > 250)
return 0;
}
data_buf[flag] = 0;
#ifdef DTU_DEBUG
serAputs("\r\nthe data is:");
serAputs(data_buf);
serAputs("\r\n");
i = 0;
/*while(data_buf[i] != 0)
{
serAputs("data_buf[n]:");
serAputc(data_buf[i]);
serAputs("\r\n");
i++;
} */
//ser485puts("/*over*/");
#endif
len = CharToHex(data_buf);
if(len == 0)
return 0;
TxEn();
TxEn();
ser485write(data_buf,len);//send hex to 485
while(ser485wrFree() != BUFSIZE);
msDelay(20);
RxEn();
RxEn();
return 1;
}
/*****************************************************************
功能: 实现ASCII到hex的转换
入口参数: 处理数据
返回参数: hex长度
0 error
作者: 周志刚
时间: 2003/11/21
*****************************************************************/
unsigned char CharToHex(char *data_buf)
{
auto unsigned char i,ch,j;
// char return_buf[150];
for(i = 0;i < 140;i++)
{
ch = data_buf[i];
if(ch == 0)
{
#ifdef DTU_DEBUG
serAputs("\r\nchar change to hex is over!\r\n");
#endif
break;
}
if((ch < 0x3a)&&(ch > 0x2f))//0-9
data_buf[i] =ch - 0x30;
else if((ch > 0x40)&&(ch < 0x47))//A-F
data_buf[i] = ch - 55;
else if((ch > 0x60)&&(ch < 0x67))//a-f
data_buf[i] = ch - 87;
else
{
#ifdef DTU_DEBUG
serAputs("\r\nexpect error char:");
serAputc(ch);
#endif
return 0;
}
if(((i+1)%2) == 0)
{
j = (i+1)/2 - 1;
data_buf[j] = ((data_buf[i-1]<<4) & 0xf0) | (data_buf[i] & 0x0f);
}
}
if((i%2) != 0)
{
#ifdef DTU_DEBUG
serAputs("\r\nthe number of char is err %2 != 0\r\n");
#endif
return 0;
}
else
return i/2;
}
/*************************************************************************************
函数功能: 实现HEX到char的转换
eg: 0x7f----> '7','F'
入口参数: 1. 转换数据缓存
2. 数据长度
返回参数: 无
作者: 周志刚
时间: 2003/11/21
********************************************************************************/
void HexToChar(char *buffer,unsigned char len)
{
auto unsigned char temp[160];
auto unsigned char i,j,ch;
for(i = 0;i < 160;i++)
{
temp[i] = 0;
}
j = 0;
for(i = 0;i < len; i++)
{
ch = (buffer[i] >> 4) & 0x0f;
if(ch < 10)
ch = ch + 0x30;
else
ch = ch + 55;
temp[j++] = ch;
ch = buffer[i] & 0x0f;
if(ch < 10)
ch = ch + 0x30;
else
ch = ch + 55;
temp[j++] = ch;
}
for(i = 0;i < j; i++)
{
buffer[i] = temp[i];
}
buffer[i] = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -