📄 liuportdlg.cpp
字号:
// liuportDlg.cpp : implementation file
//
#include "stdafx.h"
#include "liuport.h"
#include "liuportDlg.h"
#include "SerialPort.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CLiuportDlg dialog
CLiuportDlg::CLiuportDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLiuportDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLiuportDlg)
m_SendText = _T("");
m_ReceiveText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLiuportDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLiuportDlg)
DDX_Control(pDX, IDC_COMBO4, m_ComboCom);
DDX_Text(pDX, IDC_EDIT2, m_SendText);
DDX_Text(pDX, IDC_EDIT3, m_ReceiveText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLiuportDlg, CDialog)
//{{AFX_MSG_MAP(CLiuportDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTONEXIT, OnButtonexit)
ON_BN_CLICKED(IDC_BUTTONSEND, OnButtonsend)
ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
ON_BN_CLICKED(IDC_BUTTONRECEIVE, OnButtonreceive)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLiuportDlg message handlers
BOOL CLiuportDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CLiuportDlg::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 CLiuportDlg::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 CLiuportDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CLiuportDlg::OnButton1()
{
// TODO: Add your control notification handler code here
char Command[50];
sprintf(Command,"F:\\CS1.6\\cstrike.exe");
int ret=system(Command);
}
void CLiuportDlg::OnButtonexit()
{
// TODO: Add your control notification handler code here
exit (0);
}
void CLiuportDlg::OnButtonsend()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_SendText.IsEmpty())
{
MessageBox(_T("Data has been not specified.Cancel Operation!"));
return;
}
HANDLE hCom;
LPDCB lpDcb;
int i=m_ComboCom.GetCurSel();
switch (i)
{
case 0:
hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 1:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 2:
hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 3:
hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
default:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
}
if (hCom==INVALID_HANDLE_VALUE)
{
MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
return ;
}
lpDcb=new(DCB);
if (!GetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Get Communication Port State.");
return ;
}
CString strTemp=_T("baud= parity= data= stop=");
strTemp.Insert(strTemp.Find(_T("baud="))+5,"9600");
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
if (!BuildCommDCB(strTemp,lpDcb))
{
MessageBox("Program Fails To Build Communication Port State.");
return ;
}
if (!SetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Set Communication Port State.");
return ;
}
DWORD dwTimeout=1000;
DWORD dwWritten;
DWORD textLen;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if (OL.hEvent ==NULL )
{
MessageBox(_T("Program Failed!Can Not Create Event"));
return ;
}
textLen=m_SendText.GetLength();
BOOL tf=WriteFile(hCom,m_SendText,textLen,&dwWritten,&OL);
if(!tf)
MessageBox(_T("Program Memory"));
CloseHandle(OL.hEvent);
CloseHandle(hCom);
}
void CLiuportDlg::OnChangeEdit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CLiuportDlg::OnButtonreceive()
{
// TODO: Add your control notification handler code here
HANDLE hCom;
LPDCB lpDcb;
int i=m_ComboCom.GetCurSel();
switch (i)
{
case 0:
hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 1:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 2:
hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
case 3:
hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
default:
hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
break;
}
if (hCom==INVALID_HANDLE_VALUE)
{
MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
return ;
}
lpDcb=new(DCB);
if (!GetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Get Communication Port State.");
return ;
}
CString strTemp=_T("baud= parity= data= stop=");
strTemp.Insert(strTemp.Find(_T("baud="))+5,"9600");
strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
if (!BuildCommDCB(strTemp,lpDcb))
{
MessageBox("Program Fails To Build Communication Port State.");
return ;
}
if (!SetCommState(hCom,lpDcb))
{
MessageBox("Program Fails To Set Communication Port State.");
return ;
}
BYTE* Buffer;
DWORD dwTimeout=1000;
DWORD dwRead;
DWORD dwResult;
OVERLAPPED OL={0};
long BufferIndex=0;
BOOL blContinue=TRUE;
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if (OL.hEvent ==NULL )
{
MessageBox(_T("Program Failed!Can Not Create Event"));
return ;
}
Buffer=(BYTE*) malloc(4000);
if(Buffer==NULL)
{
MessageBox(_T("Program Failed!Can Not Allocate Memory"));
return;
}
do{
dwRead=ReadFile(hCom,Buffer,1, &dwRead, &OL);
if (dwRead==0)
{
if (GetLastError() != ERROR_IO_PENDING)
dwRead=0 ;
else
{ // Write is pending.
dwResult = WaitForSingleObject(OL.hEvent, dwTimeout);
switch(dwResult)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hCom, &OL, &dwRead, FALSE))
dwRead=0 ;
break;
case WAIT_ABANDONED:
dwRead=0 ;
break;
case WAIT_TIMEOUT:
dwRead=0 ;
break;
default:
dwRead=0 ;
break;
}
}
if (dwRead==0)
blContinue=FALSE;
else
{
BufferIndex+=dwRead;
m_ReceiveText+=Buffer;
}
}
else
{
BufferIndex+=dwRead;
m_ReceiveText+=Buffer;
}
}while(blContinue);
UpdateData(FALSE);
CloseHandle(OL.hEvent);
CloseHandle(hCom);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -