gsm发短信代码实现部分

来源:互联网 发布:游戏背包源码 编辑:程序博客网 时间:2024/05/03 06:46
  1. //-----------------------------------------------------   
  2. void CSendMsgDlg::OnBtnOpen()    
  3. {      
  4.     int     tRet;   
  5.     char    tBuf[1024] = {0};   
  6.     char    tChars[1024]  = {0};   
  7.     int     tLen = 0;   
  8.    
  9.     m_nPortNo = GetDlgItemInt(IDC_COMBO1);   
  10.        
  11.     memset(m_chCentNo,0,20);   
  12.     memset(m_chPhoneNo,0,20);   
  13.        
  14.     GetDlgItem(IDC_EDIT_CENTER)->GetWindowText(m_chCentNo,20);   
  15.    
  16.     CloseCom_Egl(m_nPortNo);   
  17.     tRet = OpenCom_Egl(m_nPortNo,9600,0,8,0);   
  18.     if(tRet != TRUE)   
  19.     {   
  20.         MessageBox("连接仪器失败!");   
  21.         return;   
  22.     }   
  23.    
  24.     //-----AT Test-----    
  25.     strcpy(tBuf,"");   
  26.     strcpy(tBuf,"AT/r/n");   
  27.     tLen = strlen(tBuf);   
  28.     tRet = SendATMessage(tBuf,tLen);   
  29.     if(tRet != TRUE)   
  30.     {   
  31.         MessageBox("AT失败!");   
  32.         return;   
  33.     }   
  34.    
  35.     //-----AT+CPBS="ME"-----    
  36.     strcpy(tBuf,"");   
  37.     strcpy(tBuf,"AT+CPBS=/"ME/"");   
  38.     tLen = strlen(tBuf);   
  39.     tRet = SendATMessage(tBuf,tLen);   
  40.     if(tRet != TRUE)   
  41.     {   
  42.         MessageBox("AT+CPBS=/"ME/"失败!");   
  43.         return;   
  44.     }   
  45.    
  46.     //-------AT+CPBR=88-------    
  47.     strcpy(tBuf,"");   
  48.     strcpy(tBuf,"AT+CPBR=88");   
  49.     tLen = strlen(tBuf);   
  50.     tRet = SendATMessage(tBuf,tLen);   
  51.     if(tRet != TRUE)   
  52.     {   
  53.         MessageBox("AT+CPBR=88失败!");   
  54.         return;   
  55.     }   
  56.        
  57.     //设置短消息号码:AT+CSCA=+8613800731500    
  58.     strcpy(tBuf,"");   
  59.     strcpy(tBuf,"AT+CSCA=");   
  60.     sprintf(tBuf,"%s%s",tBuf,m_chCentNo);   
  61.     tLen = strlen(tBuf);   
  62.     tRet = SendATMessage(tBuf,tLen);   
  63.     if(tRet != TRUE)   
  64.     {   
  65.         MessageBox("短消息中心号码设置失败!");   
  66.         return;   
  67.     }   
  68.    
  69.     //设置短消息格式PDU:AT+CMGF=0    
  70.     strcpy(tBuf,"");   
  71.     strcpy(tBuf,"AT+CMGF=0");   
  72.     tLen = strlen(tBuf);   
  73.     tRet = SendATMessage(tBuf,tLen);   
  74.     if(tRet != TRUE)   
  75.     {   
  76.         MessageBox("短消息格式设置失败!");   
  77.         return;   
  78.     }   
  79.    
  80.     MessageBox("连接仪器成功!");   
  81.        
  82. }   
  83.    
  84. //-----------------------------------------------------   
  85. void CSendMsgDlg::OnBtnClose()    
  86. {   
  87.     CloseCom_Egl(m_nPortNo);   
  88. }   
  89.    
  90. //-----------------------------------------------------   
  91. void CSendMsgDlg::OnClose()    
  92. {   
  93.     CloseCom_Egl(m_nPortNo);   
  94.     CDialog::OnClose();   
  95. }   
  96.    
  97. //-----------------------------------------------------   
  98. void CSendMsgDlg::OnBtnSend()    
  99. {   
  100.     u8      tBuf[1024]={0};   
  101.     u8      tMsg[1024] = {0};   
  102.     int     tRet;   
  103.     long    tLen=0,tMsgLen;   
  104.     char    tChars[1024] = {0};   
  105.    
  106.     GetDlgItem(IDC_EDIT_PHONE)->GetWindowText(m_chPhoneNo,20);   
  107.     //发送短消息;AT+CMGS=tLen    
  108.     this->msgConstructor(tMsg,&tMsgLen);   
  109.    
  110.     memset(tChars,0,1024);   
  111.     memset(tBuf,0,1024);   
  112.     strcpy(tChars,"AT+CMGS= ");   
  113.     sprintf(tChars,"%s%d",tChars,tMsgLen/2-1);   
  114.     tLen = strlen(tChars);   
  115.     SendATMessage(tChars,tLen);   
  116.        
  117.     tMsg[tMsgLen]=0x1A;   
  118.     tRet = WriteComBytes_Egl(m_nPortNo,tMsg,tMsgLen+1);   
  119.     if(tRet != TRUE)   
  120.     {   
  121.         MessageBox("写串口失败!");   
  122.         return;   
  123.     }   
  124.    
  125.     tLen = 1024;   
  126.     WaitComInput_Egl(m_nPortNo,&tLen,500);   
  127.     if(tLen=0)   
  128.     {   
  129.         MessageBox("串口返回失败!");   
  130.         return;   
  131.     }   
  132.     memset(tBuf,0,1024);   
  133.     ReadComBytes_Egl(m_nPortNo,tBuf,&tLen,"");   
  134.        
  135.     //ATD号码    
  136.     for(int i=0;i<20;i++)   
  137.     {   
  138.         strcpy(tChars,"");   
  139.         strcpy(tChars,"ATD");   
  140.         sprintf(tChars,"%s%s;/r/n",tChars,m_chPhoneNo);   
  141.         tLen = strlen(tChars);   
  142.         tRet = SendATMessage(tChars,tLen);   
  143.         Sleep(100);   
  144.        
  145.     }   
  146.     MessageBox("呼叫成功");   
  147.    
  148. }   
  149.    
  150. //-----------------------------------------------------   
  151. // UCS2编码    
  152. // pSrc: 源字符串指针    
  153. // pDst: 目标编码串指针    
  154. // nSrcLength: 源字符串长度    
  155. // 返回:目标编码串长度    
  156. int CSendMsgDlg::gsmEncodeUcs2(char* pSrc, unsigned char* pDst, int nSrcLength, int* nDstLength)   
  157. {   
  158.     int        tDstLength; // UNICODE宽字符数目   
  159.     WCHAR      wchar[128]; // UNICODE串缓冲区   
  160.        
  161.     //字符串-->UNICODE串    
  162.     tDstLength = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLength, wchar, 128);   
  163.        
  164.     // 高低字节对调,输出    
  165.     for (int i = 0; i  tDstLength; i++)   
  166.     {   
  167.         // 先输出高位字节    
  168.         *pDst++ = wchar[i] >> 8;   
  169.            
  170.         // 后输出低位字节    
  171.         *pDst++ = wchar[i] & 0xff;   
  172.     }   
  173.        
  174.     // 返回目标编码串长度    
  175.     *nDstLength = tDstLength*2;    
  176.     return TRUE;   
  177. }   
  178.    
  179.    
  180. //-----------------------------------------------------   
  181. int CSendMsgDlg::msgConstructor(unsigned char* tMsgInput, long* tMsgLen)   
  182. {   
  183.     u8      tTextBuf[1024] = {0};   
  184.     char    chText[1024]={0};   
  185.     char    chPhonNo[20];   
  186.     char    ch;   
  187.    
  188.     u8      *tMsgBuf=new u8[1024];   
  189.    
  190.     long    tLen,tTotalLen;   
  191.     int     tTextLen;   
  192.     int     i;   
  193.    
  194.     u8      tDataLen,tData;   
  195.        
  196.     GetDlgItem(IDC_EDIT_TEXT)->GetWindowText(chText,1024);   
  197.     strcpy(chPhonNo,m_chPhoneNo);   
  198.     memset(tMsgInput,0,1024);   
  199.     memset(tMsgBuf,0,1024);   
  200.    
  201.     //发送短消息;AT+CMGS=tLen    
  202.     tMsgBuf[0] = 0x30;   
  203.     tMsgBuf[1] = 0x30;   
  204.     tMsgBuf[2] = 0x31;   
  205.     tMsgBuf[3] = 0x31;   
  206.     tMsgBuf[4] = 0x30;   
  207.     tMsgBuf[5] = 0x30;   
  208.        
  209.     tMsgBuf[6] = 0x30;   
  210.     tMsgBuf[7] = 0x44;   
  211.     tMsgBuf[8] = 0x39;   
  212.     tMsgBuf[9] = 0x31;   
  213.     tMsgBuf[10] = 0x36;   
  214.     tMsgBuf[11] = 0x38;   
  215.        
  216.     tTotalLen = 12;   
  217.        
  218.     tLen = strlen(chPhonNo);   
  219.     if(tLen%2)   
  220.     {   
  221.         chPhonNo[tLen] = 'F';   
  222.         chPhonNo[tLen+1] = '/0';   
  223.     }   
  224.        
  225.     char    temp;   
  226.     tLen = strlen(chPhonNo);   
  227.     for(i=0;i<tLen;i+=2)   
  228.     {   
  229.         temp = chPhonNo[i];   
  230.         chPhonNo[i] = chPhonNo[i+1];   
  231.         chPhonNo[i+1] = temp;   
  232.     }   
  233.        
  234.     memcpy(tMsgBuf+tTotalLen,chPhonNo,tLen);   
  235.     tTotalLen += tLen;   
  236.        
  237.     //  30 30 31 38 41 38 31 38    
  238.     tMsgBuf[tTotalLen]=0x30;   
  239.     tMsgBuf[tTotalLen+1]=0x30;   
  240.     tMsgBuf[tTotalLen+2]=0x31;   
  241.     tMsgBuf[tTotalLen+3]=0x38;   
  242.     tMsgBuf[tTotalLen+4]=0x41;   
  243.     tMsgBuf[tTotalLen+5]=0x38;   
  244.     tTotalLen += 6;   
  245.        
  246.     //tMsg[tTotalLen+6]=0x31;    
  247.     //tMsg[tTotalLen+7]=0x38;    
  248.     //tTotalLen += 8;    
  249.     tLen = strlen(chText);   
  250.     gsmEncodeUcs2(chText,tTextBuf,tLen,&tTextLen);   
  251.     tDataLen = tTextLen+4;   
  252.    
  253.     tData = (tDataLen&0xf0)>>4;   
  254.     itoa(tData,&ch,16);   
  255.     ch = toupper(ch);   
  256.     tMsgBuf[tTotalLen]=ch;   
  257.     tTotalLen += 1;   
  258.     tData = tDataLen&0xF;   
  259.     itoa(tData,&ch,16);   
  260.     ch = toupper(ch);   
  261.     tMsgBuf[tTotalLen]=ch;   
  262.     tTotalLen += 1;   
  263.    
  264.        
  265.     for(i=0;i<tDataLen-4;i++)   
  266.     {   
  267.     //  MessageBox("OK");    
  268.         itoa(tTextBuf[i]>>4,&ch,16);   
  269.         ch = toupper(ch);   
  270.         tMsgBuf[tTotalLen]=ch;   
  271.         tTotalLen += 1;   
  272.         itoa(tTextBuf[i]&0x0F,&ch,16);   
  273.         ch = toupper(ch);   
  274.         tMsgBuf[tTotalLen]=ch;   
  275.         tTotalLen += 1;   
  276.     }   
  277.    
  278.     //  MessageBox((char *)tMsgBuf);    
  279.    
  280.     tMsgBuf[tTotalLen]=0x30;   
  281.     tMsgBuf[tTotalLen+1]=0x30;   
  282.     tMsgBuf[tTotalLen+2]=0x30;   
  283.     tMsgBuf[tTotalLen+3]=0x44;   
  284.     tMsgBuf[tTotalLen+4]=0x30;   
  285.     tMsgBuf[tTotalLen+5]=0x30;   
  286.     tMsgBuf[tTotalLen+6]=0x30;   
  287.     tMsgBuf[tTotalLen+7]=0x41;   
  288.    
  289.     tTotalLen += 8;   
  290.     *tMsgLen = tTotalLen;   
  291.    
  292.     memcpy(tMsgInput,tMsgBuf,tTotalLen);   
  293.     delete[] tMsgBuf;   
  294.    
  295.     return TRUE;   
  296. }   
  297.    
  298. //-----------------------------------------------------   
  299. int CSendMsgDlg::SendATMessage(char* tMsgInput,long nLen)   
  300. {   
  301.     u8      tBuf[1024]={0};   
  302.     char    tChars[1024]={0};   
  303.     int     tRet;   
  304.     long    tLen;   
  305.        
  306.     memcpy(tBuf,tMsgInput,nLen);   
  307.     tBuf[nLen]=0x0D;   
  308.    
  309.     ClearCom_Egl(m_nPortNo);   
  310.     tRet = WriteComBytes_Egl(m_nPortNo,tBuf,nLen+1);   
  311.     if(tRet != TRUE)   
  312.     {   
  313.         return -10;   
  314.     }   
  315.     tLen = 1024;   
  316.     WaitComInput_Egl(m_nPortNo,&tLen,200);   
  317.     if(tLen=0)   
  318.     {   
  319.         return -20;   
  320.     }   
  321.        
  322.     memset(tBuf,0,1024);   
  323.     ReadComBytes_Egl(m_nPortNo,tBuf,&tLen,"");   
  324.        
  325.     memcpy(tChars,tBuf,tLen);   
  326.        
  327.     if(strstr(tChars,"OK")==NULL)   
  328.     {   
  329.         return -30;   
  330.     }   
  331.        
  332.     return 1;   
  333. }   
原创粉丝点击