// modemDlg.cpp : implementation file
//
#include "stdafx.h"
#include "modem.h"
#include "modemDlg.h"
#include "AboutHelpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static BOOL bClicked = FALSE;
/////////////////////////////////////////////////////////////////////////////
// CModemDlg dialog
CModemDlg::CModemDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModemDlg::IDD, pParent),TIMER_ID1(1),TIMER_ID2(2)
{
//{{AFX_DATA_INIT(CModemDlg)
m_editchar = _T("");
m_modem = _T("");
m_com = _T("");
m_phone = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CModemDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModemDlg)
DDX_Control(pDX, IDhangup, m_hangup);
DDX_Control(pDX, IDcall, m_call);
DDX_Control(pDX, IDC_EDIT1, m_editctrl);
DDX_Text(pDX, IDC_EDIT1, m_editchar);
DDV_MaxChars(pDX, m_editchar, 40);
DDX_Text(pDX, IDC_EDIT3, m_modem);
DDX_Text(pDX, IDC_EDIT2, m_com);
DDX_Text(pDX, IDC_EDIT4, m_phone);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModemDlg, CDialog)
//{{AFX_MSG_MAP(CModemDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDcall, Oncall)
ON_BN_CLICKED(IDhangup, Onhangup)
ON_WM_CREATE()
ON_WM_CLOSE()
ON_WM_TIMER()
ON_BN_CLICKED(IDEXIT, OnExit)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(ID_HELP, OnHelp)
ON_BN_CLICKED(ID_HANGUP, Onhangup)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModemDlg message handlers
BOOL CModemDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
// TODO: Add extra initialization here
GetDlgItem(IDcall)->EnableWindow(FALSE);
GetDlgItem(IDhangup)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
vcon=0;
vconcount=0;
ring=0;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
// 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 CModemDlg::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 CModemDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModemDlg::OnChangeEdit1()
{
UpdateData();
if(m_editchar.GetLength())
GetDlgItem(IDcall)->EnableWindow(TRUE);
else
GetDlgItem(IDcall)->EnableWindow(FALSE);//无输入时禁止按钮
}
void CModemDlg::Oncall()
{
GetDlgItem(IDhangup)->EnableWindow(TRUE);
GetDlgItem(IDcall)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
m_editctrl.GetWindowText(m_editchar);
char strOutMsg[50];
DWORD dwBytesWritten;
wsprintf(strOutMsg,"ATDT%s\r",m_editchar);
BOOL bReturn=WriteFile(hCommDev,"AT#CLS=8#VLS=8#VGT=131#VSR=8000\r",strlen("AT#CLS=8#VLS=8#VGT=131#VSR=11025\r"),&dwBytesWritten,NULL);
if(!bReturn)
{
m_phone="语音模式设置......失败!";
}
else
m_phone="语音模式设置......成功!";
Sleep(500);
bReturn=WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);
if(!bReturn)
{
m_phone="拨号连接......失败!";
UpdateData(FALSE);
return;
}
m_phone="正在呼叫连接... "+m_editchar;
UpdateData(FALSE);
}
void CModemDlg::Onhangup()
{
GetDlgItem(IDhangup)->EnableWindow(FALSE);
GetDlgItem(IDcall)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
m_editctrl.EnableWindow(TRUE);
m_editctrl.GetWindowText(m_editchar);
DWORD dwBytesWritten;
BOOL bReturn=WriteFile(hCommDev,"\r",strlen("\r"),&dwBytesWritten,NULL);
if(!bReturn)
{
m_phone="挂断连接......失败!";
UpdateData(FALSE);
return;
}
Sleep(1000);
bReturn=WriteFile(hCommDev,"+++",strlen("+++"),&dwBytesWritten,NULL);
if(!bReturn)
{
m_phone="无法与调制解调器通讯,挂断连接......失败!";
UpdateData(FALSE);
return;
}
m_phone="正在挂断连接... "+m_editchar;
UpdateData(FALSE);
Sleep(1000);
bReturn=WriteFile(hCommDev,"ATZ\r",strlen("ATZ\r"),&dwBytesWritten,NULL);
if(!bReturn)
{
m_phone="无法与调制解调器通讯,挂断连接......失败!";
UpdateData(FALSE);
return;
}
vconcount=0;
}
int CModemDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
DCB dcb;
char strOutNumber[50];
DWORD dwBytesWritten;
BOOL bReturn,flagcom=FALSE,flagmodem=FALSE;
char strOutMsg[20]="ATS0=0V1M0\r";
hCommDev=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if(hCommDev!=(HANDLE)-1)
{
flagcom=TRUE;
BuildCommDCB("COM1:115200,n,8,1",&dcb);
SetCommState(hCommDev,&dcb); //以上为初始化串口
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
SetCommTimeouts(hCommDev,&CommTimeOuts);
PurgeComm(hCommDev,PURGE_TXCLEAR);
PurgeComm(hCommDev,PURGE_RXCLEAR); //设置读写串口超时时间
bReturn=WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);
if(!bReturn)
flagmodem=FALSE;
else
flagmodem=TRUE;
}
else
flagcom=FALSE;
if(flagcom==FALSE||flagmodem==FALSE)
{
CloseHandle(hCommDev);
hCommDev=CreateFile("COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if(hCommDev!=(HANDLE)-1)
{
flagcom=TRUE;
BuildCommDCB("COM2:115200,n,8,1",&dcb);
SetCommState(hCommDev,&dcb); //以上为初始化串口
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
SetCommTimeouts(hCommDev,&CommTimeOuts);
PurgeComm(hCommDev,PURGE_TXCLEAR);
PurgeComm(hCommDev,PURGE_RXCLEAR); //设置读写串口超时时间
bReturn=WriteFile(hCommDev,strOutMsg,strlen(strOutMsg),&dwBytesWritten,NULL);
if(!bReturn)
flagmodem=FALSE;
else
flagmodem=TRUE;
}
else
flagcom=FALSE;
}
if(flagcom==FALSE||flagmodem==FALSE)
{
CloseHandle(hCommDev);
hCommDev=CreateFile("COM3",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if(hCommDev!=(HANDLE)-1)
{
flagcom=TRUE;
BuildCommDCB("COM1:115200,n,8,1",&dcb);
SetCommState(hCommDev,&dcb); //以上为初始化串口
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeou