?? commserialdlg.cpp
字號:
// CommSerialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CommSerial.h"
#include "CommSerialDlg.h"
#include "afxmt.h"
#include ".\commserialdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BUFFER_SIZE 2048
HANDLE m_hCommPort; //保存打開的串行口設備句柄
UINT ReadChar(PVOID hWnd); //讀取字符線程
char RecvBuf[BUFFER_SIZE], SendBuf[BUFFER_SIZE];
UINT RecvPTR;
CEvent SendEvent(0,true,0,0),RecvEvent(0,true,0,0);
OVERLAPPED SendOV,RecvOV;
/////////////////////////////////////////////////////////////////////////////
// CCommSerialDlg dialog
CCommSerialDlg::CCommSerialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommSerialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommSerialDlg)
// 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 CCommSerialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommSerialDlg)
DDX_Control(pDX, IDOK, m_OK);
DDX_Control(pDX, IDC_SEND, m_Send);
DDX_Control(pDX, IDC_RECV, m_Receive);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommSerialDlg, CDialog)
//{{AFX_MSG_MAP(CCommSerialDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommSerialDlg message handlers
BOOL CCommSerialDlg::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
// TODO: Add extra initialization here
RecvPTR=0;
DCB CommDCB; //串行口的設備控制塊
m_hCommPort=CreateFile ("COM1", //打開串行口
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(m_hCommPort==INVALID_HANDLE_VALUE)
{
AfxMessageBox("系統無法打開此串口設備(CreateFile)!");
PostMessage(WM_QUIT,0,0);
return false;
}
if(!GetCommState(m_hCommPort,&CommDCB)) //得到原來的串口參數
{
AfxMessageBox("無法得到設置串口狀態(GetCommState)!");
PostMessage(WM_QUIT,0,0);
return false;
}
CommDCB.BaudRate =CBR_9600; //設置新的串口參數
CommDCB.ByteSize =8;
CommDCB.Parity = NOPARITY;
CommDCB.StopBits = ONESTOPBIT ;
CommDCB.fBinary = TRUE ;
CommDCB.fParity = FALSE;
if(!SetCommState(m_hCommPort, &CommDCB))
{
AfxMessageBox("無法設置串口狀態(SetCommState)!");
PostMessage(WM_QUIT,0,0);
return false;
}
if (!SetupComm(m_hCommPort,BUFFER_SIZE,BUFFER_SIZE)) //設置緩沖區
{
AfxMessageBox("串口緩沖區設置錯誤(SetupComm)!");
PostMessage(WM_QUIT,0,0);
return false;
}
if (!PurgeComm(m_hCommPort, PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT)) //清除所有接收和發送緩沖區中的數據
{
AfxMessageBox("清空串口錯誤(PurgeComm)!");
PostMessage(WM_QUIT,0,0);
return false;
}
memset(&SendOV,0,sizeof(SendOV)); //發送
SendOV.hEvent=SendEvent;
memset(&RecvOV,0,sizeof(RecvOV)); //發送
RecvOV.hEvent=RecvEvent;
AfxBeginThread(ReadChar,NULL,THREAD_PRIORITY_NORMAL);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CCommSerialDlg::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 CCommSerialDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT ReadChar(PVOID hWnd)
{
CString ShowStr;
ULONG ReadLen;
while (true) //只要線程運行,就監視端口是否收到數據
{
ReadFile(m_hCommPort,&RecvBuf[RecvPTR],1,&ReadLen,&RecvOV); //讀字符
GetOverlappedResult(m_hCommPort,&RecvOV,&ReadLen,TRUE); //等待讀狀態完成
if (ReadLen>0)
{
if (RecvBuf[RecvPTR]!=NULL) //不是一行的最后一個字符
RecvPTR++;
else
{
ShowStr="RECV:";
ShowStr=ShowStr+RecvBuf;
CCommSerialApp* pApp=(CCommSerialApp*)AfxGetApp();
CCommSerialDlg* pDlg=(CCommSerialDlg*)pApp->m_pMainWnd;
pDlg->m_Receive.InsertString (0,ShowStr);
RecvPTR=0;
}
}
RecvEvent.ResetEvent(); //復位事件變量
}
return 0;
}
void CCommSerialDlg::OnOK()
{
// TODO: Add extra validation here
ULONG SendLen,SentLen;
CString ShowStr="SEND:";
//獲取要發送的字符串
SendLen=m_Send.GetWindowText(SendBuf,BUFFER_SIZE-1);
if (SendLen==0)
{
AfxMessageBox("請輸入要傳輸的數據");
return;
}
SendBuf[SendLen++]=NULL;
m_Send.EnableWindow(FALSE);
m_OK.EnableWindow (FALSE);
WriteFile(m_hCommPort,SendBuf,SendLen,&SentLen,&SendOV);
GetOverlappedResult(m_hCommPort,&SendOV,&SentLen,TRUE); //等待發送完成
SendEvent.ResetEvent();
m_Send.EnableWindow(TRUE);
m_OK.EnableWindow (TRUE);
ShowStr=ShowStr+SendBuf; //顯示發送的消息
m_Receive.InsertString (0,(LPCTSTR)ShowStr);
m_Send.SetWindowText(NULL);
m_Send.SetFocus();
//CDialog::OnOK();
}
void CCommSerialDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
BOOL CCommSerialDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CloseHandle(m_hCommPort); //關閉串行口
return CDialog::DestroyWindow();
}
void CCommSerialDlg::OnBnClickedSend()
{
// TODO: 在此添加控件通知處理程序代碼
}
void CCommSerialDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知處理程序代碼
OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -