⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialdlg.cpp

📁 在配置文件中输入手机拨号序列。模拟手机呼叫。com串口编程
💻 CPP
字号:
// DialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Dial.h"
#include "DialDlg.h"

#include "DialPhone.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#include "Comm.h"
char *token;

CString	g_strPhone;
CString	g_strDtmf;
UINT	g_iMaxTime;
UINT	g_iInterTime;
CString	g_strCom;
UINT	g_iPort;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialDlg dialog

CDialDlg::CDialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialDlg)
	m_strPhone = _T("1015903119");
	m_strDtmf = _T("2:3:1");
	m_iMaxTime = 180000;
	m_iInterTime = 20000;
	m_strCom = _T("COM1");
	m_iPort = 9600;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialDlg)
	DDX_Text(pDX, IDC_Dial_Phone, m_strPhone);
	DDV_MaxChars(pDX, m_strPhone, 20);
	DDX_Text(pDX, IDC_DTMF, m_strDtmf);
	DDX_Text(pDX, IDC_MAXTIME, m_iMaxTime);
	DDX_Text(pDX, IDC_INTERTIME, m_iInterTime);
	DDX_Text(pDX, IDC_EDIT_COM, m_strCom);
	DDX_Text(pDX, IDC_EDIT_PORT, m_iPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDialDlg, CDialog)
	//{{AFX_MSG_MAP(CDialDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Dial, OnDial)
	ON_BN_CLICKED(IDC_HUNGUP, OnHungup)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_HungupCom, OnHungupCom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialDlg message handlers

DWORD WINAPI ProcessThread (LPVOID lp)
{
	char chDtmf[256];
	char chPhone[256];
	char chCom[10];
	UINT iPort;
	int iInterTime;
	int iMaxTime;
	SM_BUFF buff;
	char p_chDtmf[256];
	char *chtoken;
	
	sprintf(chDtmf,"%s",g_strDtmf);
	sprintf(chPhone,"%s",g_strPhone);
	iInterTime = g_iInterTime;
	iMaxTime = g_iMaxTime;
	sprintf(chCom,"%s",g_strCom);
	iPort = g_iPort;

	while(1)
	{
//		if (!::OpenComm(chCom, iPort))
//		{
//			CString strError;
//			strError.Format("无法打开端口%s port %s! 现在设置吗?",chCom,iPort);
//			TRACE(strError);
//			continue;
//		}
		sprintf(p_chDtmf,"%s",chDtmf);
		TRACE("chDtmf= %s ;chPhone = %s ;iInterTime = %d ;iMaxTime = %d ;\n",g_strDtmf,g_strPhone,g_iInterTime,g_iMaxTime);
		gsmSendPhone(chPhone);
		memset(&buff, 0, sizeof(buff));
		int cnt = 0;
		do
		{	
			Sleep(500);
			switch (gsmGetResponse(&buff))
			{
				case GSM_WAIT:
					break;
				case GSM_OK: 
					cnt = 10;
					break;
				case GSM_ERR:
					cnt = 20;
					break;
			}		
		}while( cnt++ < 20 );
		if (cnt == 11)
		{
			TRACE("Send Phone Ok!\n");
			chtoken = strtok( p_chDtmf, ":" );
			while(chtoken != NULL)
			{
				Sleep(iInterTime);
				gsmSendDtmf(chtoken);
				chtoken = strtok( NULL, ":" );
			}
		}
		else if (cnt == 21)
		{
			TRACE("Send Phone Error!\n");
			break;
		}
		else
		{
			TRACE("Send Phone Failed!\n");
			break;
		}
		Sleep(iMaxTime);
		gsmSendHungup();
		Sleep(2000);
//		::CloseComm();

	}

	return 0;
}
BOOL CDialDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	::CloseComm();
	memset(m_threadFlag,0,sizeof(m_threadFlag));

	char szIniFile[256];
//    memset(szIniFile,0,256);
    ::GetModuleFileName (NULL,szIniFile,256);//得到运行程序的全路径和程序名
    char * pPos=strrchr(szIniFile,'\\');
    pPos[1]='\0';
    strcat(szIniFile,"DialPhone.ini");

	 //从配置文件中读取数据库配置信息
	char szCom[256];
	char szPort[256];
	char szCOMName[256],szPORTName[256];
	char szTmp[256],szTmpName[256];	int iTmp;
	HANDLE hProcessThread;

	int rc = GetPrivateProfileString("SvcConfig","NUM_COM", "", szTmp, 256, szIniFile);
    if(rc < 1){
        PostMessage(WM_QUIT);
        return -2;
    }
	iTmp=atoi(szTmp);

	
	for (int i=1;i<=iTmp;i++)
	{
		TRACE("i = %d\n",i);
		sprintf(szCOMName,"COM_%d",i);
		sprintf(szPORTName,"PORT_%d",i);

		rc = GetPrivateProfileString("SvcConfig",szCOMName, "", szCom, 256, szIniFile);
		if(rc < 1){
			continue;
		}
		m_strCom = szCom;
		rc = GetPrivateProfileString("SvcConfig",szPORTName, "", szPort, 256, szIniFile);
		if(rc < 1){
			continue;
		}
		m_iPort = atoi(szPort);
		if (!::OpenComm(szCom, m_iPort))
		{
			CString strError;
			strError.Format("无法打开端口%s port %s! 现在设置吗?",szCom,szPort);
			AfxMessageBox(strError);
			continue;
		}
		
		sprintf(szTmpName,"PHONE_%d",i);
		rc = GetPrivateProfileString("SvcConfig",szTmpName, "", szTmp, 256, szIniFile);
		m_strPhone = szTmp;

		sprintf(szTmpName,"DTMF_%d",i);
		rc = GetPrivateProfileString("SvcConfig",szTmpName, "", szTmp, 256, szIniFile);
		m_strDtmf = szTmp;

		sprintf(szTmpName,"INTERTIME_%d",i);
		rc = GetPrivateProfileString("SvcConfig",szTmpName, "", szTmp, 256, szIniFile);
		m_iInterTime = atoi(szTmp)*1000;

		sprintf(szTmpName,"MAXTIME_%d",i);
		rc = GetPrivateProfileString("SvcConfig",szTmpName, "", szTmp, 256, szIniFile);
		m_iMaxTime = atoi(szTmp)*1000*60;

		g_strPhone = m_strPhone;
		g_strDtmf = m_strDtmf;
		g_iMaxTime = m_iMaxTime;
		g_iInterTime = m_iInterTime;
		g_strCom = m_strCom;
		g_iPort = m_iPort;		
		hProcessThread=CreateThread(NULL,0,ProcessThread, (LPVOID)this->m_hWnd,0,NULL);
		if(hProcessThread){
			sprintf(m_threadFlag[i].com ,"%s",szCom);
			m_threadFlag[i].handle = hProcessThread;
		}
		else{
			CString strError;
			strError.Format("创建com为%s的线程失败!",szCom);
			AfxMessageBox(strError);
			continue;
		}

		Sleep(500);
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDialDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDialDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDialDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDialDlg::OnDial() 
{
	char ans[128];		

	UpdateData();
	char chDial[128];
	sprintf(chDial,"AT+CKPD=\"#31#%s\"\r",m_strPhone);
	WriteComm(chDial, 26);
	Sleep(200);
	ReadComm(ans, 128);
	sprintf(m_chPhone,"%s",m_strDtmf);
	
	token = strtok( m_chPhone, ":" );
	SetTimer(1,m_iInterTime,NULL);

	SetTimer(2,m_iMaxTime,NULL);
}

void CDialDlg::OnHungup()
{
	char ans[128];
	WriteComm("ATH\r", 26);
	ReadComm(ans, 128);
	::CloseComm();
}

void CDialDlg::OnTimer(UINT nIDEvent) 
{
	//
	char ans[128];
	if ( nIDEvent == 1 )
	{
		if( token != NULL )
		{
			sprintf(ans,"AT+VTS=%s\r",token);
			WriteComm(ans, 26);
			Sleep(200);
			ReadComm(ans, 128);

			token = strtok( NULL, ":" );
		}
		else
		{
			KillTimer(nIDEvent);
		}
	}
	if ( nIDEvent == 2 )
	{
		OnHungup();
		KillTimer(nIDEvent);
		OnDial();
	}
	CDialog::OnTimer(nIDEvent);
}


void CDialDlg::OnHungupCom() 
{
	UpdateData();
	if (m_strCom == _T(""))
	{
		AfxMessageBox("请设置需要停止的com口!");
		return;
	}
	gsmSendHungup();
	for(int i=0;i<MAXCOM;i++)
	{
		if (m_threadFlag[i].com==m_strCom)
		{
			TerminateThread(m_threadFlag[i].handle,0);
			break;
		}
	}
	if (i == MAXCOM)
	{
		AfxMessageBox("无法停止该com口的线程!");
		return;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -