?? myserialdlg.cpp
字號:
// MyserialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Myserial.h"
#include "MyserialDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyserialDlg dialog
CMyserialDlg::CMyserialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyserialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyserialDlg)
m_strSendEdit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyserialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyserialDlg)
DDX_Text(pDX, IDC_EDIT_WRITE, m_strSendEdit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyserialDlg, CDialog)
//{{AFX_MSG_MAP(CMyserialDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyserialDlg message handlers
BOOL CMyserialDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyserialDlg::OnButton1() //關(guān)閉串口
{
// TODO: Add your control notification handler code here
if (m_ExitThreadEvent != NULL)
{
SetEvent(m_ExitThreadEvent); /* 通知線程退出 */
Sleep(1000);
CloseHandle(m_ExitThreadEvent);
m_ExitThreadEvent = NULL;
}
ClosePort();
}
void CMyserialDlg::OnWrite()
{
int rc;
UpdateData(TRUE);
int len = m_strSendEdit.GetLength(); /* 取得輸入字符串長度 */
char *psendbuf = new char[len];
DWORD dwactlen;
for(int i = 0; i < len;i++)
psendbuf[i] = (char)m_strSendEdit.GetAt(i); /* 轉(zhuǎn)換為單字節(jié)字符 */
rc=WriteFile(m_hSerial, psendbuf, len, &dwactlen, NULL); /* 從串口發(fā)送數(shù)據(jù) */
delete[] psendbuf;
}
void CMyserialDlg::OnOpen()
{
// TODO: Add your control notification handler code here
m_hSerial = CreateFile(L"COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(m_hSerial == NULL)
{
MessageBox(_T("無法打開端口或端口已打開!請檢查是否已被占用."));
}
///配置串口
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
// 默認(rèn)串口參數(shù)
GetCommState(m_hSerial, &PortDCB);
PortDCB.BaudRate = 9600; // baud
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONE5STOPBITS;
if (! SetCommState(m_hSerial, &PortDCB))
{
MessageBox(_T("配置串口失敗"));
}
////配置超時(shí)值
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(m_hSerial, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 10;
CommTimeouts.ReadTotalTimeoutConstant = 10;
CommTimeouts.WriteTotalTimeoutMultiplier = 50;
CommTimeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(m_hSerial, &CommTimeouts))
{
MessageBox(_T("不能設(shè)置超時(shí)參數(shù)"));
}
DWORD IDThread;
HANDLE hRecvThread;
m_ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
/* 創(chuàng)建串口接收線程退出事件*/
// 創(chuàng)建串口接收線程
hRecvThread = CreateThread(0, 0, CommRecvTread, this, 0, &IDThread);
if (hRecvThread == NULL)
{
MessageBox(_T("創(chuàng)建接收線程失敗!"));
return;
}
CloseHandle(hRecvThread);
}
DWORD CMyserialDlg::CommRecvTread(LPVOID lparam)
{
DWORD dwLength;
char *recvBuf = new char[1024];
CMyserialDlg *pDlg = (CMyserialDlg*)lparam;
while(TRUE)
{ /* 等待線程退出事件 */
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;
if (pDlg->m_hSerial != INVALID_HANDLE_VALUE)
{ /* 從串口讀取數(shù)據(jù) */
BOOL fReadState = ReadFile(pDlg->m_hSerial, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
// AfxMessageBox(_T("無法從串口讀取數(shù)據(jù)!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg,recvBuf,dwLength);
}
}
}
delete[] recvBuf;
return 0;
}
void CALLBACK CMyserialDlg::OnCommRecv(CWnd* pWnd, char *buf, int buflen)
{
CString tmp;
CMyserialDlg * pDlg = (CMyserialDlg*)pWnd;
CEdit *pRecvStrEdit = (CEdit*)pDlg->GetDlgItem(IDC_REC_DISP);
/* 取得控件指針 */
for (int i = 0; i < buflen; i++, buf++)
{
tmp.Format(_T("%c"), *buf); /* 將字符轉(zhuǎn)換為字符串 */
pDlg->m_strRecDisp += tmp;
}
pRecvStrEdit->SetWindowText(pDlg->m_strRecDisp); /* 顯示在窗口上 */
}
BOOL CMyserialDlg::ClosePort()
{
if(m_hSerial != NULL)
{
SetCommMask(m_hSerial, 0);
PurgeComm(m_hSerial, PURGE_TXCLEAR | PURGE_RXCLEAR); /* 清除收/發(fā)緩沖 */
CloseHandle(m_hSerial); /* 關(guān)閉串口操作句柄 */
m_hSerial = NULL;
return TRUE;
}
return FALSE;
}
void CMyserialDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (m_ExitThreadEvent != NULL)
{
SetEvent(m_ExitThreadEvent); /* 通知線程退出 */
Sleep(1000);
CloseHandle(m_ExitThreadEvent);
m_ExitThreadEvent = NULL;
}
ClosePort();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -