📄 comwin32.cpp
字号:
/************************************************************************
* Function name : ComWin32::RetrieveRxData
* Description : Retrieve data from the internal circular buffer.
* :
* Parameters : buffer - Destination.
* : size - Size of destination buffer.
* Returns : Number of bytes retrieved.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 03Sep99 RCS Created.
************************************************************************/
int ComWin32::RetrieveRxData(char *buffer, int size)
{
return circular->PullIt(buffer, size);
} // ComWin32::RetrieveData()
/************************************************************************
* Function name : ComWin32::AdvanceTo
* Description : Advance the pull pointer of the circular buffer to
* : the specified character.
* :
* Parameters : expChar - Expected character.
* Returns : TRUE - Character found.
* : FALSE - Character not found.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 03Sep99 RCS Created.
************************************************************************/
BOOL ComWin32::AdvanceTo(char expChar)
{
return circular->AdvanceTo(expChar);
} // ComWin32::AdvanceTo()
/************************************************************************
* Function name : ComWin32::PacketSize
* Description : Calculate size of a packet. This function will also
* : move the pull pointer of the cicrular buffer to the
* : start character specified, if found.
* :
* Parameters : startChar - Define the start character of a packet.
* : endChar - Define the end character of a packet.
* Returns : -1 - Start character is not found.
* : 0 - End character not found.
* : Others - Size of the packet.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 03Sep99 RCS Created.
************************************************************************/
int ComWin32::PacketSize(char startChar, char endChar)
{
int size = -1;
if (circular->AdvanceTo(startChar))
size = circular->SearchForChar(endChar);
return size;
} // ComWin32::packetSize()
/************************************************************************
* Function name : ComWin32::PacketSize
* Description : Calculate size of a packet from the current pull
* : position.
* :
* Parameters : endChar - Define the end character of a packet.
* Returns : -1 - Start character is not found.
* : 0 - End character not found.
* : Others - Size of the packet.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 03Sep99 RCS Created.
************************************************************************/
int ComWin32::PacketSize(char endChar)
{
return circular->SearchForChar(endChar);
} // ComWin32
/************************************************************************
* Function name : ComWin32::AvailRxBufData
* Description : Get the number of bytes of data in the circular buffer.
* :
* Parameters : -
* Returns : Bytes available.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 05Jul01 RCS Created.
************************************************************************/
int ComWin32::AvailRxBufData(void)
{
return circular->DataAvailable();
} // ComWin32::AvailRxBufData()
/************************************************************************
* Function name : ComWin32::FlushRxBufData
* Description : Flush the circular buffer.
* :
* Parameters : -
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 05Jul01 RCS Created.
************************************************************************/
void ComWin32::FlushRxBufData(void)
{
circular->Flush();
} // ComWin32::FlushRxBufData()
/************************************************************************
* Function name : ComWin32::WaitForRxComplete
* Description : Wait for Tx buffer sent.
* :
* Parameters : timeOut - Timeout.
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 04May03 RCS Created.
************************************************************************/
void ComWin32::WaitForTxComplete(DWORD timeOut)
{
DWORD oldMask;
DWORD event = EV_TXEMPTY;
if (GetEventMask(oldMask) == NO_ERROR)
{
SetEventMask(EV_TXEMPTY);
WaitForEvent(event, timeOut);
SetEventMask(oldMask);
} /* end of if */
return;
} // ComWin32::WaitForTxComplete()
/************************************************************************
* Function name : ComWin32::Circular::Circular
* Description : Constructor of class.
* :
* Parameters : size - Size of the circular buffer.
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
ComWin32::Circular::Circular(int size)
{
cirBufSize = size;
dataBuffer = new char [size + 1];
availData = 0;
bottom = dataBuffer;
top = dataBuffer + size + 1;
objValid = (dataBuffer != 0) ? TRUE : FALSE;
Flush();
} // ComWin32::Circular::Circular()
/************************************************************************
* Function name : ComWin32::Circular::~Circular
* Description : Destructor of class.
* :
* Parameters : -
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
ComWin32::Circular::~Circular(void)
{
if (dataBuffer != 0)
delete [] dataBuffer;
return;
} // ComWin32::Circular::~Circular()
/************************************************************************
* Function name : ComWin32::Circular::Flush
* Description : Flush the buffer.
* :
* Parameters : -
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
void ComWin32::Circular::Flush(void)
{
push = bottom;
pull = bottom;
availData = 0;
} // ComWin32::Circular::Flush()
/************************************************************************
* Function name : ComWin32::Circular::PushIt
* Description : Store data into the circular buffer.
* :
* Parameters : data - Data to store.
* : size - Size of data.
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
void ComWin32::Circular::PushIt(char *data, int size)
{
if (Good())
{
for (int i = 0; i < size; i++)
PushIt(*(data + i));
} /* end of if */
return;
} // ComWin32::Circular::PushIt()
/************************************************************************
* Function name : ComWin32::Circular::PushIt
* Description : Store one byte of data.
* :
* Parameters : data -
* Returns : -
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
void ComWin32::Circular::PushIt(char data)
{
if (!Good())
return;
*push = data;
if (push + 1 == top)
push = bottom; // End of buffer go to bottom
else
push++;
availData++;
} // ComWin32::Circular::PushIt()
/************************************************************************
* Function name : ComWin32::Circular::PullIt
* Description : Retrieve stored data.
* :
* Parameters : data - Buffer to retrieve data to.
* : size - Buffer size.
* Returns : Number of data retrieved.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
int ComWin32::Circular::PullIt(char *data, int size)
{
if (!Good())
return 0;
int count;
for (count = 0; count < size; count++)
{
if (PullIt(data + count) == FALSE)
break;
} /* end of for */
availData -= count;
if (availData < 0)
availData = 0;
return count;
} // ComWin32::Circular::PullIt()
/************************************************************************
* Function name : ComWin32::Circular::PullIt
* Description : Retrieve one byte of data.
* :
* Parameters : data -
* Returns : TRUE - Successful.
* : FALSE - No data.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
BOOL ComWin32::Circular::PullIt(char *data)
{
if (!Good() || pull == push)
return FALSE; // No data
*data = *pull;
if (pull + 1 == top) // Get next char ready
pull = bottom;
else
pull++;
availData--;
return TRUE;
} // ComWin32::Circular::PullIt()
/************************************************************************
* Function name : ComWin32::Circular::AdvanceTo
* Description : Search for the specified character.
* :
* Parameters : expChar - Expected character.
* Returns : TRUE - Character found.
* : FALSE - Character not found.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
BOOL ComWin32::Circular::AdvanceTo(char expChar)
{
BOOL found = FALSE;
while (Good() && DataAvailable() && found == FALSE)
{
if (*pull != expChar)
{
if (pull + 1 == top) // Get next char ready
pull = bottom;
else
pull++;
availData--;
} /* end of if */
else
found = TRUE;
} /* end of while */
return found;
} // ComWin32::Circular::AdvanceTo()
/************************************************************************
* Function name : ComWin32::Circular::SearchForChar
* Description : Search for a specified character.
* :
* Parameters : expChar - Character to search.
* Returns : > 0 - Number of bytes from the current pull
* : location to the search character.
* : 0 - Not found.
* Author : Richard Shen
* ----------------------------------------------------------------------
* Date By Description
* ----------------------------------------------------------------------
* 13Aug99 RCS Created.
************************************************************************/
int ComWin32::Circular::SearchForChar(char expChar)
{
char *ptr = pull;
char *start = pull;
int count;
for (count = 0; count < DataAvailable(); count++)
{
if (*ptr == expChar)
{
count++;
break;
} /* end of if */
if (ptr + 1 == top) // Get next char ready
ptr = bottom;
else
ptr++;
} /* end of for */
if (ptr == start)
count = 0; // Not found
return count;
} // ComWin32::Circular::SearchForChar()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -