📄 client232dlg.cpp
字号:
// Client232Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client232.h"
#include "Client232Dlg.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()
/////////////////////////////////////////////////////////////////////////////
// CClient232Dlg dialog
CClient232Dlg::CClient232Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CClient232Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClient232Dlg)
// 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 CClient232Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClient232Dlg)
DDX_Control(pDX, IDC_MSG, m_stcMsg);
DDX_Control(pDX, IDC_FILENAME, m_edtFilename);
DDX_Control(pDX, IDC_PORT, m_cboComPort);
DDX_Control(pDX, IDC_BAUD, m_cboBaudRate);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClient232Dlg, CDialog)
//{{AFX_MSG_MAP(CClient232Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClient232Dlg message handlers
BOOL CClient232Dlg::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_cboComPort.SetCurSel(0);
m_cboBaudRate.SetCurSel(4);
m_edtFilename.SetWindowText(_T("C:\\PPP.zip"));
return TRUE; // return TRUE unless you set the focus to a control
}
void CClient232Dlg::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 CClient232Dlg::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 CClient232Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
#define WAITCMD_TIMEOUT 7000
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
#define PT_ACK 0x06
#define BLOCKSIZE 1000
HANDLE WINAPI OpenComport(DWORD dwComport, DWORD dwBaudRate)
{
HANDLE DriverHandle;
char Com_Name[10];
DWORD DCB_Baud_Rate, retval;
DCB Our_DCB;
//
// Range check the Comport number
//
if (dwComport > 16) {
return 0;
}
//
// Make a COMPORT name from a number
//
sprintf (Com_Name, _T("COM%i:"), dwComport);
//
// Convert the Baud Rate to the Com library define for the DCB
//
if(dwBaudRate < 300)
DCB_Baud_Rate = CBR_110;
else if(dwBaudRate <600)
DCB_Baud_Rate = CBR_300;
else if(dwBaudRate <1200)
DCB_Baud_Rate = CBR_600;
else if(dwBaudRate <2400)
DCB_Baud_Rate = CBR_1200;
else if(dwBaudRate <4800)
DCB_Baud_Rate = CBR_2400;
else if(dwBaudRate <9600)
DCB_Baud_Rate = CBR_4800;
else if(dwBaudRate <19200)
DCB_Baud_Rate = CBR_9600;
else if(dwBaudRate <38400)
DCB_Baud_Rate = CBR_19200;
else if(dwBaudRate <57600)
DCB_Baud_Rate = CBR_38400;
else if(dwBaudRate <115200)
DCB_Baud_Rate = CBR_57600;
else if(dwBaudRate <128000)
DCB_Baud_Rate = CBR_115200;
else
DCB_Baud_Rate = CBR_128000;
//
// Open a channel to the Comport - This example DOES NOT use overlapped I/O
// Since the benefits of overlapped I/O are few, we suggest not using it.
//
DriverHandle = CreateFile (Com_Name, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
//
// Do we have a valid handle? (If not, the driver probably isn't loaded)
//
if (DriverHandle == INVALID_HANDLE_VALUE) {
//MessageBox(0, _T("Open COM error"), _T(""), MB_OK);
OutputDebugString(_T("Open comport error!!\n"));
return (HANDLE)0;
} else {
//
// The SetupComm() function establishes the Transmit and Receive
// buffer sizes.
//
// SetupComm(DriverHandle, 10240, 10240);
//
// Obtain the current DCB structure. this can be saved away for restore after
// the application is done using the Comport
//
GetCommState (DriverHandle, &Our_DCB);
//
// Fill in the DCB structure with our own settings.
//
Our_DCB.BaudRate = DCB_Baud_Rate;
Our_DCB.fParity = 0;
Our_DCB.fOutxCtsFlow = 0 ;
Our_DCB.fOutxDsrFlow = 0;
Our_DCB.fDtrControl = DTR_CONTROL_ENABLE;
Our_DCB.fDsrSensitivity = FALSE;
Our_DCB.fTXContinueOnXoff = 0;
Our_DCB.fOutX = Our_DCB.fInX = FALSE;
Our_DCB.XonChar = ASCII_XON ;
Our_DCB.XoffChar = ASCII_XOFF ;
Our_DCB.fErrorChar = 0;
Our_DCB.fNull = 0;
Our_DCB.fRtsControl = RTS_CONTROL_DISABLE;
Our_DCB.fAbortOnError = 0;
Our_DCB.ByteSize = 8;
Our_DCB.Parity = NOPARITY;
Our_DCB.StopBits = ONESTOPBIT;
//
// Configure the comport with our new DCB
//
retval=SetCommState (DriverHandle, &Our_DCB);
//
// Setup a mask that allows us to tell if the port is done transmitting
// the current buffer of data
//
SetCommMask (DriverHandle, EV_TXEMPTY);
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(DriverHandle, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(DriverHandle, &CommTimeouts);
PurgeComm( DriverHandle, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
}
return DriverHandle;
}
DWORD WINAPI CloseComport(HANDLE hComport)
{
// purge any outstanding reads/writes and close device handle
PurgeComm(hComport, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle(hComport);
return 0;
}
//单
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -