📄 chap12_4.htm
字号:
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY">else</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">CloseHandle(m_hCom);</p>
<p ALIGN="JUSTIFY">return FALSE;</p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY">return TRUE;</p>
<p ALIGN="JUSTIFY">}</b></p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p></font><b><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">结束工作者线程,关闭串行口</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">void CTerminalDoc::CloseConnection()</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">if(!m_bConnected) return;</p>
<p ALIGN="JUSTIFY">m_bConnected=FALSE;</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">//</font><font SIZE="3">结束</font><font FACE="Times New Roman" SIZE="3">CommProc</font><font SIZE="3">线程中</font><font FACE="Times New Roman" SIZE="3">WaitSingleObject</font><font SIZE="3">函数的等待</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">SetEvent(m_hPostMsgEvent); </p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">//</font><font SIZE="3">结束</font><font FACE="Times New Roman" SIZE="3">CommProc</font><font SIZE="3">线程中</font><font FACE="Times New Roman" SIZE="3">WaitCommEvent</font><font SIZE="3">的等待</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">SetCommMask(m_hCom, 0); </p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">//</font><font SIZE="3">等待辅助线程终止</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">WaitForSingleObject(m_pThread->m_hThread, INFINITE);</p>
<p ALIGN="JUSTIFY">m_pThread=NULL;</p>
<p ALIGN="JUSTIFY">CloseHandle(m_hCom);</p>
<p ALIGN="JUSTIFY">}</b></p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">让用户设置串行口</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">void CTerminalDoc::OnFileSettings() </p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">// TODO: Add your command handler code here</p>
<p></font><b><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">CSetupDlg dlg;</p>
<p ALIGN="JUSTIFY">CString str;</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">dlg.m_bConnected=m_bConnected;</p>
<p ALIGN="JUSTIFY">dlg.m_sPort=m_sPort;</p>
<p ALIGN="JUSTIFY">str.Format("%d",m_nBaud);</p>
<p ALIGN="JUSTIFY">dlg.m_sBaud=str;</p>
<p ALIGN="JUSTIFY">str.Format("%d",m_nDataBits);</p>
<p ALIGN="JUSTIFY">dlg.m_sDataBits=str;</p>
<p ALIGN="JUSTIFY">dlg.m_nParity=m_nParity;</p>
<p ALIGN="JUSTIFY">dlg.m_nStopBits=m_nStopBits;</p>
<p ALIGN="JUSTIFY">dlg.m_nFlowCtrl=m_nFlowCtrl;</p>
<p ALIGN="JUSTIFY">dlg.m_bEcho=m_bEcho;</p>
<p ALIGN="JUSTIFY">dlg.m_bNewLine=m_bNewLine;</p>
<p ALIGN="JUSTIFY">if(dlg.DoModal()==IDOK)</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">m_sPort=dlg.m_sPort;</p>
<p ALIGN="JUSTIFY">m_nBaud=atoi(dlg.m_sBaud);</p>
<p ALIGN="JUSTIFY">m_nDataBits=atoi(dlg.m_sDataBits);</p>
<p ALIGN="JUSTIFY">m_nParity=dlg.m_nParity;</p>
<p ALIGN="JUSTIFY">m_nStopBits=dlg.m_nStopBits;</p>
<p ALIGN="JUSTIFY">m_nFlowCtrl=dlg.m_nFlowCtrl;</p>
<p ALIGN="JUSTIFY">m_bEcho=dlg.m_bEcho;</p>
<p ALIGN="JUSTIFY">m_bNewLine=dlg.m_bNewLine;</p>
<p ALIGN="JUSTIFY">if(m_bConnected)</p>
<p ALIGN="JUSTIFY">if(!ConfigConnection())</p>
<p ALIGN="JUSTIFY">AfxMessageBox("Can't realize the settings!");</p>
<p ALIGN="JUSTIFY">}</b></p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p></font><b><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">配置串行口</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">BOOL CTerminalDoc::ConfigConnection()</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">DCB dcb;</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">if(!GetCommState(m_hCom, &dcb))</p>
<p ALIGN="JUSTIFY">return FALSE;</p>
<p ALIGN="JUSTIFY">dcb.fBinary=TRUE;</p>
<p ALIGN="JUSTIFY">dcb.BaudRate=m_nBaud; // </font><font SIZE="3">波特率</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">dcb.ByteSize=m_nDataBits; // </font><font SIZE="3">每字节位数</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">dcb.fParity=TRUE;</p>
<p ALIGN="JUSTIFY">switch(m_nParity) // </font><font SIZE="3">校验设置</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">case 0: dcb.Parity=NOPARITY;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">case 1: dcb.Parity=EVENPARITY;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">case 2: dcb.Parity=ODDPARITY;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">default:;</p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY">switch(m_nStopBits) // </font><font SIZE="3">停止位</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">case 0: dcb.StopBits=ONESTOPBIT;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">case 1: dcb.StopBits=ONE5STOPBITS;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">case 2: dcb.StopBits=TWOSTOPBITS;</p>
<p ALIGN="JUSTIFY">break;</p>
<p ALIGN="JUSTIFY">default:;</p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">硬件流控制设置</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">dcb.fOutxCtsFlow=m_nFlowCtrl==1;</p>
<p ALIGN="JUSTIFY">dcb.fRtsControl=m_nFlowCtrl==1?</p>
<p ALIGN="JUSTIFY">RTS_CONTROL_HANDSHAKE:RTS_CONTROL_ENABLE;</p>
<p ALIGN="JUSTIFY">// XON/XOFF</font><font SIZE="3">流控制设置</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">dcb.fInX=dcb.fOutX=m_nFlowCtrl==2;</p>
<p ALIGN="JUSTIFY">dcb.XonChar=XON;</p>
<p ALIGN="JUSTIFY">dcb.XoffChar=XOFF;</p>
<p ALIGN="JUSTIFY">dcb.XonLim=50;</p>
<p ALIGN="JUSTIFY">dcb.XoffLim=50;</p>
<p ALIGN="JUSTIFY">return SetCommState(m_hCom, &dcb);</p>
<p ALIGN="JUSTIFY">}</b></p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p></font><b><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">从串行口输入缓冲区中读入指定数量的字符</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">DWORD CTerminalDoc::ReadComm(char *buf,DWORD dwLength)</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">DWORD length=0;</p>
<p ALIGN="JUSTIFY">COMSTAT ComStat;</p>
<p ALIGN="JUSTIFY">DWORD dwErrorFlags;</p>
<p ALIGN="JUSTIFY">ClearCommError(m_hCom,&dwErrorFlags,&ComStat);</p>
<p ALIGN="JUSTIFY">length=min(dwLength, ComStat.cbInQue);</p>
<p ALIGN="JUSTIFY">ReadFile(m_hCom,buf,length,&length,&m_osRead);</p>
<p ALIGN="JUSTIFY">return length;</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">// </font><font SIZE="3">将指定数量的字符从串行口输出</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">DWORD CTerminalDoc::WriteComm(char *buf,DWORD dwLength)</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">BOOL fState;</p>
<p ALIGN="JUSTIFY">DWORD length=dwLength;</p>
<p ALIGN="JUSTIFY">COMSTAT ComStat;</p>
<p ALIGN="JUSTIFY">DWORD dwErrorFlags;</p>
<p ALIGN="JUSTIFY">ClearCommError(m_hCom,&dwErrorFlags,&ComStat);</p>
<p ALIGN="JUSTIFY">fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite);</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -