您的位置:首页 > 编程语言

gsm发短信代码实现部分

2011-04-26 18:54 330 查看
//-----------------------------------------------------

void CSendMsgDlg::OnBtnOpen()
{
int tRet;
char tBuf[1024] = {0};
char tChars[1024] = {0};
int tLen = 0;

m_nPortNo = GetDlgItemInt(IDC_COMBO1);

memset(m_chCentNo,0,20);
memset(m_chPhoneNo,0,20);

GetDlgItem(IDC_EDIT_CENTER)->GetWindowText(m_chCentNo,20);

CloseCom_Egl(m_nPortNo);
tRet = OpenCom_Egl(m_nPortNo,9600,0,8,0);
if(tRet != TRUE)
{
MessageBox("连接仪器失败!");
return;
}

//-----AT Test-----
strcpy(tBuf,"");
strcpy(tBuf,"AT/r/n");
tLen = strlen(tBuf);
tRet = SendATMessage(tBuf,tLen);
if(tRet != TRUE)
{
MessageBox("AT失败!");
return;
}

//-----AT+CPBS="ME"-----
strcpy(tBuf,"");
strcpy(tBuf,"AT+CPBS=/"ME/"");
tLen = strlen(tBuf);
tRet = SendATMessage(tBuf,tLen);
if(tRet != TRUE)
{
MessageBox("AT+CPBS=/"ME/"失败!");
return;
}

//-------AT+CPBR=88-------
strcpy(tBuf,"");
strcpy(tBuf,"AT+CPBR=88");
tLen = strlen(tBuf);
tRet = SendATMessage(tBuf,tLen);
if(tRet != TRUE)
{
MessageBox("AT+CPBR=88失败!");
return;
}

//设置短消息号码:AT+CSCA=+8613800731500

strcpy(tBuf,"");
strcpy(tBuf,"AT+CSCA=");
sprintf(tBuf,"%s%s",tBuf,m_chCentNo);
tLen = strlen(tBuf);
tRet = SendATMessage(tBuf,tLen);
if(tRet != TRUE)
{
MessageBox("短消息中心号码设置失败!");
return;
}

//设置短消息格式PDU:AT+CMGF=0
strcpy(tBuf,"");
strcpy(tBuf,"AT+CMGF=0");
tLen = strlen(tBuf);
tRet = SendATMessage(tBuf,tLen);
if(tRet != TRUE)
{
MessageBox("短消息格式设置失败!");
return;
}

MessageBox("连接仪器成功!");

}

//-----------------------------------------------------

void CSendMsgDlg::OnBtnClose()
{
CloseCom_Egl(m_nPortNo);
}

//-----------------------------------------------------

void CSendMsgDlg::OnClose()
{
CloseCom_Egl(m_nPortNo);
CDialog::OnClose();
}

//-----------------------------------------------------

void CSendMsgDlg::OnBtnSend()
{
u8 tBuf[1024]={0};
u8 tMsg[1024] = {0};
int tRet;
long tLen=0,tMsgLen;
char tChars[1024] = {0};

GetDlgItem(IDC_EDIT_PHONE)->GetWindowText(m_chPhoneNo,20);
//发送短消息;AT+CMGS=tLen
this->msgConstructor(tMsg,&tMsgLen);

memset(tChars,0,1024);
memset(tBuf,0,1024);
strcpy(tChars,"AT+CMGS= ");
sprintf(tChars,"%s%d",tChars,tMsgLen/2-1);
tLen = strlen(tChars);
SendATMessage(tChars,tLen);

tMsg[tMsgLen]=0x1A;
tRet = WriteComBytes_Egl(m_nPortNo,tMsg,tMsgLen+1);
if(tRet != TRUE)
{
MessageBox("写串口失败!");
return;
}

tLen = 1024;
WaitComInput_Egl(m_nPortNo,&tLen,500);
if(tLen=0)
{
MessageBox("串口返回失败!");
return;
}
memset(tBuf,0,1024);
ReadComBytes_Egl(m_nPortNo,tBuf,&tLen,"");

//ATD号码
for(int i=0;i<20;i++)
{
strcpy(tChars,"");
strcpy(tChars,"ATD");
sprintf(tChars,"%s%s;/r/n",tChars,m_chPhoneNo);
tLen = strlen(tChars);
tRet = SendATMessage(tChars,tLen);
Sleep(100);

}
MessageBox("呼叫成功");

}

//-----------------------------------------------------

// UCS2编码
// pSrc: 源字符串指针
// pDst: 目标编码串指针
// nSrcLength: 源字符串长度
// 返回:目标编码串长度
int CSendMsgDlg::gsmEncodeUcs2(char* pSrc, unsigned char* pDst, int nSrcLength, int* nDstLength)
{
int tDstLength; // UNICODE宽字符数目

WCHAR wchar[128]; // UNICODE串缓冲区


//字符串-->UNICODE串
tDstLength = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLength, wchar, 128);

// 高低字节对调,输出
for (int i = 0; i tDstLength; i++)
{
// 先输出高位字节
*pDst++ = wchar[i] >> 8;

// 后输出低位字节
*pDst++ = wchar[i] & 0xff;
}

// 返回目标编码串长度
*nDstLength = tDstLength*2;
return TRUE;
}


//-----------------------------------------------------

int CSendMsgDlg::msgConstructor(unsigned char* tMsgInput, long* tMsgLen)
{
u8 tTextBuf[1024] = {0};
char chText[1024]={0};
char chPhonNo[20];
char ch;

u8 *tMsgBuf=new u8[1024];

long tLen,tTotalLen;
int tTextLen;
int i;

u8 tDataLen,tData;

GetDlgItem(IDC_EDIT_TEXT)->GetWindowText(chText,1024);
strcpy(chPhonNo,m_chPhoneNo);
memset(tMsgInput,0,1024);
memset(tMsgBuf,0,1024);

//发送短消息;AT+CMGS=tLen
tMsgBuf[0] = 0x30;
tMsgBuf[1] = 0x30;
tMsgBuf[2] = 0x31;
tMsgBuf[3] = 0x31;
tMsgBuf[4] = 0x30;
tMsgBuf[5] = 0x30;

tMsgBuf[6] = 0x30;
tMsgBuf[7] = 0x44;
tMsgBuf[8] = 0x39;
tMsgBuf[9] = 0x31;
tMsgBuf[10] = 0x36;
tMsgBuf[11] = 0x38;

tTotalLen = 12;

tLen = strlen(chPhonNo);
if(tLen%2)
{
chPhonNo[tLen] = 'F';
chPhonNo[tLen+1] = '/0';
}

char temp;
tLen = strlen(chPhonNo);
for(i=0;i<tLen;i+=2)
{
temp = chPhonNo[i];
chPhonNo[i] = chPhonNo[i+1];
chPhonNo[i+1] = temp;
}

memcpy(tMsgBuf+tTotalLen,chPhonNo,tLen);
tTotalLen += tLen;

// 30 30 31 38 41 38 31 38
tMsgBuf[tTotalLen]=0x30;
tMsgBuf[tTotalLen+1]=0x30;
tMsgBuf[tTotalLen+2]=0x31;
tMsgBuf[tTotalLen+3]=0x38;
tMsgBuf[tTotalLen+4]=0x41;
tMsgBuf[tTotalLen+5]=0x38;
tTotalLen += 6;

//tMsg[tTotalLen+6]=0x31;
//tMsg[tTotalLen+7]=0x38;
//tTotalLen += 8;
tLen = strlen(chText);
gsmEncodeUcs2(chText,tTextBuf,tLen,&tTextLen);
tDataLen = tTextLen+4;

tData = (tDataLen&0xf0)>>4;
itoa(tData,&ch,16);
ch = toupper(ch);
tMsgBuf[tTotalLen]=ch;
tTotalLen += 1;
tData = tDataLen&0xF;
itoa(tData,&ch,16);
ch = toupper(ch);
tMsgBuf[tTotalLen]=ch;
tTotalLen += 1;


for(i=0;i<tDataLen-4;i++)
{
// MessageBox("OK");
itoa(tTextBuf[i]>>4,&ch,16);
ch = toupper(ch);
tMsgBuf[tTotalLen]=ch;
tTotalLen += 1;
itoa(tTextBuf[i]&0x0F,&ch,16);
ch = toupper(ch);
tMsgBuf[tTotalLen]=ch;
tTotalLen += 1;
}

// MessageBox((char *)tMsgBuf);


tMsgBuf[tTotalLen]=0x30;
tMsgBuf[tTotalLen+1]=0x30;
tMsgBuf[tTotalLen+2]=0x30;
tMsgBuf[tTotalLen+3]=0x44;
tMsgBuf[tTotalLen+4]=0x30;
tMsgBuf[tTotalLen+5]=0x30;
tMsgBuf[tTotalLen+6]=0x30;
tMsgBuf[tTotalLen+7]=0x41;

tTotalLen += 8;
*tMsgLen = tTotalLen;

memcpy(tMsgInput,tMsgBuf,tTotalLen);
delete[] tMsgBuf;

return TRUE;
}

//-----------------------------------------------------

int CSendMsgDlg::SendATMessage(char* tMsgInput,long nLen)
{
u8 tBuf[1024]={0};
char tChars[1024]={0};
int tRet;
long tLen;

memcpy(tBuf,tMsgInput,nLen);
tBuf[nLen]=0x0D;

ClearCom_Egl(m_nPortNo);
tRet = WriteComBytes_Egl(m_nPortNo,tBuf,nLen+1);
if(tRet != TRUE)
{
return -10;
}
tLen = 1024;
WaitComInput_Egl(m_nPortNo,&tLen,200);
if(tLen=0)
{
return -20;
}

memset(tBuf,0,1024);
ReadComBytes_Egl(m_nPortNo,tBuf,&tLen,"");

memcpy(tChars,tBuf,tLen);

if(strstr(tChars,"OK")==NULL)
{
return -30;
}

return 1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: