?? evctestcomdlg.cpp
字號:
// EVCTestCOMDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EVCTestCOM.h"
#include "EVCTestCOMDlg.h"
#include "winerror.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static HANDLE hComport;
/////////////////////////////////////////////////////////////////////////////
// CEVCTestCOMDlg dialog
CEVCTestCOMDlg::CEVCTestCOMDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEVCTestCOMDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEVCTestCOMDlg)
m_send = _T("");
m_recv = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEVCTestCOMDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEVCTestCOMDlg)
DDX_Text(pDX, IDC_EDIT1, m_send);
DDX_Text(pDX, IDC_EDIT2, m_recv);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEVCTestCOMDlg, CDialog)
//{{AFX_MSG_MAP(CEVCTestCOMDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEVCTestCOMDlg message handlers
BOOL CEVCTestCOMDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
DCB dcb;
COMMTIMEOUTS timeouts;
hComport = CreateFile(L"COM2:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hComport == INVALID_HANDLE_VALUE)
{
CString err;
err.Format(L"COM open failed ! error = %d", GetLastError());
MessageBox(err, L"Error", MB_ICONERROR);
}
else
{
GetCommState(hComport ,&dcb);
dcb.BaudRate = 38400;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.fParity = FALSE;
dcb.Parity = NOPARITY;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fErrorChar = FALSE;
dcb.fAbortOnError = FALSE;
SetCommState(hComport, &dcb);
timeouts.ReadIntervalTimeout = MAXWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComport, &timeouts);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CEVCTestCOMDlg::OnButton1()
{
char tx_buf[1024];
DWORD bytes_written;
UpdateData(TRUE);
for (int i = 0; i < m_send.GetLength(); i++)
{
tx_buf[i] = (char)m_send[i];
}
tx_buf[m_send.GetLength()] = '\0';
PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
WriteFile(hComport, tx_buf, (DWORD)strlen(tx_buf), &bytes_written, 0);
CString str;
str.Format(L"Send bytes: %d", bytes_written);
MessageBox(str);
}
void CEVCTestCOMDlg::OnButton2()
{
DWORD bytes_read = 0;
DWORD errors;
COMSTAT stat;
char response[1024];
response[0] = '\0';
ClearCommError(hComport, &errors, &stat);
if (stat.cbInQue > 0)
{
ReadFile(hComport, response, stat.cbInQue, &bytes_read, 0);
}
response[bytes_read] = '\0';
m_recv = response;
UpdateData(FALSE);
CString str;
str.Format(L"Receive bytes: %d", bytes_read);
MessageBox(str);
}
void CEVCTestCOMDlg::OnDestroy()
{
CDialog::OnDestroy();
PurgeComm(hComport, PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(hComport);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -