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;
}
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;
}
相关文章推荐
- jQuery的实现原理的模拟代码 -1 核心部分
- 实现聊天室的思路 和部分代码
- 在linux系统下实现音视频即时通讯的部分代码
- HTTP代理实现请求报文的拦截与篡改10--大结局 篡改部分的代码分析
- JS中实现隐藏部分姓名或者电话号码的代码
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第七部分:僵尸类<后续会提供源码下载链接>
- jQuery的实现原理的模拟代码 -2 数据部分
- Java二维码登录流程实现(包含短地址生成,含部分代码)
- 《算法竞赛入门经典》中的部分代码实现
- 【转】用winpcap实现局域网DNS欺骗之三(代码部分及深入研究)
- svg 图表应用测试(部分代码实现, 备忘)
- Android自定义布局实现仿qq侧滑部分代码
- js 实现打印网页中定义的部分内容的代码
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第二部分:菜单场景<后续会提供源码下载链接>
- 51实现gsm的发短信收短信的程序
- 公用代码实现两个表的拼接(部分代码)
- 优先级队列(代码实现+部分练习)
- 算法导论 6章堆排序的代码实现和部分课后练习
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第八部分:子弹类<后续会提供源码下载链接>
- Android截图代码实现(DDMS使用部分)