I'm having trouble with a recursive function crashing my program. I'll post the function and then explain what it does:
void MainWindow::sendGlobalCoordinates()
{
if(myThread->TXFlag == 1)
{
return sendGlobalCoordinates(); // try try again
}
else // TXFlag == 0
{
if(globalCoordinatesSent == 0) // if x&y not sent, Send the x
{
myThread->TXBytes[0] = 56; // setGlobalPosX
myThread->TXData = (int)(pointVector[calPoint2].gX * (double)4000);
myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
myThread->TXFlag = 1;
globalCoordinatesSent++;
}
else if(globalCoordinatesSent == 1) // If x sent, but y not sent, sent the y
{
myThread->TXBytes[0] = 57; // setGlobalPosY
myThread->TXData = (int)(pointVector[calPoint2].gY * (double)4000);
myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
myThread->TXFlag = 1;
globalCoordinatesSent++;
}
else if(globalCoordinatesSent == 2) // if x&y sent
{
startButton->setEnabled(1); // let the drilling begin!
return; // the recursive loop must be broken.
}
return sendGlobalCoordinates(); // keep going
}
}
void MainWindow::sendGlobalCoordinates()
{
if(myThread->TXFlag == 1)
{
return sendGlobalCoordinates(); // try try again
}
else // TXFlag == 0
{
if(globalCoordinatesSent == 0) // if x&y not sent, Send the x
{
myThread->TXBytes[0] = 56; // setGlobalPosX
myThread->TXData = (int)(pointVector[calPoint2].gX * (double)4000);
myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
myThread->TXFlag = 1;
globalCoordinatesSent++;
}
else if(globalCoordinatesSent == 1) // If x sent, but y not sent, sent the y
{
myThread->TXBytes[0] = 57; // setGlobalPosY
myThread->TXData = (int)(pointVector[calPoint2].gY * (double)4000);
myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
myThread->TXFlag = 1;
globalCoordinatesSent++;
}
else if(globalCoordinatesSent == 2) // if x&y sent
{
startButton->setEnabled(1); // let the drilling begin!
return; // the recursive loop must be broken.
}
return sendGlobalCoordinates(); // keep going
}
}
To copy to clipboard, switch view to plain text mode
OK, so here's the deal. I have a QThread running - named myThread - and its sole purpose is to send and receive data over the serial port. The function you see above was written to solve a problem. I need to send two sets of data over the serial port, but the second set of data can't be sent until the first one has been sent. So my idea was to write this recursive function which first sends the first set of data (if nothing is currently being sent over the serial port) and then, after that, sends the second set of data. If the serial port is busy sending data then the function just returns calling itself and eventually the job should get done. Brilliant idea, right? Unfortunately, the program crashes when I run it, and I know that the recursion is causing the problem (commenting out " return sendGlobalCoordinates();" keeps the program from crashing).
Any advice as to what I should do?
Bookmarks