mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-02-03 09:44:01 -05:00
VISA: Prevent crash if VISA library not available
This commit is contained in:
parent
ce5837a392
commit
3bd0776706
@ -52,7 +52,7 @@ VISA::VISA() :
|
|||||||
viFindRsrc = (ViStatus (*) (ViSession vi, ViString expr, ViPFindList li, ViPUInt32 retCnt, ViChar desc[])) libraryFunc(visaLibrary, "viFindRsrc");
|
viFindRsrc = (ViStatus (*) (ViSession vi, ViString expr, ViPFindList li, ViPUInt32 retCnt, ViChar desc[])) libraryFunc(visaLibrary, "viFindRsrc");
|
||||||
viFindNext = (ViStatus (*) (ViSession vi, ViChar desc[])) libraryFunc(visaLibrary, "viFindNext");
|
viFindNext = (ViStatus (*) (ViSession vi, ViChar desc[])) libraryFunc(visaLibrary, "viFindNext");
|
||||||
|
|
||||||
if (viOpenDefaultRM && viOpen && viClose && viPrintf) {
|
if (viOpenDefaultRM && viOpen && viClose && viPrintf && viFindRsrc && viFindNext) {
|
||||||
m_available = true;
|
m_available = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,62 +111,74 @@ void VISA::close(ViSession session)
|
|||||||
|
|
||||||
QStringList VISA::processCommands(ViSession session, const QString& commands, bool *error)
|
QStringList VISA::processCommands(ViSession session, const QString& commands, bool *error)
|
||||||
{
|
{
|
||||||
QStringList list = commands.split("\n");
|
|
||||||
QStringList results;
|
QStringList results;
|
||||||
ViStatus status;
|
|
||||||
|
|
||||||
if (error) {
|
if (isAvailable())
|
||||||
*error = false;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < list.size(); i++)
|
|
||||||
{
|
{
|
||||||
QString command = list[i].trimmed();
|
QStringList list = commands.split("\n");
|
||||||
if (!command.isEmpty() && !command.startsWith("#")) // Allow # to comment out lines
|
ViStatus status;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
*error = false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < list.size(); i++)
|
||||||
{
|
{
|
||||||
if (m_debugIO) {
|
QString command = list[i].trimmed();
|
||||||
qDebug() << "VISA ->: " << command;
|
if (!command.isEmpty() && !command.startsWith("#")) // Allow # to comment out lines
|
||||||
}
|
|
||||||
QByteArray bytes = QString("%1\n").arg(command).toLatin1();
|
|
||||||
char *cmd = bytes.data();
|
|
||||||
status = viPrintf(session, cmd);
|
|
||||||
if (error && status) {
|
|
||||||
*error = true;
|
|
||||||
}
|
|
||||||
if (command.contains("?"))
|
|
||||||
{
|
{
|
||||||
char buf[1024] = "";
|
if (m_debugIO) {
|
||||||
char format[] = "%t";
|
qDebug() << "VISA ->: " << command;
|
||||||
status = viScanf(session, format, buf);
|
}
|
||||||
|
QByteArray bytes = QString("%1\n").arg(command).toLatin1();
|
||||||
|
char *cmd = bytes.data();
|
||||||
|
status = viPrintf(session, cmd);
|
||||||
if (error && status) {
|
if (error && status) {
|
||||||
*error = true;
|
*error = true;
|
||||||
}
|
}
|
||||||
results.append(buf);
|
if (command.contains("?"))
|
||||||
if (m_debugIO) {
|
{
|
||||||
qDebug() << "VISA <-: " << QString(buf).trimmed();
|
char buf[1024] = "";
|
||||||
|
char format[] = "%t";
|
||||||
|
status = viScanf(session, format, buf);
|
||||||
|
if (error && status) {
|
||||||
|
*error = true;
|
||||||
|
}
|
||||||
|
results.append(buf);
|
||||||
|
if (m_debugIO) {
|
||||||
|
qDebug() << "VISA <-: " << QString(buf).trimmed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (error)
|
||||||
|
{
|
||||||
|
*error = true;
|
||||||
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList VISA::findResources()
|
QStringList VISA::findResources()
|
||||||
{
|
{
|
||||||
QStringList resources;
|
QStringList resources;
|
||||||
ViChar rsrc[VI_FIND_BUFLEN];
|
|
||||||
ViFindList list;
|
|
||||||
ViRsrc matches = rsrc;
|
|
||||||
ViUInt32 nMatches = 0;
|
|
||||||
ViChar expr[] = "?*INSTR";
|
|
||||||
|
|
||||||
if (VI_SUCCESS == viFindRsrc(m_defaultRM, expr, &list, &nMatches, matches))
|
if (isAvailable())
|
||||||
{
|
{
|
||||||
if (nMatches > 0)
|
ViChar rsrc[VI_FIND_BUFLEN];
|
||||||
|
ViFindList list;
|
||||||
|
ViRsrc matches = rsrc;
|
||||||
|
ViUInt32 nMatches = 0;
|
||||||
|
ViChar expr[] = "?*INSTR";
|
||||||
|
|
||||||
|
if (VI_SUCCESS == viFindRsrc(m_defaultRM, expr, &list, &nMatches, matches))
|
||||||
{
|
{
|
||||||
resources.append(QString(rsrc));
|
if (nMatches > 0)
|
||||||
while (VI_SUCCESS == viFindNext(list, matches))
|
|
||||||
{
|
{
|
||||||
resources.append(QString(rsrc));
|
resources.append(QString(rsrc));
|
||||||
|
while (VI_SUCCESS == viFindNext(list, matches))
|
||||||
|
{
|
||||||
|
resources.append(QString(rsrc));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,30 +187,33 @@ QStringList VISA::findResources()
|
|||||||
|
|
||||||
bool VISA::identification(ViSession session, QString &manufacturer, QString &model, QString &serialNumber, QString &revision)
|
bool VISA::identification(ViSession session, QString &manufacturer, QString &model, QString &serialNumber, QString &revision)
|
||||||
{
|
{
|
||||||
QStringList result = processCommands(session, "*IDN?");
|
if (isAvailable())
|
||||||
if ((result.size() == 1) && (!result[0].isEmpty()))
|
|
||||||
{
|
{
|
||||||
QStringList details = result[0].trimmed().split(',');
|
QStringList result = processCommands(session, "*IDN?");
|
||||||
manufacturer = details[0];
|
if ((result.size() == 1) && (!result[0].isEmpty()))
|
||||||
// Some serial devices (ASRLn) loop back the the command if not connected
|
{
|
||||||
if (manufacturer == "*IDN?") {
|
QStringList details = result[0].trimmed().split(',');
|
||||||
return false;
|
manufacturer = details[0];
|
||||||
|
// Some serial devices (ASRLn) loop back the the command if not connected
|
||||||
|
if (manufacturer == "*IDN?") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (details.size() >= 2) {
|
||||||
|
model = details[1];
|
||||||
|
}
|
||||||
|
if (details.size() >= 3) {
|
||||||
|
serialNumber = details[2];
|
||||||
|
}
|
||||||
|
if (details.size() >= 4) {
|
||||||
|
revision = details[3];
|
||||||
|
}
|
||||||
|
qDebug() << "VISA::identification: "
|
||||||
|
<< "manufacturer: " << manufacturer
|
||||||
|
<< "model: " << model
|
||||||
|
<< "serialNumber: " << serialNumber
|
||||||
|
<< "revision: " << revision;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (details.size() >= 2) {
|
|
||||||
model = details[1];
|
|
||||||
}
|
|
||||||
if (details.size() >= 3) {
|
|
||||||
serialNumber = details[2];
|
|
||||||
}
|
|
||||||
if (details.size() >= 4) {
|
|
||||||
revision = details[3];
|
|
||||||
}
|
|
||||||
qDebug() << "VISA::identification: "
|
|
||||||
<< "manufacturer: " << manufacturer
|
|
||||||
<< "model: " << model
|
|
||||||
<< "serialNumber: " << serialNumber
|
|
||||||
<< "revision: " << revision;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -207,27 +222,31 @@ bool VISA::identification(ViSession session, QString &manufacturer, QString &mod
|
|||||||
QList<VISA::Instrument> VISA::instruments(QRegularExpression *filter)
|
QList<VISA::Instrument> VISA::instruments(QRegularExpression *filter)
|
||||||
{
|
{
|
||||||
QList<VISA::Instrument> instruments;
|
QList<VISA::Instrument> instruments;
|
||||||
QStringList resourceList = findResources();
|
|
||||||
|
|
||||||
for (auto const &resource : resourceList)
|
if (isAvailable())
|
||||||
{
|
{
|
||||||
if (filter)
|
QStringList resourceList = findResources();
|
||||||
|
|
||||||
|
for (auto const &resource : resourceList)
|
||||||
{
|
{
|
||||||
if (filter->match(resource).hasMatch()) {
|
if (filter)
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ViSession session = open(resource);
|
|
||||||
if (session)
|
|
||||||
{
|
|
||||||
Instrument instrument;
|
|
||||||
QString manufacturer, model, serialNumber, revision;
|
|
||||||
if (identification(session, instrument.m_manufacturer, instrument.m_model, instrument.m_serial, instrument.m_revision))
|
|
||||||
{
|
{
|
||||||
instrument.m_resource = resource;
|
if (filter->match(resource).hasMatch()) {
|
||||||
instruments.append(instrument);
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ViSession session = open(resource);
|
||||||
|
if (session)
|
||||||
|
{
|
||||||
|
Instrument instrument;
|
||||||
|
QString manufacturer, model, serialNumber, revision;
|
||||||
|
if (identification(session, instrument.m_manufacturer, instrument.m_model, instrument.m_serial, instrument.m_revision))
|
||||||
|
{
|
||||||
|
instrument.m_resource = resource;
|
||||||
|
instruments.append(instrument);
|
||||||
|
}
|
||||||
|
close(session);
|
||||||
}
|
}
|
||||||
close(session);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return instruments;
|
return instruments;
|
||||||
|
Loading…
Reference in New Issue
Block a user