?? serialport.cpp
字號:
bResult = ClearCommError(port->m_hComm,&dwError,&comstat); //檢測并且清除錯誤條件
LeaveCriticalSection(&port->m_csCommunicationSync); //離開臨界區
//當緩沖區空時跳出循環
if(comstat.cbInQue==0)
{
break;
}
//進入臨界區
EnterCriticalSection(&port->m_csCommunicationSync);
if(bRead)
{
//讀串口
bResult = ReadFile(port->m_hComm, //串口號碼
&RXBuff, //接收地址
1, //每次讀一字節
&ByteRead, //返回實際讀取的字節數
&port->m_ov); //重疊結構
//處理錯誤
if(!bResult)
{
switch(dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
//正常返回值,讀未完成,調用
//GetOverlappedResults()調用下一步
bRead=FALSE;
break;
}
default:
{
//其他錯誤返回值顯示錯誤消息
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
//讀操作已完成,不必再調用GetOverlappedResult()
bRead = TRUE;
}
} //CLOSE IF (bRead)
if(!bRead)
{
bRead=TRUE;
//繼續完成讀操作
bResult = GetOverlappedResult(port->m_hComm, //串口句柄
&port->m_ov, //重疊結構
&BytesRead, //實際讀取字節數
TRUE); //等待標志
//錯誤處理
if(!bResult)
{
port->ProcessErrorMessage("GetOverlappedResult() in ReadFile()");
}
}//close if (!bRead)
//離開臨界區
LeaveCriticalSection(&port->m_csCommunicationSync);
//發送WM_COMM_RXCHAR消息到父窗口,通知數據到達
::SendMessage((port->m_pOwner)->m_hWnd,WM_COMM_RXCHAR,(WPARAM) RXBuff,
(LPARAM)port->m_nPortNr);
}//end forever loop
}
BYTE* CSerialPortEx::ReadBlock(CSerialPortEx* port,int& readlen)
{
COMSTAT comstat;
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
DWORD BytesToRead = readLen;
BYTE* pRec;
EnterCriticalSection(&port->m_csCommunicationSync); //進入緩沖區
bResult = ClearCommError(port->m_hComm,&dwError,&comstat); //檢測并且清除錯誤條件
LeaveCriticalSection(&port->m_csCommunicationSync); //離開臨界區
if(comstat.cbInQue == 0)
{
//若緩沖區空,則返回NULL
readLen=0;
return NULL;
}
else
{
//若緩沖區非空,則為返回數據開辟緩沖區
BytesToRead=BytesToRead+2>comstat.cbInQue?comstat.cbInQue:BytesToRead+2;
pRec=new BYTE[BytesToRead];
}
//進入臨界區
EnterCriticalSection(&port->m_csCommunicationSync);
if(bRead)
{
bResult=ReadFile(port->m_hComm, //串口句柄
pRec, //接收緩沖區地址
BytesToRead, // 希望讀取的字節數
&BytesRead, //實際讀取的字節數
&port->m_ov); //重疊結構
//錯誤處理
if(!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
//正常返回值,寫未完成,使用GetOverlappedResults()繼續下一步
bRead = FALSE;
break;
}
default:
{
//其他錯誤返回值顯示錯誤消息
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
//讀操作已完成,不必再調用GetOverlappedResults();
bRead=TRUE;
}
}//end if (bRead)
if(!bRead)
{
bRead=TRUE;
//
bResult = GetOverlappedResult(port->m_hComm, //串口句柄
&port->m_ov, //重疊結構
&BytesRead, //實際讀取字節數
TRUE); //等待標志
//錯誤處理
if(!bResult)
{
port->ProcessErrorMessage("GetOverlappedResult() in ReadFile()");
}
}//close if (!bRead)
//離開臨界區
LeaveCriticalSection(&port->m_csCommunicationSync);
readLen=BytesRead; //返回實際讀取的字節數
return pRec; //返回所接收數據地址
}
//寫串口
void CSerialPortEx::WriteChar(CSerialPortEx* port)
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;
DWORD BytesSent = 0;
ResetEvent(port->m_hWriteEvent);//使該事件回到無事件狀態
//
EnterCriticalSection(&port->m_csCommunicationSync);
if(bWrite)
{
//初始化OVERLAPPED結構
port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0;
//清空緩沖區
PurgeComm(port->m_hComm,PURGE_RXCLEAR|PURGE_TXCLEAR
|PURGE_RXABORT|PURGE_TXABORT);
//寫串口
bResult = WriteFile(port->m_hComm, //串口句柄
port->m_szWriteBuffer, //要發送的數據地址
port->m_nToSend, //要發送的數據長度
&BytesSent, //返回實際發送的數據長度
&port->m_ov); //重疊結構
//錯誤處理
if(!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
//正常返回值,寫未完成,使用GetOverlappedResults()繼續下一步
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
//其他錯誤返回值顯示錯誤消息
port->ProcessErrorMessage("WriteFile()");
break;
}
}
}
else
{
//讀操作已完成,則離開臨界區
LeaveCriticalSection(port->m_csCommunicationSync);
}
}//end if (bRead)
if(!bWrite)
{
bWrite = TRUE;
//
bResult = GetOverlappedResult(port->m_hComm, //串口句柄
&port->m_ov, //重疊結構
&BytesSent, //已發送字節數
TRUE); //等待標志
//離開臨界區
LeaveCriticalSection(&port->m_csCommunicationSync);
//錯誤處理
if(!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
}//close if (!bWrite)
}
//將一塊內存區中的數據通過串口發送
void CSerialPortEx::WriteToPort(BYTE* string,int nLength)
{
if(m_hComm == 0)
{
::AfxMessageBox("error strange");
return;
}
//將待發送數據復制到CSerialPortEx發送到緩沖區中
memset(m_szWriteBuffer,0,m_nWriteBufferSize);
memcpy(m_szWriteBuffer,string,nLength);
m_nToSend=nLength;
//觸發寫事件
SetEvent(m_hWriteEvent);
}
//將CString類型字符串寫入串口
void CSerialPortEx::WriteToPort(CString str)
{
char *buffer=new char[m_nWriteBufferSize];
memset(buffer,0,m_nWriteBufferSize);
int nMaxLength=(str.GetLength()+1>m_nWriteBufferSize)?str.GetLength()+1:m_nWriteBufferSize;
char *temp=str.GetBuffer(nMaxLength);
memcpy(buffer,temp,str.GetLength());
str.ReleaseBuffer();
WriteToPort((BYTE*)buffer,str.GetLength());
}
//獲取并顯示以指定格式顯示錯誤信息
void CSerialPortEx::ProcessErrorMessage(char* ErrorText)
{
char *Temp = new char[200];
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),//default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
sprintf(Temp,"WARNING: %s Failed with the following error:\n%s\nPort:%d\n",
(char*)ErrorText,lpMsgBuf,m_nPortNr);
MessageBox(NULL,Temp,"Application Error",MB_ICONSTOP);
LocalFree(lpMsgBuf);
delete[] Temp;
}
//返回設備控制塊
DCB CSerialPortEx::GetDCB()
{
return m_dcb;
}
//返回串口狀態事件
DWORD CSerialPortEx::GetCommEvents()
{
return m_dwCommEvents;
}
//返回輸出緩沖區大小
DWORD CSerialPortEx::GetWriteBufferSize()
{
return m_nWriteBufferSize;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -