I'm currently working on integrating a VISA connection into my Qt application to interact with a device for various tasks such as setting parameters like voltage and sample rate, starting data recording, fetching data, and returning the data. Most functionalities are implemented and working as expected; however, I encounter an issue when attempting to fetch data: after successfully receiving 250,000 values, I no longer receive any data, and a timeout error is triggered, specifically "Timeout Expired Before Operation Completed", as indicated by the condition if (status < VI_SUCCESS).
Has anyone experienced a similar issue or noticed any potential mistakes in my approach that could lead to this problem?
QString GetData() {
ViStatus status = VI_SUCCESS;
ViUInt32 returnCount = 0;
ViSession rmHandle, scopeHandle;
char idnResponse[5000];
vector<float> allData;
const char resourceString1[] = "USB Connection string";
// Open Resource manager first
status = viOpenDefaultRM(&rmHandle);
status = viOpen(rmHandle, resourceString1, VI_NULL, VI_NULL, &scopeHandle);
QElapsedTimer timer;
status = viClear(scopeHandle); //clear instrument's io buffers
QString command = "FLOG:DATA?";
QByteArray startMeasurementbyteArray = command.toUtf8();
QByteArray byteArray = command.toUtf8();
const char* cStartCommand = startMeasurementbyteArray.constData();
const char* cCommand = byteArray.constData();
QString allValues = "<br>";
bool firstRead = true;
uint8_t leftoverBytes[3];
uint8_t leftoverByteLength = 0;
timer.start();
bool weHaveData = true;
while(timer.elapsed() <= 300000)
{
status = viWrite(scopeHandle, (ViBuf)cCommand, (ViUInt32)strlen(cCommand), &returnCount);
status = viRead(scopeHandle, (ViBuf)idnResponse, sizeof(idnResponse), &returnCount);
// idnResponse[returnCount] = '\0'; //terminate the string properly
if (status < VI_SUCCESS)
{
char statusStr[100];
viStatusDesc(scopeHandle, status, statusStr);
TRACE(TL_WARN, statusStr);
weHaveData = false;
// break;
}
if(weHaveData)
{
int dataOffset = 0;
if (firstRead)
{
if (idnResponse[0] != '#') break;
dataOffset = (int)idnResponse[1] - '0' + 2; // skip length of length
firstRead = false;
}
if (leftoverByteLength)
{
float val;
memcpy(&val, leftoverBytes, leftoverByteLength);
memcpy(((uint8_t*)&val)+leftoverByteLength, idnResponse, sizeof(float)-leftoverByteLength);
allData.push_back(val);
dataOffset = sizeof(float)-leftoverByteLength;
}
leftoverByteLength = (returnCount - dataOffset) % sizeof(float);
// Convert the binary data to a float array and return it as a QString
size_t oldSize = allData.size();
allData.resize(oldSize + (returnCount - dataOffset)/sizeof(float));
memcpy(allData.data() + oldSize, idnResponse + dataOffset, returnCount - dataOffset - leftoverByteLength);
// rest goes to leftover buffer
memcpy(leftoverBytes, idnResponse + sizeof(idnResponse)-leftoverByteLength, leftoverByteLength);
}
weHaveData = true;
//Just debug stuff
if(allData.size() >= 250000){
TRACE(TL_WARN,"Why dont you continue");
status = viWrite(scopeHandle, (ViBuf)cCommand, (ViUInt32)strlen(cCommand), &returnCount);
}
// QThread::msleep(10);
}
return "All Values recorded: "+ QString::number(allData.size());
}