📄 phoneport.cpp
字号:
// PhonePort.cpp: implementation of the CPhonePort class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TrafficExecLaw.h"
#include "PhonePort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CTrafficExecLawApp theApp;
#include "Phone_Dlg.h"
UINT AFX_CDECL ProbIncomingCallTrd(LPVOID *lpData)
{
MSG msg;
CPhonePort *pc_pp=(CPhonePort *)lpData;
while (true)
{
if(::PeekMessage (&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message ==WU_PHONE_QUIT_PROB)break;
}
if(pc_pp->ProbWrkSta ==PROB_INCOMING_CALL)
pc_pp->ProbIncomingCall ();
if(pc_pp->ProbWrkSta == PROB_HANGUP)
pc_pp->ProbHangup();
}
return 0;
}
UINT AFX_CDECL PhoneThrd(LPVOID *lpData)
{
MSG msg;
CPhonePort *pc_pp=(CPhonePort *)lpData;
CPhone_Dlg *ppd=NULL;
int nProbIncCall=1000;
while (true)
{
if(::PeekMessage (&msg,NULL,0,0,PM_REMOVE))
{
::TranslateMessage (&msg);
::DispatchMessage (&msg);
if(msg.message ==WU_PHONE_QUIT)break;
switch (msg.message)
{
case WU_PHONE_CLOSE:
ppd->DestroyWindow ();
delete ppd;
ppd=NULL;
break;
case WU_PHONE_OPEN:
if(ppd==NULL)
{
ppd=new CPhone_Dlg;
ppd->Create (IDD_DIALOG_Phone,NULL);
::SetWindowPos (ppd->m_hWnd,HWND_TOPMOST,
0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
}
else
ppd->ShowWindow (SW_SHOW);
break;
case WU_PHONE_NEW_COMEING_CALL:
ppd->SendMessage (WU_PHONE_NEW_COMEING_CALL);
break;
}
}
Sleep(2);
}
if(ppd){
ppd->DestroyWindow ();
delete ppd;
}
return 0;
}
CPhonePort::CPhonePort()
{
this->fUsePhoneSys =FALSE;
}
CPhonePort::~CPhonePort()
{
}
int CPhonePort::Send(const char *strAT)
{
int len=strlen(strAT);
DWORD dwBytesWrite;
::WriteFile (this->hCom3,strAT,len,&dwBytesWrite,NULL);
return dwBytesWrite;
}
int CPhonePort::Recv(char *buf, int len)
{
DWORD dwBytesRead;
::ReadFile(hCom3,buf,len,&dwBytesRead,NULL);
return dwBytesRead;
}
void CPhonePort::ProbIncomingCall()
{
char buf[64];
//check com2 to determine incoming call
if(GetNextText (buf)>0)
{
if(strcmp(buf,"RING")==0){
pPhoneTrd ->PostThreadMessage (WU_PHONE_OPEN,0,0);
pPhoneTrd ->PostThreadMessage (WU_PHONE_NEW_COMEING_CALL,0,0);
this->ProbWrkSta=PROB_NONE;
}
}
}
int CPhonePort::GetNextText(char *text)
{
int i=0;
char c;
//skip \r and \n
while (true)
{
if(Recv (&c,1)==0)
goto end;
if(c==13 || c==10)
continue;
break;
}
while (true)
{
text[i++]=c;
if(Recv (&c,1)==0)
break;
if(c==13 || c==10)
break;
}
end: text[i]=0;
return i;
}
void CPhonePort::ProbHangup()
{
char buf[64];
//check com2 to determine incoming call
if(GetNextText (buf)>0)
{
if(strstr(buf,"VOICE CALL : END")){
this->ProbWrkSta =PROB_INCOMING_CALL;
}
}
}
void CPhonePort::InitPhoneSys()
{
this->hCom3=::CreateFile (L"COM3:",
GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
NULL);
if(this->hCom3==INVALID_HANDLE_VALUE)
return;
DCB dcb;
dcb.DCBlength =sizeof(DCB);
::GetCommState (hCom3,&dcb);
dcb.BaudRate =CBR_115200;
dcb.StopBits =ONESTOPBIT;
dcb.ByteSize =8;
::SetCommState (hCom3,&dcb);
this->pPhoneTrd =::AfxBeginThread ((AFX_THREADPROC)PhoneThrd,this);
this->pProbTrd =::AfxBeginThread ((AFX_THREADPROC)ProbIncomingCallTrd,this,
THREAD_PRIORITY_LOWEST);
this->ProbWrkSta =PROB_INCOMING_CALL;
}
void CPhonePort::ClosePhoneSys()
{
pProbTrd ->PostThreadMessage (WU_PHONE_QUIT_PROB,0,0);
pPhoneTrd ->PostThreadMessage (WU_PHONE_QUIT,0,0);
Sleep(50);
::CloseHandle (hCom3);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -