void CSerialDataFrameDecoder::run()
{
int bytesToRead = -1;
Q_LONG bytesRead = 0;
int startSequenceIndex = -1;
int stopSequenceIndex = -1;
while( bMustDecode == true )
{
qDebug("In CSerialDataFrameDecoder. After : while( bMustDecode == true )");
// TO DO
// ... read the com port buffer
// ... decode frames
// ... trigger a custom events each time a complete frame is decoded
if( comPort != 0 )
{
qDebug("In CSerialDataFrameDecoder::run(). After : if( comPort != 0 )");
if( comPort->isOpen() )
{
qDebug("In CSerialDataFrameDecoder::run(). After : if( comPort->isOpen() )");
// Get the number of bytes to read
bytesToRead = comPort->bytesWaiting();
qDebug("In CSerialDataFrameDecoder::run(). After : bytesToRead = comPort->bytesWaiting();");
if( bytesToRead != 0 )
{
// Here we can do some stuff
char* buffData = 0;
buffData = new char[bytesToRead+1];
qDebug("In CSerialDataFrameDecoder::run(). After : buffData = new char[bytesToRead+1];");
bytesRead = comPort->readBlock(buffData, bytesToRead);
qDebug("In CSerialDataFrameDecoder::run(). After : bytesRead = comPort->readBlock(buffData, bytesToRead);");
buffData[bytesRead] = '\0';
comPort->flush();
qDebug("In CSerialDataFrameDecoder::run(). After : comPort->flush();");
buffer = buffData;
while( (buffer.length() != 0) && (bMustDecode == true) )
{
qDebug("In CSerialDataFrameDecoder::run(). After : while( (buffer.length() != 0) && (bMustDecode == true) )");
startSequenceIndex = buffer.find(startSequence, 0, false);
stopSequenceIndex = buffer.find(stopSequence, 0, false);
qDebug("In CSerialDataFrameDecoder::run(). startSequenceIndex=%d, stopSequenceIndex=%d", startSequenceIndex, stopSequenceIndex);
// Check previous indexes to determine if the frame is complete or not
// CASE 1 : start and stop indexex are found, stop index is greather than start index
// !!! A complete frame can be extracted !!!
// CASE 2 and CASE 5 : stop index is found but not start index
// !!! An incomplete frame can be extracted, its beginning is missing !!!
// CASE 3 : start index is found but not stop
// !!! An incomplete frame can be extracted, its ending is missing
// CASE 4 : start index neither stop are found
// !!! An incomplete frame can be extracted, its beginning and ending are missing
// CASE 1
if( (startSequenceIndex == 0) && (stopSequenceIndex != -1) && (stopSequenceIndex>startSequenceIndex) )
{
qDebug("In CSerialDataFrameDecoder::run(). Entry : CASE1");
frame = buffer.mid(startSequenceIndex, (stopSequenceIndex-startSequenceIndex)+1);
buffer.remove(0, stopSequenceIndex+stopSequence.length());
CSerialDataFrame aFrame;
aFrame.setSerialDataFrameValue(frame);
aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusComplete);
qDebug("In CSerialDataFrameDecoder::run(). Before : this->serialDataFrameDecoded(aFrame);");
this->serialDataFrameDecoded(aFrame);
qDebug("In CSerialDataFrameDecoder::run(). After : this->serialDataFrameDecoded(aFrame);");
}
// CASE 2
else if( (startSequenceIndex == -1) && (stopSequenceIndex != -1) )
{
qDebug("In CSerialDataFrameDecoder::run(). Entry : CASE2");
frame = buffer.left(stopSequenceIndex+1);
buffer.remove(0, stopSequenceIndex+stopSequence.length());
CSerialDataFrame aFrame;
aFrame.setSerialDataFrameValue(frame);
aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheEndingPart);
qDebug("In CSerialDataFrameDecoder::run(). Before : this->serialDataFrameDecoded(aFrame);");
this->serialDataFrameDecoded(aFrame);
qDebug("In CSerialDataFrameDecoder::run(). After : this->serialDataFrameDecoded(aFrame);");
}
// CASE 3
else if( (startSequenceIndex != -1) && (stopSequenceIndex == -1) )
{
qDebug("In CSerialDataFrameDecoder::run(). Entry : CASE3");
frame = buffer.mid(0, buffer.length());
buffer.remove(0, buffer.length());
CSerialDataFrame aFrame;
aFrame.setSerialDataFrameValue(frame);
aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheBeginningPart);
qDebug("In CSerialDataFrameDecoder::run(). Before : this->serialDataFrameDecoded(aFrame);");
this->serialDataFrameDecoded(aFrame);
qDebug("In CSerialDataFrameDecoder::run(). After : this->serialDataFrameDecoded(aFrame);");
}
// CASE 4
else if( (startSequenceIndex == -1) && (stopSequenceIndex == -1) )
{
qDebug("In CSerialDataFrameDecoder::run(). Entry : CASE4");
frame = buffer;
buffer.remove(frame);
CSerialDataFrame aFrame;
aFrame.setSerialDataFrameValue(frame);
aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheMiddlePart);
qDebug("In CSerialDataFrameDecoder::run(). Before : this->serialDataFrameDecoded(aFrame);");
this->serialDataFrameDecoded(aFrame);
qDebug("In CSerialDataFrameDecoder::run(). After this->serialDataFrameDecoded(aFrame);");
}
// CASE 5
else if( (startSequenceIndex != -1) && (stopSequenceIndex != -1) && (stopSequenceIndex<startSequenceIndex) )
{
qDebug("In CSerialDataFrameDecoder::run(). Entry : CASE5");
frame = buffer.left(stopSequenceIndex+1);
buffer.remove(0, stopSequenceIndex+stopSequence.length());
CSerialDataFrame aFrame;
aFrame.setSerialDataFrameValue(frame);
aFrame.setSerialDataFrameStatus(CSerialDataFrame::SerialDataFrameStatusIncompleteHasJustTheEndingPart);
qDebug("In CSerialDataFrameDecoder::run(). Before : this->serialDataFrameDecoded(aFrame);");
this->serialDataFrameDecoded(aFrame);
qDebug("In CSerialDataFrameDecoder::run(). After : this->serialDataFrameDecoded(aFrame);");
}
}
}
}
}
}
}
void CSerialDataFrameDecoder::serialDataFrameDecoded(const CSerialDataFrame& frame)
{
// Allocate some memory for the data
CSerialDataFrame* data = 0;
data = new CSerialDataFrame(frame);
// The allocated memory for the data MUST by freed by the receiver
// Allocate some memory for the custom event
QCustomEvent* ce = 0;
ce = new QCustomEvent(1002);
ce->setData(data);
// Send the event
qDebug("In CSerialDataFrameDecoder::serialDataFrameDecoded. Before : QApplication::postEvent(objectOwner, ce);");
qDebug("In CSerialDataFrameDecoder::serialDataFrameDecoded. After : QApplication::postEvent(objectOwner, ce);");
// The allocated memory for the custum event is freed by Qt so the receiver neither the sender MUST NOT free it
}
Bookmarks