📄 smstest.cpp
字号:
nPduLength = GsmEncodePdu(&SmParam, pdu);
strcat(pdu,"\x1A");
GsmString2Bytes(pdu, &nSmscLength, 2); // 取PDU串中的SMSC信息长度
nSmscLength++;
//cmd.Format("AT+CMGS=%d\r" ,(nPduLength / 2 - nSmscLength) );
QDDataService()->ParseSMS_SendTestData( SMSS_AtSend , 0, _T(""));
cmd.Format("at+cmgs=%d\r" ,(nPduLength / 2 - nSmscLength) );
Senddata(portA,(char*)(LPCTSTR)cmd,cmd.GetLength());
Sleep(500);
ans=ReceiveData();
//int h=::GetLastError();
// strcpy(SmParam.SCA, (LPCTSTR)CenterNum);
//ans=recvstr;
if(ans.Find("\r\n>")>-1)
{
// SendWMMessage(WM_SMS_STATUS,SMSStartSend,timeGetTime());
QDDataService()->ParseSMS_SendTestData( SMSS_SendContent , 0, _T(""));
starttime=timeGetTime();
Senddata(portA,pdu,(int)strlen(pdu));
//memset(recvstr,0,4096);
}
else
{
QDDataService()->ParseSMS_SendTestData( SMSS_AtSendFall , 0, _T(""));
delete recvstr;
delete sendstr;
return FALSE;
}
ans.Empty();
memset(recvstr,0,4096);
while(!status)
{
ans=ReceiveData( );
//if(recvlen!=0) ans=ans+recvstr;
//recvlen=0;
//memset(recvstr,0,4096);
if(ans.Find("OK")!=-1)
{
// Memo1->Lines->Add("成功发送");
// SendWMMessage(WM_SMS_STATUS ,SMSSend_Success ,timeGetTime());
QDDataService()->ParseSMS_SendTestData( SMSS_SendSuccess , 0, _T(""));
status=TRUE;
}
if(ans.Find("ERROR")!=-1)
{
// Memo1->Lines->Add("发送失败");
// SendWMMessage(WM_SMS_STATUS ,SMSSend_Fail ,0);
QDDataService()->ParseSMS_SendTestData( SMSS_SendFail , 0, _T(""));
delete recvstr;
delete sendstr;
return FALSE;
}
Sleep(50);
}
delete recvstr;
delete sendstr;
return status;
}
BOOL WINAPI SendSMS(SMS_PARAM *param , BOOL* pStop , UINT DevId , UINT TestCount , UINT TestInterval)
//------------------------SendSMS(¶m , 20 , 2 , 1);
{
#ifdef _DEBUG
DebugFile.Open("debug.txt" , CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite );
DebugFile.SeekToEnd();
CString temp = "\r\n[SMSSendTest] ";
time_t current=time(NULL);
temp = temp + ctime(¤t) + "\r\n";
DebugFile.Write(temp , temp.GetLength());
temp.Format("HWND : %d\r\nSendPort : %d\r\nPhoneNum : %s\r\nContent : %s\r\n",
param->hWnd , param->sendcomport , param->phonenum , param->content );
DebugFile.Write(temp , temp.GetLength());
#endif
g_notifyhWnd = param->hWnd;
g_DevID = DevId;
g_TestCount = TestCount;
g_TestInterval = TestInterval; //现在看来几乎没用的一组变量
g_IsTerminal = pStop;
/*SMS_PARAM *threadparam=new SMS_PARAM;
memset(threadparam,0,sizeof(SMS_PARAM));
memcpy(threadparam,param,sizeof(SMS_PARAM));*/
//----------------------------------------------------------
BOOL status;
CSMSTest csms;
//param->sendcomport=3;
//param->phonenum="13823050369";
//param->content="zzf";
csms.SysTemPath=param->SysPath;
/*QDDataService()->ParseSMS_SendTestData(SMSS_Dllinit, 0, _T(""));
if(!csms.InitialComPort())
{
QDDataService()->ParseSMS_SendTestData(SMSS_Dllinit, 0, _T(""));
return 0;
}*/
QDDataService()->ParseSMS_SendTestData(SMSS_OpenPort , 0, _T(""));
if(!csms.OpenPorte(param->sendcomport , param->portrate))
{
QDDataService()->ParseSMS_SendTestData(SMSS_OpenFall , 0, _T(""));
return 0;
}
//-------------------------------------------------------------
/*SMS_PARAM* param=(SMS_PARAM*) lpPara;*/
// g_TestCount=0;
// QDDataService()->ParseSMS_SendTestData(SMSS_Begin, 0, _T(""));
SMS_PARAM aparam=*param;
for(int i=0;i<(int)g_TestCount;i++)
{
aparam=*param;
if(*g_IsTerminal)break;
// SendWMMessage(WM_TEST_STATUS , Test_Count , i);
QDDataService()->ParseSMS_SendTestData(SMSS_TheNum, 0, _T(""));
//在此处添加发送失败计数,利用 status ;
if(!(*g_IsTerminal))
status = csms.sendsms(aparam.sendcomport , aparam.phonenum, aparam.content, aparam.centernum);
else break;
//if(*g_IsTerminal)
if(!status)continue;
//--------------------------------------------------------------------------------------
if(!(*g_IsTerminal))
status = csms.recvsms(aparam.recvcomport,&aparam);
else break;
//--------------------------------------------------------------------------------------
//if(status) (++g_SuccessCount);
//if(status)
// SendWMMessage(WM_TEST_STATUS , End_TestSuccess , timeGetTime());
//else
//SendWMMessage(WM_TEST_STATUS , End_TestFail , timeGetTime());//
if(i<(int)g_TestCount-1)
Sleep(g_TestInterval);
}
//QDDataService()->ParseSMS_SendTestData( SMSS_End , 0, _T(""));
//-------------------------------------------------------------------
QDDataService()->ParseSMS_SendTestData(SMSS_ClosePort , 0, _T(""));
csms.ClosePorte(param->sendcomport);
//csms.FreeComPort();
if(*g_IsTerminal)// ::ExitThread(2);
{
QDDataService()->ParseSMS_SendTestData(SMSS_EndByUser , 0, _T(""));
return 1;
}
//------------------------------------------------------------------
// SendWMMessage(WM_TEST_STATUS , Start_Test , timeGetTime());
/*DWORD id;
DWORD retcode = 0;
HANDLE _thread_handle;
SMS_PARAM *threadparam=new SMS_PARAM;
memset(threadparam,0,sizeof(SMS_PARAM));
memcpy(threadparam,param,sizeof(SMS_PARAM));
_thread_handle = CreateThread(NULL, 0, SendThread,threadparam, 0, &id); //辅助线程
if( _thread_handle != NULL)
{
WaitForSingleObject(_thread_handle,INFINITE);
GetExitCodeThread(_thread_handle , &retcode);
CloseHandle(_thread_handle);
}*/
// delete threadparam;
/*if( retcode == 2 )
SendWMMessage(WM_TEST_STATUS , End_ByUser , timeGetTime());*/
/*else
SendWMMessage(WM_TEST_STATUS , End_TotalTest , timeGetTime());*/
g_notifyhWnd = NULL;
g_DevID = 0;
g_TestCount = 1;
g_TestInterval = 1000;
g_IsTerminal = NULL;
#ifdef _DEBUG
DebugFile.Close();
#endif
//return retcode ;
return 0 ;
}
BOOL RecvSMS(SMS_PARAM *param , BOOL* pStop, UINT DevId , UINT TestCount , UINT TestInterval)
{
/*#ifdef _DEBUG
DebugFile.Open("debug.txt" , CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite );
DebugFile.SeekToEnd();
CString temp = "\r\n[SMSRecvTest] ";
time_t current=time(NULL);
temp = temp + ctime(¤t) + "\r\n";
DebugFile.Write(temp , temp.GetLength());
temp.Format("HWND : %d\r\nSendPort : %d\r\nRecvPort : %d\r\nPhoneNum : %s\r\nContent : %s\r\nRecvFolder : %s\r\n",
param->hWnd , param->sendcomport , param->recvcomport , param->phonenum , param->content , param->recvfolder);
DebugFile.Write(temp , temp.GetLength());
#endif */
g_notifyhWnd = param->hWnd;
g_DevID = DevId;
g_TestCount = TestCount;
g_TestInterval = TestInterval;
g_IsTerminal = pStop;
// SendWMMessage(WM_TEST_STATUS , Start_Test , timeGetTime());
//SMS_PARAM* param=(SMS_PARAM*) lpPara;
QDDataService()->ParseSMS_ReceiveTestData(SMSR_Begin, 0, _T(""));
for(int i=0;i<(int)g_TestCount;i++)
{
//SendWMMessage(WM_TEST_STATUS , Test_Count , i);
QDDataService()->ParseSMS_ReceiveTestData(SMSR_TheNum , 0, _T(""));
BOOL status;
CSMSTest csms;
csms.m_recvFolder = param->recvfolder;
//QDDataService()->ParseSMS_ReceiveTestData(SMSR_Dllinit , 0, _T(""));
// if(!csms.InitialComPort()) return 0;
QDDataService()->ParseSMS_ReceiveTestData(SMSR_OpenPort , 0, _T(""));
if(!csms.OpenPorte(param->recvcomport , param->portrate))
{
QDDataService()->ParseSMS_ReceiveTestData(SMSR_OpenFall , 0, _T(""));
return 0;
}
status = csms.recvsms(param->recvcomport,param);
QDDataService()->ParseSMS_SendTestData(SMSS_ClosePort , 0, _T(""));
csms.ClosePorte(param->recvcomport);
//csms.FreeComPort();
if(*g_IsTerminal) //::ExitThread(2);
{
QDDataService()->ParseSMS_SendTestData(SMSR_EndByUser , 0, _T(""));
}
/* if(status)
SendWMMessage(WM_TEST_STATUS , End_TestSuccess , timeGetTime());
else
SendWMMessage(WM_TEST_STATUS , End_TestFail , timeGetTime());*/
if(i<(int)g_TestCount-1)
Sleep(g_TestInterval);
}
QDDataService()->ParseSMS_SendTestData( SMSS_End , 0, _T(""));
/*DWORD id;
DWORD retcode = 0;
HANDLE _thread_handle;
/SMS_PARAM *threadparam=new SMS_PARAM;
memset(threadparam,0,sizeof(SMS_PARAM));
memcpy(threadparam,param,sizeof(SMS_PARAM));/
_thread_handle = CreateThread(NULL, 0, RecvThread, param, 0, &id); //辅助线程
if( _thread_handle != NULL)
{
WaitForSingleObject(_thread_handle,INFINITE);
GetExitCodeThread(_thread_handle , &retcode);
CloseHandle(_thread_handle);
}*/
//delete threadparam;
/*if( retcode == 2 )
SendWMMessage(WM_TEST_STATUS , End_ByUser , timeGetTime());
else
SendWMMessage(WM_TEST_STATUS , End_TotalTest , timeGetTime());
g_notifyhWnd = NULL;
g_DevID = 0;
g_TestCount = 1;
g_TestInterval = 1000;
g_IsTerminal = FALSE;*/
/*#ifdef _DEBUG
DebugFile.Close();
#endif */
return 1;
}
int CSMSTest::GsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
for (int i = 0; i < nSrcLength; i += 2)
{
// 输出高4位
if ((*pSrc >= '0') && (*pSrc <= '9'))
{
*pDst = (*pSrc - '0') << 4;
}
else
{
*pDst = (*pSrc - 'A' + 10) << 4;
}
pSrc++;
// 输出低4位
if ((*pSrc>='0') && (*pSrc<='9'))
{
*pDst |= *pSrc - '0';
}
else
{
*pDst |= *pSrc - 'A' + 10;
}
pSrc++;
pDst++;
}
// 返回目标数据长度
return (nSrcLength / 2);
}
// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"
// 输入: pSrc - 源数据指针
// nSrcLength - 源数据长度
// 输出: pDst - 目标字符串指针
// 返回: 目标字符串长度
int CSMSTest::gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
for (int i = 0; i < nSrcLength; i++)
{
*pDst++ = tab[*pSrc >> 4]; // 输出高4位
*pDst++ = tab[*pSrc & 0x0f]; // 输出低4位
pSrc++;
}
// 输出字符串加个结束符
*pDst = '\0';
// 返回目标字符串长度
return (nSrcLength * 2);
}
// 7bit编码
// 输入: pSrc - 源字符串指针
// nSrcLength - 源字符串长度
// 输出: pDst - 目标编码串指针
// 返回: 目标编码串长度
int CSMSTest::gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
int nSrc; // 源字符串的计数值
int nDst; // 目标编码串的计数值
int nChar; // 当前正在处理的组内字符字节的序号,范围是0-7
unsigned char nLeft; // 上一字节残余的数据
// 计数值初始化
nSrc = 0;
nDst = 0;
// 将源串每8个字节分为一组,压缩成7个字节
// 循环该处理过程,直至源串被处理完
// 如果分组不到8字节,也能正确处理
while (nSrc < nSrcLength)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -