?? robotdoc.cpp
字號:
// robotDoc.cpp : implementation of the CRobotDoc class
//
#include "stdafx.h"
#include "robot.h"
#include"SetupDlg.h"
#include "robotDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRobotDoc
IMPLEMENT_DYNCREATE(CRobotDoc, CDocument)
BEGIN_MESSAGE_MAP(CRobotDoc, CDocument)
//{{AFX_MSG_MAP(CRobotDoc)
ON_COMMAND(ID_SETUPCOMM, OnSetupcomm)
ON_COMMAND(ID_CLOSECOMM, OnClosecomm)
ON_COMMAND(ID_OPENCOMM, OnOpencomm)
ON_UPDATE_COMMAND_UI(ID_OPENCOMM, OnUpdateOpencomm)
ON_UPDATE_COMMAND_UI(ID_CLOSECOMM, OnUpdateClosecomm)
ON_COMMAND(ID_ANJIANCTRL, OnAnjianctrl)
ON_UPDATE_COMMAND_UI(ID_ANJIANCTRL, OnUpdateAnjianctrl)
ON_COMMAND(ID_VOICECTRL, OnVoicectrl)
ON_UPDATE_COMMAND_UI(ID_VOICECTRL, OnUpdateVoicectrl)
ON_COMMAND(ID_HELP, OnHelp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRobotDoc construction/destruction
CRobotDoc::CRobotDoc()
{
// TODO: add one-time construction code here
m_bConnected=FALSE;
m_pThread=NULL;
m_nBaud =244;
m_nDataBits = 8;
m_nParity = 1;
m_sPort = "COM1";
m_nStopBits = 0;
m_bctrlmodal=FALSE;
m_uCurrentBtn=ID_CLOSECOMM;
}
CRobotDoc::~CRobotDoc()
{
if(m_bConnected)
CloseConnection();
// 刪除事件句柄
if(m_hPostMsgEvent)
CloseHandle(m_hPostMsgEvent);
if(m_osRead.hEvent)
CloseHandle(m_osRead.hEvent);
if(m_osWrite.hEvent)
CloseHandle(m_osWrite.hEvent);
}
BOOL CRobotDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
// 為WM_COMMNOTIFY消息創建事件對象,手工重置,初始化為有信號的
if((m_hPostMsgEvent=CreateEvent(NULL, TRUE, TRUE, NULL))==NULL)
return FALSE;
memset(&m_osRead, 0, sizeof(OVERLAPPED));
memset(&m_osWrite, 0, sizeof(OVERLAPPED));
// 為重疊讀創建事件對象,手工重置,初始化為無信號的
if((m_osRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
return FALSE;
// 為重疊寫創建事件對象,手工重置,初始化為無信號的
if((m_osWrite.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CRobotDoc serialization
void CRobotDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CRobotDoc diagnostics
#ifdef _DEBUG
void CRobotDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CRobotDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRobotDoc commands
void CRobotDoc::OnSetupcomm()
{
// TODO: Add your command handler code here
CSetupDlg dlg;
CString str;
dlg.m_bConnected=m_bConnected;
dlg.m_sPort=m_sPort;
str.Format("%d",m_nBaud);
dlg.m_sBaud=str;
str.Format("%d",m_nDataBits);
dlg.m_sDataBits=str;
dlg.m_nParity=m_nParity;
dlg.m_nStopBits=m_nStopBits;
//dlg.m_nFlowCtrl=m_nFlowCtrl;
//dlg.m_bEcho=m_bEcho;
// dlg.m_bNewLine=m_bNewLine;
if(dlg.DoModal()==IDOK)
{
m_sPort=dlg.m_sPort;
m_nBaud=atoi(dlg.m_sBaud);
m_nDataBits=atoi(dlg.m_sDataBits);
m_nParity=dlg.m_nParity;
m_nStopBits=dlg.m_nStopBits;
// m_nFlowCtrl=dlg.m_nFlowCtrl;
// m_bEcho=dlg.m_bEcho;
// m_bNewLine=dlg.m_bNewLine;
if(m_bConnected)
if(!ConfigConnection())
AfxMessageBox("Can't realize the settings!");
}
}
void CRobotDoc::OnClosecomm()
{
// TODO: Add your command handler code here
m_uCurrentBtn=ID_CLOSECOMM;
CloseConnection();
}
void CRobotDoc::OnOpencomm()
{
// TODO: Add your command handler code here
m_uCurrentBtn=ID_OPENCOMM;
if(!OpenConnection())
AfxMessageBox("Can't open connection");
}
void CRobotDoc::OnUpdateOpencomm(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//pCmdUI->Enable(!m_bConnected);
pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);
}
void CRobotDoc::OnUpdateClosecomm(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
// pCmdUI->Enable(m_bConnected);
pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);
}
void CRobotDoc::OnAnjianctrl()
{
// TODO: Add your command handler code here
}
void CRobotDoc::OnUpdateAnjianctrl(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CRobotDoc::OnVoicectrl()
{
// TODO: Add your command handler code here
// WinExec(NULL,NULL,_T("dutty.exe"),NULL,_T("D:\\Program Files\\Dutty\\Dutty.exe"),NULL);
WinExec(_T("D:\\Program Files\\Dutty\\Dutty.exe"),SW_SHOW);
m_bctrlmodal=TRUE;
//STARTUPINFO stinfo; //啟動窗口的信息
//PROCESSINFO procinfo; //進程的信息
//CreateProcess(NULL,_T("dutty.exe"),NULL,NULL.FALSE, NORMAL_PRIORITY_ CLASS,NULL,NULL, &stinfo,&procinfo);
}
void CRobotDoc::OnUpdateVoicectrl(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_bctrlmodal);
}
UINT CommProc(LPVOID pParam)
{
OVERLAPPED os;
DWORD dwMask, dwTrans;
COMSTAT ComStat;
DWORD dwErrorFlags;
CRobotDoc *pDoc=(CRobotDoc*)pParam;
memset(&os, 0, sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if(os.hEvent==NULL)
{
AfxMessageBox("Can't create event object!");
return (UINT)-1;
}
while(pDoc->m_bConnected)
{
ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);
if(ComStat.cbInQue) //查詢輸入緩沖區中是否有字符 ,若有
{
// 等待WM_COMMNOTIFY消息被處理完
WaitForSingleObject(pDoc->m_hPostMsgEvent, INFINITE);
ResetEvent(pDoc->m_hPostMsgEvent);
PostMessage(pDoc->m_hTermWnd, 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;
}
BOOL CRobotDoc::OpenConnection()
{
COMMTIMEOUTS TimeOuts;
POSITION firstViewPos;
CView *pView;
firstViewPos=GetFirstViewPosition();
pView=GetNextView(firstViewPos);
m_hTermWnd=pView->GetSafeHwnd();
if(m_bConnected)
return FALSE;
m_hCom=CreateFile(m_sPort, GENERIC_READ|GENERIC_WRITE,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL); // 重疊方式
if(m_hCom==INVALID_HANDLE_VALUE)
//AfxMessageBox("dfhksdjfhsjkfhks",MB_OK);
return FALSE;
SetupComm(m_hCom,1024,1024);
SetCommMask(m_hCom, EV_RXCHAR);///////////////////!!!!!!!!!!!
// 把間隔超時設為最大,把總超時設為0將導致ReadFile立即返回并完成操作
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
/* 設置寫超時以指定WriteComm成員函數中的
GetOverlappedResult函數的等待時間*/
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;
}
void CRobotDoc::CloseConnection()
{
if(!m_bConnected) return;
m_bConnected=FALSE;
//結束CommProc線程中WaitSingleObject函數的等待
SetEvent(m_hPostMsgEvent);
//結束CommProc線程中WaitCommEvent的等待
SetCommMask(m_hCom, 0);
//等待輔助線程終止
WaitForSingleObject(m_pThread->m_hThread, INFINITE);
m_pThread=NULL;
CloseHandle(m_hCom);
}
BOOL CRobotDoc::ConfigConnection()///設置DCB
{
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:;
}
switch(m_nStopBits) // 停止位
{
case 0: dcb.StopBits=ONESTOPBIT;
break;
case 1: dcb.StopBits=ONE5STOPBITS;
break;
case 2: dcb.StopBits=TWOSTOPBITS;
break;
default:;
}
// 硬件流控制設置
// dcb.fOutxCtsFlow=m_nFlowCtrl==1;
// dcb.fRtsControl=m_nFlowCtrl==1?RTS_CONTROL_HANDSHAKE:RTS_CONTROL_ENABLE;
// XON/XOFF流控制設置
// dcb.fInX=dcb.fOutX=m_nFlowCtrl==2;
// dcb.XonChar=XON;
// dcb.XoffChar=XOFF;
// dcb.XonLim=50;
// dcb.XoffLim=50;
return SetCommState(m_hCom, &dcb);
}
DWORD CRobotDoc::ReadComm(unsigned char *buf,DWORD dwLength)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除錯誤標志
length=min(dwLength, ComStat.cbInQue);
ReadFile(m_hCom,buf,length,&length,&m_osRead);// 將指定數量的字符從串行口輸出
return length;
}
// 將指定數量的字符從串行口輸出
DWORD CRobotDoc::WriteComm(unsigned char *buf,DWORD dwLength)
{
BOOL fState;
DWORD length=dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat); // 清除錯誤標志
fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite);
if(!fState){
if(GetLastError()==ERROR_IO_PENDING)
{
GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);// 等待
}
else
length=0;
}
return length;
}
// 工作者線程,負責監視串行口
void CRobotDoc::OnHelp()
{
// TODO: Add your command handler code here
//ShellExecute(NULL,NULL,_T("Jqrhelp.chm"),NULL,_T("e:\\機器人"),NULL);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -