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

📄 atdlg.cpp

📁 VC++下通过计算机串口
💻 CPP
字号:
// AtDlg.cpp : implementation file
//

#include "stdafx.h"
#include "At.h"
#include "AtDlg.h"

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

BOOL startflag=0;
BOOL processflag=0;
BOOL timer1flag=0;
BOOL timeoutflag=0;

CString s="";
CString hangupstr="ATH0\r";
CString phonenumber="201,1,0588807971#,9392#,8616844#";   
CString settingstr="ATQ0E0V0X0S0=1&D0&K0\r";//&Y0&W0
CString DialNumber="ATDT"+phonenumber+"\r";
CString sendstr="jianghuiwen\r";
CString rs="";

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAtDlg dialog

CAtDlg::CAtDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAtDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAtDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAtDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAtDlg)
	DDX_Control(pDX, IDB_START, m_start);
	DDX_Control(pDX, IDC_ERROR, m_error);
	DDX_Control(pDX, IDC_STATUS, m_status);
	DDX_Control(pDX, IDC_MSCOMM1, m_com);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAtDlg, CDialog)
	//{{AFX_MSG_MAP(CAtDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDB_START, OnStart)
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_HANGUP, OnHangup)
	ON_BN_CLICKED(IDC_CONFIGMODEM, OnConfigmodem)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAtDlg message handlers

BOOL CAtDlg::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
	
	// TODO: Add extra initialization here
	m_com.SetCommPort(2);
	m_com.SetRThreshold(1);
	m_com.SetPortOpen(TRUE);

	if(	m_com.GetPortOpen()!=TRUE)	MessageBox("COM2打不开","ERROE!",MB_OK);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAtDlg::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 CAtDlg::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 CAtDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BEGIN_EVENTSINK_MAP(CAtDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CAtDlg)
	ON_EVENT(CAtDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CAtDlg::OnOnCommMscomm1() 
{
	CString strEvent, strError;

	switch(m_com.GetCommEvent())
	{
		case 1: // vbMSCommEvSend:
			strEvent.LoadString(IDS_SENDING);
			break;
		case 2: // vbMSCommEvReceive:
			//strEvent.LoadString(IDS_RECEIVING);
			ShowReturn(m_com.GetInput());
			break;
		case 3: // vbMSCommEvCTS:
			strEvent.LoadString(IDS_CTSCHANGE);
			break;
		case 4: // vbMSCommEvDSR:
			strEvent.LoadString(IDS_DSRCHANGE);
			break;
		case 5: // vbMSCommEvCD:
			strEvent.LoadString(IDS_CDCHANGE);
			break;
		case 6: // vbMSCommEvRing:
			strEvent.LoadString(IDS_PHONERING);
			break;
		case 7: // vbMSCommEvEOF:
			strEvent.LoadString(IDS_EOFDETECTED);
			break;

		case 1001: //  vbMSCommErBreak:
			strError.LoadString(IDS_BREAKRECEIVED);
			break;
		case 1002: // vbMSCommErCTSTO:
			strError.LoadString(IDS_CTSTIMEOUT);
			break;
		case 1003: // vbMSCommErDSRTO:
			strError.LoadString(IDS_DSRTIMEOUT);
			break;
		case 1004: // vbMSCommErFrame:
			strError.LoadString(IDS_FRAMEERROR);
			break;
		case 1006: // vbMSCommErOverrun:
			strError.LoadString(IDS_OVERRUNERROR);
			break;
		case 1007: // vbMSCommErCDTO:
			strError.LoadString(IDS_CDTIMEOUT);
			break;
		case 1008: // vbMSCommErRxOver:
			strError.LoadString(IDS_RBUFFEROVERFLOW);
			break;
		case 1009: // vbMSCommErRxParity:
			strError.LoadString(IDS_PARITYERROR);
			break;
		case 1010: // vbMSCommErTxFull:
			strError.LoadString(IDS_TBUFFERFULL);
			break;
	}

	if (!strEvent.IsEmpty())
		m_status.SetWindowText(strEvent);
	else
		if (!strError.IsEmpty())
		{
			MessageBeep(MB_ICONEXCLAMATION);
			m_error.SetWindowText(strError);
			strError += _T("\nOK to ignore, Cancel to quit");
			int ret = AfxMessageBox(strError,MB_OKCANCEL|MB_ICONEXCLAMATION);
			if (ret==IDCANCEL)	m_com.SetPortOpen(FALSE);
		}	
}
void CAtDlg::ShowReturn(VARIANT var)
{
	rs=var.bstrVal;
	m_error.SetWindowText(rs);
	
/*	if(rs.GetLength()!=0 && rs.GetAt(0)=='1')
	{
		delay(5);
		Send("jianghuiwen\r");
	}*/
	s.Insert(s.GetLength(),rs);
		m_error.SetWindowText(s);
if(s.Find("jianghuiwen",0)!=-1)
{
	Send("liqinghua");
	s.Empty();
}

	/*rs.Insert(rs.GetLength(),s);
	int k=m_com.GetInBufferCount();
	//if(k==0)
	{
		MessageBox(rs,"Return",MB_OK|MB_ICONEXCLAMATION);
		rs.Empty();
	}
/*	switch(processflag)
	{
	case 1:
		
	}*/

}

void CAtDlg::OnStart() 
{
	if(m_com.GetPortOpen()!=TRUE) 
	{
		m_com.SetCommPort(2);
		m_com.SetRThreshold(1);
		m_com.SetPortOpen(TRUE);
	}
	if(	m_com.GetPortOpen()!=TRUE)
	{
		MessageBox("串口打不开","ERROE!",MB_OK);
		return;
	}	
   timer1flag=1;
   SetTimer(1,50,NULL);

}
void CAtDlg::Send(CString str)
{
	VARIANT var;
	var.vt = VT_BSTR;
	var.bstrVal =str.AllocSysString();
	m_com.SetOutBufferCount(0);
	m_com.SetOutput(var);
	SysFreeString(var.bstrVal);
}

void CAtDlg::OnClose() 
{
	
  if(m_com.GetPortOpen()==TRUE)
	{
		OnHangup();
		delay(5);
	    m_com.SetPortOpen(FALSE);
	}
	CDialog::OnClose();
}
void CAtDlg::delay(int i)
{
	LONG w;
	int j;
	for(j=0;j<i;j++)
		for(w=0;w<0xffffff;w++);

}
void CAtDlg::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent==2)
	{
		timeoutflag=1;
		return;
	}

	if(nIDEvent==1)
	{ 
		if (timer1flag==0) return;
		switch(processflag)
		{
		case 0:
			//delay(1);
			if(!IsCTSTrue()) break;
			Send(settingstr);
			processflag=1;
			break; 
		case 1:
			if(rs.GetLength()!=0 && rs.GetAt(0)=='0')
			{
				//delay(1);
				if(!IsCTSTrue()) break;
				Send(DialNumber);
				rs.Empty();
				s.Empty();
				processflag=2;
				SetTimer(2,60000L,NULL);
				timeoutflag=0;
			}
			else
			{
				delay(1);
				processflag=0;
			}
			break;
		case 2:
			if(timeoutflag==1)
			{
				timeoutflag=0;
				KillTimer(2);
				//如果超时可再发送请求,规定次数内还未收到指定的内容,
				//则报告错误,然后挂断
				//delay(1);
				OnHangup();
				processflag=4;
			}
			if(rs.GetLength()!=0 && rs.GetAt(0)=='1')
			{
				timeoutflag=0;
				KillTimer(2);
				if(!IsCTSTrue()) break;
				rs.Empty();
				s.Empty();
				Send(sendstr);
				//m_error.SetWindowText(sendstr);
				processflag=3;
				SetTimer(2,60000L,NULL);
				timeoutflag=0;
			}
			break;
		case 3:
			if(timeoutflag==1)
			{
				timeoutflag=0;
				KillTimer(2);
				//delay(1);
				//如果超时可再拨几次,规定次数内还未建立连接,
				//则报告错误,然后挂断
				OnHangup();
				processflag=4;
			}
			if(s.Find("liqinghua",0)!=-1)
			{
				//收到指定的内容
				timeoutflag=0;
				KillTimer(2);
				rs.Empty();
				OnHangup();
				processflag=4;
			    m_status.SetWindowText(" Send OK");
				break;
			}
			break;
		case 4:
			if(rs.GetLength()!=0 && rs.GetAt(0)=='0')
			{
				timer1flag=0;
				processflag=0;
				rs.Empty();
				s.Empty();
				KillTimer(1);
			    m_status.SetWindowText("Hang up");
				break;
			}
			delay(1);
			OnHangup();
			break;
		}
	}// end nIDEvent==1

	CDialog::OnTimer(nIDEvent);
}

BOOL CAtDlg::IsCTSTrue()
{
	if(!m_com.GetCTSHolding())
	{
		return FALSE;
	}
	return TRUE;
}
void CAtDlg::OnHangup() 
{
	Send("+");
	Send("+");
	Send("+");
	delay(2);
	Send(hangupstr);
}

void CAtDlg::OnConfigmodem() 
{
	if(m_com.GetPortOpen()!=TRUE) 
	{
		m_com.SetCommPort(2);
		m_com.SetRThreshold(1);
		m_com.SetPortOpen(TRUE);
	}
	if(	m_com.GetPortOpen()!=TRUE)
	{
		MessageBox("串口打不开","ERROE!",MB_OK);
		return;
	}	

	//m_com.SetDTREnable(TRUE);
	delay(1);
	if(!m_com.GetCTSHolding())
	{
		for(int i=0;i<10;i++)
		{
			delay(1);
			if(m_com.GetCTSHolding()) break;
		}
		if(i==10) 
		{
			MessageBox("无法跟modem建立联系","错误",MB_ICONEXCLAMATION|MB_OK);
			return;
		}

	}
	Send("AT&F&W0\r");
	delay(5);
	Send(settingstr);	
}

⌨️ 快捷键说明

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