Found a bug where complete frames being removed from the buffer were being shorted by 1 character
Committing now with debug comments for future reference
This commit is contained in:
+21
-21
@@ -32,6 +32,7 @@ SocketCANd::SocketCANd(QString portName) :
|
|||||||
for (int i = 0; i < mNumBuses; i++)
|
for (int i = 0; i < mNumBuses; i++)
|
||||||
{
|
{
|
||||||
rx_state.append(IDLE);
|
rx_state.append(IDLE);
|
||||||
|
unprocessedData.append("");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -251,28 +252,23 @@ void SocketCANd::switchToRawMode(int busNum)
|
|||||||
|
|
||||||
QString SocketCANd::decodeFrames(QString data, int busNum)
|
QString SocketCANd::decodeFrames(QString data, int busNum)
|
||||||
{
|
{
|
||||||
if (data.indexOf("< frame ") == -1)
|
|
||||||
{
|
|
||||||
//qDebug() << "Received datagramm doesn't contain any frame: " << data;
|
|
||||||
if (data.indexOf("<") == -1)
|
if (data.indexOf("<") == -1)
|
||||||
return "";
|
return "";
|
||||||
else
|
else if(data.length() >= 8 && data.indexOf("< frame ") == -1)
|
||||||
return data;
|
return "";
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int firstIndex = data.indexOf("< frame ");
|
int firstIndex = data.indexOf("< frame ");
|
||||||
if(firstIndex > 0)
|
if(firstIndex > 0)
|
||||||
{
|
{
|
||||||
QString framePartial = data.left(firstIndex);
|
QString framePartial = data.left(firstIndex);
|
||||||
qDebug() << "Received datagramm that starts with fragment (missing '< frame'), this should only occur on startup, removing...: " << framePartial;
|
qDebug() << "Received datagramm that starts with fragment (missing '< frame'), this should only occur on startup, removing...: " << framePartial;
|
||||||
}
|
}
|
||||||
QString framePart = data.mid(firstIndex, data.length()); //remove starting beginning of payload if not < frame >
|
QString framePart = data.mid(firstIndex); //remove starting beginning of payload if not < frame >
|
||||||
const QString frameStrConst = framePart.left(framePart.indexOf(">")+1);
|
const QString frameStrConst = framePart.left(framePart.indexOf(">")+1);
|
||||||
QString frameStr = frameStrConst;
|
QString frameStr = frameStrConst;
|
||||||
QStringList frameParsed = (frameStr.remove(QRegExp("^<")).remove(QRegExp(">$"))).simplified().split(' ');
|
QStringList frameParsed = (frameStr.remove(QRegExp("^<")).remove(QRegExp(">$"))).simplified().split(' ');
|
||||||
|
|
||||||
if(frameParsed.length() < 2)
|
if(frameParsed.length() < 3)
|
||||||
{
|
{
|
||||||
//qDebug() << "Received datagramm is an incomplete frame: " << data;
|
//qDebug() << "Received datagramm is an incomplete frame: " << data;
|
||||||
|
|
||||||
@@ -332,11 +328,12 @@ QString SocketCANd::decodeFrames(QString data, int busNum)
|
|||||||
//take out the data that we just processed and anything that is in front of it
|
//take out the data that we just processed and anything that is in front of it
|
||||||
//this should keep broken frames from accumulating at in the data buffer
|
//this should keep broken frames from accumulating at in the data buffer
|
||||||
if (framePart.length() > frameStrConst.length())
|
if (framePart.length() > frameStrConst.length())
|
||||||
|
{
|
||||||
return decodeFrames(framePart.right(framePart.length() - frameStrConst.length()), busNum);
|
return decodeFrames(framePart.right(framePart.length() - frameStrConst.length()), busNum);
|
||||||
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SocketCANd::disconnectDevice() {
|
void SocketCANd::disconnectDevice() {
|
||||||
for (int i = 0; i < tcpClient.length(); i++)
|
for (int i = 0; i < tcpClient.length(); i++)
|
||||||
@@ -405,6 +402,7 @@ void SocketCANd::procRXData(QString data, int busNum)
|
|||||||
{
|
{
|
||||||
switchToRawMode(busNum);
|
switchToRawMode(busNum);
|
||||||
rx_state[busNum] = SWITCHING2RAW;
|
rx_state[busNum] = SWITCHING2RAW;
|
||||||
|
unprocessedData[busNum].clear();
|
||||||
}
|
}
|
||||||
else qInfo() << hostCanIDs[busNum] << ": Could not open bus. Host did not respond with ""< ok >"": " << data;
|
else qInfo() << hostCanIDs[busNum] << ": Could not open bus. Host did not respond with ""< ok >"": " << data;
|
||||||
break;
|
break;
|
||||||
@@ -418,29 +416,31 @@ void SocketCANd::procRXData(QString data, int busNum)
|
|||||||
{
|
{
|
||||||
qDebug() << "Ok found at start of compound message, switching to RAW and decoding immediately";
|
qDebug() << "Ok found at start of compound message, switching to RAW and decoding immediately";
|
||||||
rx_state[busNum] = RAWMODE;
|
rx_state[busNum] = RAWMODE;
|
||||||
unprocessedData = decodeFrames(data, busNum);
|
unprocessedData[busNum] = decodeFrames(data, busNum);
|
||||||
}
|
}
|
||||||
else if(data.indexOf("< ok >", 0, Qt::CaseSensitivity::CaseInsensitive) > 0)
|
else if(data.indexOf("< ok >", 0, Qt::CaseSensitivity::CaseInsensitive) > 0)
|
||||||
{
|
{
|
||||||
qDebug() << "Ok found at in middle of compound message, switching to RAW and decoding immediately";
|
qDebug() << "Ok found at in middle of compound message, switching to RAW and decoding immediately";
|
||||||
rx_state[busNum] = RAWMODE;
|
rx_state[busNum] = RAWMODE;
|
||||||
unprocessedData = decodeFrames(data, busNum);
|
unprocessedData[busNum] = decodeFrames(data, busNum);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RAWMODE:
|
case RAWMODE:
|
||||||
if(!unprocessedData.isEmpty())
|
//if(!unprocessedData[busNum].isEmpty())
|
||||||
{
|
//{
|
||||||
//qDebug() << unprocessedData.length() << " bytes of unprocessedData: " << unprocessedData << " adding it to new data: " + data.left(50) + "...";
|
// qDebug() << "busNum: " << busNum << "- " << unprocessedData[busNum].length() << " bytes of unprocessedData: " << unprocessedData[busNum] << " adding it to new data: " + data.left(50) + "...";
|
||||||
}
|
// }
|
||||||
unprocessedData = decodeFrames(unprocessedData + data, busNum);
|
unprocessedData[busNum] = decodeFrames(unprocessedData[busNum] + data, busNum);
|
||||||
|
//if(unprocessedData[busNum].length() > 0)
|
||||||
|
// qDebug() << "busNum: " << busNum << " has data left over, what was at the end of the last packet?: " << data.right(20);
|
||||||
|
|
||||||
if(unprocessedData.length() > 128)
|
if(unprocessedData[busNum].length() > 128)
|
||||||
{
|
{
|
||||||
//the buffer has grown too much we need to clear it out, but what is good logic for that?
|
//the buffer has grown too much we need to clear it out, but what is good logic for that?
|
||||||
//the decodeFrames function strips out datat that doesn't have a '< frame' starting token, and in its
|
//the decodeFrames function strips out datat that doesn't have a '< frame' starting token, and in its
|
||||||
//recursive calling of itself it strips out data that preceedes valid frames, so this should never happen
|
//recursive calling of itself it strips out data that preceedes valid frames, so this should never happen
|
||||||
qDebug() << unprocessedData.length() << " bytes in unprocessedData, something is wrong, clearing...";
|
qDebug() << "busNum: " << busNum << "- " << unprocessedData[busNum].length() << " bytes in unprocessedData, something is wrong, clearing...";
|
||||||
unprocessedData.clear();
|
unprocessedData[busNum].clear();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ISOTP:
|
case ISOTP:
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ protected:
|
|||||||
QByteArray buildData;
|
QByteArray buildData;
|
||||||
QVarLengthArray<MODE> rx_state;
|
QVarLengthArray<MODE> rx_state;
|
||||||
CANFrame buildFrame;
|
CANFrame buildFrame;
|
||||||
QString unprocessedData;
|
QVarLengthArray<QString> unprocessedData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user