gsm发短信代码实现部分
来源:互联网 发布:游戏背包源码 编辑:程序博客网 时间:2024/05/03 06:46
-
- 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;
- }
-
-
- strcpy(tBuf,"");
- strcpy(tBuf,"AT/r/n");
- tLen = strlen(tBuf);
- tRet = SendATMessage(tBuf,tLen);
- if(tRet != TRUE)
- {
- MessageBox("AT失败!");
- return;
- }
-
-
- strcpy(tBuf,"");
- strcpy(tBuf,"AT+CPBS=/"ME/"");
- tLen = strlen(tBuf);
- tRet = SendATMessage(tBuf,tLen);
- if(tRet != TRUE)
- {
- MessageBox("AT+CPBS=/"ME/"失败!");
- return;
- }
-
-
- strcpy(tBuf,"");
- strcpy(tBuf,"AT+CPBR=88");
- tLen = strlen(tBuf);
- tRet = SendATMessage(tBuf,tLen);
- if(tRet != TRUE)
- {
- MessageBox("AT+CPBR=88失败!");
- return;
- }
-
-
- 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;
- }
-
-
- 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);
-
- 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,"");
-
-
- 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("呼叫成功");
-
- }
-
-
-
-
-
-
-
- int CSendMsgDlg::gsmEncodeUcs2(char* pSrc, unsigned char* pDst, int nSrcLength, int* nDstLength)
- {
- int tDstLength;
- WCHAR wchar[128];
-
-
- 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);
-
-
- 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;
-
-
- 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;
-
-
-
-
- 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++)
- {
-
- 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;
- }
-
-
-
- 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;
- }