📄 testcomxdlg.cpp
字号:
// testcomxDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testcomx.h"
#include "testcomxDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CTestcomxDlg dialog
CTestcomxDlg::CTestcomxDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestcomxDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestcomxDlg)
// 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 CTestcomxDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestcomxDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestcomxDlg, CDialog)
//{{AFX_MSG_MAP(CTestcomxDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestcomxDlg message handlers
BOOL CTestcomxDlg::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
flag=FALSE;
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestcomxDlg::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 CTestcomxDlg::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 CTestcomxDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestcomxDlg::OnOK()
{
// TODO: Add extra validation here
memset(&comstate,0,sizeof(COMSTAT));
((CComboBox *)GetDlgItem(IDC_COMBO1))->GetWindowText(str1);
hcom=CreateFile(str1,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if(hcom==INVALID_HANDLE_VALUE)
{
dwerror=GetLastError();
str.Format("串口异常 %ld",dwerror);
MessageBox(str);
}
if(flag==TRUE)
{
MessageBox("串口已开");
}
flag=TRUE;
SetupComm(hcom,4096,4096);
GetCommTimeouts(hcom,&timeouts);
timeouts.ReadIntervalTimeout=MAXWORD;
timeouts.ReadTotalTimeoutMultiplier=0;
timeouts.ReadTotalTimeoutConstant=0;
timeouts.WriteTotalTimeoutMultiplier=0;
timeouts.WriteTotalTimeoutMultiplier=1000;
SetCommTimeouts(hcom,&timeouts);
GetCommState(hcom,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
dcb.Parity=ODDPARITY;
SetCommState(hcom,&dcb);
memset(&m_read,0,sizeof(OVERLAPPED));
memset(&m_write,0,sizeof(OVERLAPPED));
SetCommMask(hcom,EV_RXCHAR);
m_read.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_read.hEvent==NULL)
{
dwerror=GetLastError();
str.Format("读串口事件设置错误 %ld",dwerror);
MessageBox(str);
}
m_write.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_write.hEvent==NULL)
{
dwerror=GetLastError();
str.Format("写串口事件设置错误 %ld",dwerror);
MessageBox(str);
}
handle=CreateEvent(NULL,TRUE,TRUE,NULL);
if(handle==NULL)
return;
pthread=AfxBeginThread(ThreadFunc,(LPVOID)this,0,0,CREATE_SUSPENDED,NULL);
if(pthread!=NULL)
{
pthread->m_bAutoDelete=FALSE;
pthread->ResumeThread();
}
}
DWORD CTestcomxDlg::OnReadCom(byte *but,DWORD length)
{ ClearCommError(hcom,&dwerror,&comstate);
flag1=ReadFile(hcom,but,length,&length,&m_read);
if(flag1==FALSE)
{
dwerror=GetLastError();
if(dwerror==ERROR_IO_PENDING)
GetOverlappedResult(hcom,&m_read,&length,TRUE);
}
SetEvent(handle);
string+=but;
GetDlgItem(IDC_EDIT2)->SetWindowText(string);
return length;
}
DWORD CTestcomxDlg::OnWriteCom(byte *butt,DWORD length)
{ ClearCommError(hcom,&dwerror,&comstate);
flag1=WriteFile(hcom,butt,length,&length,&m_write);
if(flag1==FALSE)
{
dwerror=GetLastError();
if(dwerror==ERROR_IO_PENDING)
GetOverlappedResult(hcom,&m_write,&length,TRUE);
}
return length;
}
UINT ThreadFunc(LPVOID n)
{
CTestcomxDlg *pdlg=(CTestcomxDlg *)n;
COMSTAT comstat;
memset(&comstat,0,sizeof(COMSTAT));
DWORD dword;
DWORD dwmask;
OVERLAPPED m_os;
CString strth;
memset(&m_os,0,sizeof(OVERLAPPED));
m_os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_os.hEvent==NULL)
return -1;
while(pdlg->flag)
{
dwmask=0;
WaitCommEvent(pdlg->hcom,&dwmask,&m_os);
if(dwmask==EV_RXCHAR)
{
ClearCommError(pdlg->hcom,&dword,&comstat);
dword=comstat.cbInQue;
if(dword)
{
strth.Format("%ld",dword);
pdlg->OnDisplay(strth);
WaitForSingleObject(pdlg->handle,INFINITE);//该事件用于控制线程保证 读完缓冲区后再循环
ResetEvent(pdlg->handle);
memset(pdlg->buf,0,sizeof(pdlg->buf));
pdlg->OnReadCom(pdlg->buf,dword);
continue;
}
}
else
{
if(GetLastError()==ERROR_IO_PENDING)
GetOverlappedResult(pdlg->hcom,&m_os,&dword,TRUE);//等待通信事件结束是个重叠操作若结束线程时没有setcommmask(hcom,0)将一直等待
else
{
CloseHandle(m_os.hEvent);
//if(pdlg->flag==FALSE)
return -1;
}
}
// Sleep(1);
}
CloseHandle(m_os.hEvent);
return 0;
}
void CTestcomxDlg::OnChangeEdit1()
{
// 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 CTestcomxDlg::OnButton1()
{
// TODO: Add your control notification handler code here
string=_T("");
memset(buff,0,sizeof(buff));
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
memcpy(buff,str,strlen(str));
OnWriteCom(buff,strlen(str));
str2.Format("%d",strlen(str));
GetDlgItem(IDC_STATICD)->SetWindowText(str2);
}
void CTestcomxDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(flag==TRUE)
{
SetCommMask(hcom,0);
SetEvent(handle);
if(handle)
CloseHandle(handle);
flag=FALSE;
WaitForSingleObject(pthread->m_hThread,INFINITE);
pthread=NULL;
if(m_read.hEvent!=NULL)
CloseHandle(m_read.hEvent);
if(m_write.hEvent!=NULL)
CloseHandle(m_write.hEvent);
CloseHandle(hcom);
}
CDialog::OnCancel();
}
void CTestcomxDlg::OnDisplay(CString s)
{
GetDlgItem(IDC_DISP)->SetWindowText(s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -