?? recorderconsoledoc.cpp
字號:
// RecorderConsoleDoc.cpp : implementation of the CRecorderConsoleDoc class
//
#include "stdafx.h"
#include "RecorderConsole.h"
#include "comsettingdlg.h"
#include "RecorderConsoleDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleDoc
UINT CommProc(LPVOID pParam);
IMPLEMENT_DYNCREATE(CRecorderConsoleDoc, CDocument)
BEGIN_MESSAGE_MAP(CRecorderConsoleDoc, CDocument)
//{{AFX_MSG_MAP(CRecorderConsoleDoc)
ON_COMMAND(ID_COM_SETTING, OnComSetting)
ON_COMMAND(ID_COM_CONNECT, OnComConnect)
ON_COMMAND(ID_DISCONNECT, OnDisconnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleDoc construction/destruction
CRecorderConsoleDoc::CRecorderConsoleDoc()
{
// TODO: add one-time construction code here
m_bConnected=FALSE;
m_pThread=NULL;
m_nBaud=9600;
m_nDatabits=8;
m_nParity=0;
m_strPort="COM1";
m_nStopbits=0;
m_nFlowctrl=0;
}
CRecorderConsoleDoc::~CRecorderConsoleDoc()
{
}
BOOL CRecorderConsoleDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleDoc serialization
void CRecorderConsoleDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleDoc diagnostics
#ifdef _DEBUG
void CRecorderConsoleDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CRecorderConsoleDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleDoc commands
void CRecorderConsoleDoc::OnComSetting()
{
// TODO: Add your command handler code here
CComSettingDlg dlg;
CString str;
dlg.m_bConnected = m_bConnected;
dlg.m_strPort = m_strPort;
str.Format("%d",m_nBaud);
dlg.m_strBaud = str;
str.Format("%d",m_nDatabits);
dlg.m_strDatabits =str;
dlg.m_nParity = m_nParity;
dlg.m_nStopbits = m_nStopbits;
dlg.m_nFlowctrl = m_nFlowctrl;
if(dlg.DoModal()==IDOK)
{
m_strPort = dlg.m_strPort;
m_nBaud = atoi(dlg.m_strBaud);
m_nDatabits=atoi(dlg.m_strDatabits);
m_nParity=dlg.m_nParity;
m_nStopbits=dlg.m_nStopbits;
m_nFlowctrl=dlg.m_nFlowctrl;
if(m_bConnected)
if(!ConfigConnection())
AfxMessageBox("無法按指定地參數設置串口");
}
}
BOOL CRecorderConsoleDoc::ConfigConnection()
{
DCB dcb;
if(!GetCommState(m_hCom,&dcb))
return FALSE;
dcb.fBinary=TRUE;
dcb.BaudRate=m_nBaud;
dcb.ByteSize=m_nDatabits;
dcb.fParity=TRUE;
switch(m_nParity)
{
case 0:
dcb.Parity=NOPARITY;
break;
case 1:
dcb.Parity=EVENPARITY;
break;
case 2:
dcb.Parity=ODDPARITY;
break;
default:
break;
}
switch(m_nStopbits)
{
case 0:
dcb.StopBits=ONESTOPBIT;
break;
case 1:
dcb.StopBits=ONE5STOPBITS;
break;
case 2:
dcb.StopBits=TWOSTOPBITS;
break;
default:
break;
}
switch(m_nFlowctrl)
{
case 0:
dcb.fOutxCtsFlow=FALSE;
dcb.fRtsControl=FALSE;
dcb.fInX=dcb.fOutX=FALSE;
break;
case 1:
dcb.fOutxCtsFlow=TRUE;
dcb.fRtsControl=TRUE;
dcb.fInX=dcb.fOutX=FALSE;
break;
case 2:
dcb.fOutxCtsFlow=FALSE;
dcb.fRtsControl=FALSE;
dcb.fInX=dcb.fOutX=TRUE;
dcb.XonChar=XON;
dcb.XoffChar=XOFF;
dcb.XonLim=50;
dcb.XoffLim=50;
break;
default:
break;
}
return SetCommState(m_hCom,&dcb);
}
void CRecorderConsoleDoc::OnComConnect()
{
// TODO: Add your command handler code here
if(!OpenConnection())
AfxMessageBox("無法建立串口連接");
}
void CRecorderConsoleDoc::OnDisconnect()
{
// TODO: Add your command handler code here
if(!m_bConnected)
return;
m_bConnected=FALSE;
SetEvent(m_hPostMsgEvent);
SetCommMask(m_hCom,0);
WaitForSingleObject(m_pThread->m_hThread,INFINITE);
m_pThread=NULL;
CloseHandle(m_hCom);
}
BOOL CRecorderConsoleDoc::OpenConnection()
{
COMMTIMEOUTS TimeOuts;
POSITION firstViewPos;
CView * pView;
firstViewPos=GetFirstViewPosition();
pView=GetNextView(firstViewPos);
m_hVTWnd=pView->GetSafeHwnd();
//COMMCONFIG lpcc;
//CommConfigDialog("COM1",m_hVTWnd,&lpcc);
if(m_bConnected)
return FALSE;
m_hCom=CreateFile(m_strPort,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(m_hCom==INVALID_HANDLE_VALUE)
return FALSE;
SetupComm(m_hCom,MAXBLOCK,MAXBLOCK);
SetCommMask(m_hCom,EV_RXCHAR);
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
TimeOuts.WriteTotalTimeoutMultiplier=50;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(m_hCom,&TimeOuts);
if(ConfigConnection())
{
m_pThread=AfxBeginThread(CommProc,this,THREAD_PRIORITY_NORMAL,
0,CREATE_SUSPENDED,NULL);
if(m_pThread==NULL)
{
CloseHandle(m_hCom);
return FALSE;
}
else
{
m_bConnected=TRUE;
m_pThread->ResumeThread();
}
}
else
{
CloseHandle(m_hCom);
return FALSE;
}
return TRUE;
}
UINT CommProc(LPVOID pParam)
{
OVERLAPPED os;
DWORD dwMask,dwTrans;
COMSTAT ComStat;
DWORD dwErrorFlags;
CRecorderConsoleDoc *pDoc=(CRecorderConsoleDoc *)pParam;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(os.hEvent=NULL)
{
AfxMessageBox("無法創建對象");
return (UINT)-1;
}
while(pDoc->m_bConnected)
{
ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);
if(ComStat.cbInQue)
{
WaitForSingleObject(pDoc->m_hPostMsgEvent,INFINITE);
ResetEvent(pDoc->m_hPostMsgEvent);
PostMessage(pDoc->m_hVTWnd,WM_COMMNOTIFY,EV_RXCHAR,0);
continue;
}
dwMask=0;
if(!WaitCommEvent(pDoc->m_hCom,&dwMask,&os))
{
if(GetLastError()==ERROR_IO_PENDING)
GetOverlappedResult(pDoc->m_hCom,&os,&dwTrans,TRUE);
else
{
CloseHandle(os.hEvent);
return (UINT)-1;
}
}
}
CloseHandle(os.hEvent);
return 0;
}
DWORD CRecorderConsoleDoc::WriteComm(BYTE *pByte, int iLen)
{
BOOL fState;
COMSTAT ComStat;
DWORD length=(DWORD)iLen;
DWORD dwErrorFlags;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
fState=WriteFile(m_hCom,pByte,length,&length,&m_osWrite);
if(!fState)
{
if(GetLastError()==ERROR_IO_PENDING)
{
GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);
}
else
length=0;
}
return length;
}
DWORD CRecorderConsoleDoc::ReadComm(BYTE *pByte, int iLen)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
length=min((DWORD)iLen,ComStat.cbInQue);
ReadFile(m_hCom,pByte,length,&length,&m_osRead);
return length;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -