📄 mycomdlg.cpp
字号:
// mycomDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mycom.h"
#include "mycomDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define INBUF_SIZE 10
#define OUTBUF_SIZE 10
char szReadBuffer[INBUF_SIZE];
char szWriteBuffer[OUTBUF_SIZE];
HANDLE hCom; //准备打开的串口的句柄
HANDLE hCommWatchThread; //辅助线程的全局函数
#define WM_SHOW_RECV_DATA WM_USER+100
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
DWORD WINAPI ReciveProc(HWND hSendWnd);
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()
/////////////////////////////////////////////////////////////////////////////
// CMycomDlg dialog
CMycomDlg::CMycomDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMycomDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMycomDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_isHex = FALSE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMycomDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMycomDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMycomDlg, CDialog)
//{{AFX_MSG_MAP(CMycomDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_SHOW_RECV_DATA, OnShowData)
ON_BN_CLICKED(IDC_BUTTON_RECV, OnButtonRecv)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_CHECK_HEXDECIMAL, OnCheckHexdecimal)
ON_CBN_SELCHANGE(IDC_COMBO_BAUTRATE, OnSelchangeComboBautrate)
ON_CBN_SELCHANGE(IDC_COMBO_BYTESIZE, OnSelchangeComboBytesize)
ON_CBN_SELCHANGE(IDC_COMBO_STOPBIT, OnSelchangeComboStopbit)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMycomDlg message handlers
BOOL CMycomDlg::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
CComboBox *pComBox;
pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_BAUTRATE);
pComBox->ResetContent();
pComBox->InsertString(0,"110");
pComBox->InsertString(1,"300");
pComBox->InsertString(2,"600");
pComBox->InsertString(3,"1200");
pComBox->InsertString(4,"2400");
pComBox->InsertString(5,"4800");
pComBox->InsertString(6,"9600");
pComBox->InsertString(7,"14400");
pComBox->InsertString(8,"19200");
pComBox->InsertString(9,"38400");
pComBox->InsertString(10,"57600");
pComBox->InsertString(11,"115200");
pComBox->InsertString(12,"128000");
pComBox->InsertString(13,"256000");
pComBox->SetCurSel(6);
pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_STOPBIT);
pComBox->ResetContent();
pComBox->InsertString(0,"1");
pComBox->InsertString(1,"1.5");
pComBox->InsertString(2,"2");
pComBox->SetCurSel(0);
pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_BYTESIZE);
pComBox->ResetContent();
pComBox->AddString("8");
pComBox->SetCurSel(0);
//得到串口句柄
hCom = GetCommHandle(_T("COM1"));
ASSERT(hCom != INVALID_HANDLE_VALUE);
//设置串口
SetDCB(hCom,9600,1,8);
InitCom(hCom);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMycomDlg::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 CMycomDlg::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 CMycomDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMycomDlg::SetDCB(HANDLE hCom, DWORD bautRate, UINT stopBit, UINT byteSize)
{
DCB dcb;
BOOL bSuccess;
memset(&dcb,0,sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
bSuccess = GetCommState(hCom, &dcb);
if(!bSuccess)
{
AfxMessageBox("can't get Com state!");
return;
}
dcb.BaudRate = bautRate;
dcb.ByteSize = byteSize; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
if(stopBit == 1)
{
dcb.StopBits = ONESTOPBIT; // one stop bit
}
else if(stopBit == 2)
{
dcb.StopBits = ONE5STOPBITS;//1.5 stop bit
}
else
{
dcb.StopBits = TWOSTOPBITS;//two stop bit
}
bSuccess = SetCommState(hCom, &dcb);
if(bSuccess)
{
//AfxMessageBox("初始化设置成功!");
}
}
HANDLE CMycomDlg::GetCommHandle(TCHAR *pcCommPort)
{
HANDLE hCom;
hCom = INVALID_HANDLE_VALUE;
hCom = CreateFile(pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED,//overlapped I/O
NULL); // hTemplate must be NULL for comm devices
return hCom;
}
void CMycomDlg::InitCom(HANDLE hCom)
{
ASSERT(hCom!=INVALID_HANDLE_VALUE);
SetCommMask(hCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm(hCom,INBUF_SIZE,OUTBUF_SIZE); //设置输入、输出缓冲区的大小
PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR
| PURGE_RXCLEAR);//清干净输入、输出缓冲区
COMMTIMEOUTS commTimeOuts;//定义超时结构,并填写该结构
commTimeOuts.ReadIntervalTimeout = 0; //interval time-outs are not used
commTimeOuts.ReadTotalTimeoutConstant = 0;
commTimeOuts.ReadTotalTimeoutMultiplier = 0;
commTimeOuts.WriteTotalTimeoutConstant = 0;
commTimeOuts.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(hCom, &commTimeOuts);//设置读写操作所允许的超时
}
DWORD WINAPI ReciveProc(HWND hSendWnd)
{
DWORD dwEvtMask=0;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
WaitCommEvent( hCom, &dwEvtMask,&overlapped);//等待串口通信事件的发生
//检测返回的dwEvtMask,知道发生了什么串口事件
if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
{
COMSTAT ComStat;
DWORD dwLength,dwBytesRead,dwErrorFlags;
ClearCommError(hCom, &dwErrorFlags, &ComStat);
dwLength = ComStat.cbInQue;//输入缓冲区有多少数据
if(dwLength > 0)
{
//Recv(hCom,szWriteBuffer,dwLength,dwBytesRead);
BOOL bReadStat;
bReadStat = ReadFile(hCom, //串口句柄
szReadBuffer,//
dwLength, //读最大数目
&dwBytesRead,//实际读得的数目
&overlapped);//
if(!bReadStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
while(!GetOverlappedResult(hCom,
&overlapped,
&dwBytesRead, TRUE));//
}
}
//通知主线程,串口收到数据
::PostMessage((HWND)hSendWnd,WM_SHOW_RECV_DATA,0,0);
}
}
CloseHandle(hCommWatchThread);
return 1;
}
void CMycomDlg::Send(HANDLE hCom,char *szWriteBuffer,DWORD dwBytesToWrite,DWORD dwBytesWritten)
{
BOOL bWriteStat;//
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
bWriteStat = WriteFile(hCom, szWriteBuffer, dwBytesToWrite,
&dwBytesWritten,
&overlapped);
if (!bWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
while(!GetOverlappedResult(hCom,//
&overlapped,//
&dwBytesWritten,//
TRUE));//
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -