📄 mobile.cpp
字号:
// Mobile.cpp: implementation of the CMobile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "SMS.h"
#include "Mobile.h"
#include "ComPort.h"
#include "ComPorts.h"
#include "SerialPort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define MAXLENGTH 59
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//extern LsComm::CComPort cs;
extern CComPorts cs;
BOOL ReceiveOpen = FALSE;
CMobile::CMobile()
{
// cs = NULL;
m_orderThread = NULL;
hEvent = NULL;
InitializeCriticalSection(&lpCriticalSection);
//lpCriticalSection
}
CMobile::~CMobile()
{
// if(cs != NULL)
// delete cs;
if(m_orderThread != NULL)
{
CloseHandle(m_orderThread);
m_orderThread = NULL;
}
if(hEvent != NULL)
{
CloseHandle(hEvent);
}
DeleteCriticalSection(&lpCriticalSection);
}
BOOL CMobile::TestConnect()
{
return TRUE;
}
BOOL CMobile::ConnectMobile(int portnum, int band)
{
return TRUE;
}
BOOL CMobile::DisconnectMobile()
{
return TRUE;
}
BOOL CMobile::SendSMS(char *PhoneNumber, int phonelen, char *Text, int len)
{
if(len > MAXLENGTH)
{
AfxMessageBox(_T("Message is too long!"));
return FALSE;
}
char buffer[60];
char ret[1024];
char strPhone[20];
strcpy(strPhone,PhoneNumber);
memset(buffer,0,sizeof(buffer));
sprintf(buffer,"%s%s%s%s\r",_T("at+cmgs="),"\"",strPhone,"\"");
EnterCriticalSection(&lpCriticalSection);
cs.Output(buffer,phonelen+11);
Sleep(1000);
cs.GetInput(ret,1024);
if( (strstr(ret,_T("\r\n> ")) != NULL))
{
AfxMessageBox(_T("发送配置成功"));
}
else
{
AfxMessageBox(_T("发送配置失败!"));
LeaveCriticalSection(&lpCriticalSection);
return FALSE;
}
//Sleep(3000);
memset(buffer,0,sizeof(buffer));
strcpy(buffer,Text);
buffer[len] = 0x1a;
AfxMessageBox(buffer);
cs.Output(buffer,len+1);
Sleep(5000);
cs.GetInput(ret,1024);
if( (strstr(ret,_T("ok"))!=NULL)||(strstr(ret,_T("OK"))!=NULL) )
{
AfxMessageBox(_T("信息发送成功"));
}
else if( (strstr(ret,_T("error"))!=NULL)||(strstr(ret,_T("ERROR"))!=NULL) )
{
AfxMessageBox(_T("信息发送失败"));
LeaveCriticalSection(&lpCriticalSection);
return FALSE;
}
else
{
AfxMessageBox(_T("信息发送失败"));
LeaveCriticalSection(&lpCriticalSection);
return FALSE;
}
LeaveCriticalSection(&lpCriticalSection);
return TRUE;
}
DWORD WINAPI ReceiveSMS(LPVOID param);
BOOL CMobile::StartReceive()
{
ReceiveOpen = TRUE;
m_orderThread = CreateThread(NULL, 0, ReceiveSMS, this, 0, NULL);
return TRUE;
}
void ParseSMS(CString message);
DWORD WINAPI ReceiveSMS(LPVOID param)
{
char buffer[] = "AT+CMGL=\"REC UNREAD\"\r";
char ret[1024];
CMobile * mobile = (CMobile*)param;
memset(ret,0,sizeof(ret));
CString ReceiveMessage;
while(ReceiveOpen)
{
// if(mobile->SendFlag)
// continue;
EnterCriticalSection(&mobile->lpCriticalSection);
cs.Output(buffer,21);
Sleep(3000);
memset(ret,0,sizeof(ret));
cs.GetInput(ret,1024);
if( (strstr(ret,"error") != NULL) || (strstr(ret,"ERROR") != NULL) )
{
AfxMessageBox(_T("读取信息失败!"));
Sleep(3000);
continue;
}
if( (strstr(ret,"ok")!= NULL) || (strstr(ret,"OK") != NULL) )
{
ReceiveMessage = ret;
mobile->ParseSMS(ReceiveMessage);
}
LeaveCriticalSection(&mobile->lpCriticalSection);
Sleep(3000);
}
return TRUE;
}
void CMobile::ParseSMS(CString message)
{
int index = 0;
CString temp1, temp2;
CString ReveiveMessage;
char MessageIndex;
CString tel;
CString time;
int index1 = 0;
int index2 = 0;
int length = 0;
int sublength = 0;
int index3 = 0;
int index4 = 0;
int index5 = 0;
while(TRUE)
{
index = message.Find(_T("+CMGL:"));
length = message.GetLength();
//index = message.Find("<data>");
if(index == -1)
break;
temp1 = message.Right(length-index);
index3 = temp1.Find(" ");
//MessageIndex = temp1.Mid(index3+1,1);
MessageIndex = temp1.GetAt(index3+1);
index3 = temp1.Find("\"+");
index4 = temp1.Find("\"",index3+2);
tel = temp1.Mid(index3+1,index4-index3-2);
index5 = temp1.Find(",,");
sublength = temp1.GetLength();
index1 = temp1.Find("\r\n");
time = temp1.Mid(index5+2,index1-index5-2);
index2 = temp1.Find("\r\n",index1+2);
ReveiveMessage = temp1.Mid(index1+2,index2-index1-2);
message = temp1.Right(sublength-index2-2);
// AfxMessageBox(ReveiveMessage);
DeleteMessage(MessageIndex);
// temp = message.GetAt(message.Find(_T("<index>"))+7);
// StoreIndex = atoi(temp);
// DataPosition = message.Find(_T("<data>"));
// DataLength = message.GetAt(message.Find(_T("<length>"))+8);
// ReveiveMessage = message.Mid(DataPosition+6,DataPosition+6+atoi(DataLength));
// AfxMessageBox(ReveiveMessage);
// //AddToReceiveDataPool
// message = message.Right(DataPosition+6+atoi(DataLength));
// DeleteMessage(StoreIndex);
// //DeleteMessage
}
}
BOOL CMobile::CloseReceive()
{
ReceiveOpen = FALSE;
if(m_orderThread != NULL)
return CloseHandle(m_orderThread);
return true;
}
BOOL CMobile::DeleteMessage(char index)
{
char buffer[20];
sprintf(buffer,"%s%c\r",_T("AT+CMGD="),index);
char ret[1024];
cs.Output(buffer,10);
Sleep(3000);
cs.GetInput(ret,1024);
if( (strstr(ret,_T("ok"))!=NULL)||(strstr(ret,_T("OK"))!=NULL) )
{
AfxMessageBox(_T("信息删除成功"));
return TRUE;
}
else if( (strstr(ret,_T("error"))!=NULL)||(strstr(ret,_T("ERROR"))!=NULL) )
{
AfxMessageBox(_T("信息删除失败"));
return FALSE;
}
else
{
AfxMessageBox(_T("信息发送失败"));
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -