mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-05-29 13:32:26 -04:00
VISA: Add methods to get available resources. Make I/O logging optional. Check for errors in processCommands.
This commit is contained in:
parent
13baf92a95
commit
9895d4618c
@ -16,6 +16,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include "visa.h"
|
#include "visa.h"
|
||||||
|
|
||||||
@ -48,6 +49,8 @@ VISA::VISA() :
|
|||||||
viClose = (ViStatus (*)(ViObject vi)) libraryFunc(visaLibrary, "viClose");
|
viClose = (ViStatus (*)(ViObject vi)) libraryFunc(visaLibrary, "viClose");
|
||||||
viPrintf = (ViStatus (*) (ViSession vi, ViString writeFmt, ...)) libraryFunc(visaLibrary, "viPrintf");
|
viPrintf = (ViStatus (*) (ViSession vi, ViString writeFmt, ...)) libraryFunc(visaLibrary, "viPrintf");
|
||||||
viScanf = (ViStatus (*) (ViSession vi, ViString writeFmt, ...)) libraryFunc(visaLibrary, "viScanf");
|
viScanf = (ViStatus (*) (ViSession vi, ViString writeFmt, ...)) libraryFunc(visaLibrary, "viScanf");
|
||||||
|
viFindRsrc = (ViStatus (*) (ViSession vi, ViString expr, ViPFindList li, ViPUInt32 retCnt, ViChar desc[])) libraryFunc(visaLibrary, "viFindRsrc");
|
||||||
|
viFindNext = (ViStatus (*) (ViSession vi, ViChar desc[])) libraryFunc(visaLibrary, "viFindNext");
|
||||||
|
|
||||||
if (viOpenDefaultRM && viOpen && viClose && viPrintf) {
|
if (viOpenDefaultRM && viOpen && viClose && viPrintf) {
|
||||||
m_available = true;
|
m_available = true;
|
||||||
@ -106,32 +109,130 @@ void VISA::close(ViSession session)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList VISA::processCommands(ViSession session, const QString& commands)
|
QStringList VISA::processCommands(ViSession session, const QString& commands, bool *error)
|
||||||
{
|
{
|
||||||
QStringList list = commands.split("\n");
|
QStringList list = commands.split("\n");
|
||||||
QStringList results;
|
QStringList results;
|
||||||
|
ViStatus status;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
*error = false;
|
||||||
|
}
|
||||||
for (int i = 0; i < list.size(); i++)
|
for (int i = 0; i < list.size(); i++)
|
||||||
{
|
{
|
||||||
QString command = list[i].trimmed();
|
QString command = list[i].trimmed();
|
||||||
if (!command.isEmpty() && !command.startsWith("#")) // Allow # to comment out lines
|
if (!command.isEmpty() && !command.startsWith("#")) // Allow # to comment out lines
|
||||||
{
|
{
|
||||||
qDebug() << "VISA ->: " << command;
|
if (m_debugIO) {
|
||||||
|
qDebug() << "VISA ->: " << command;
|
||||||
|
}
|
||||||
QByteArray bytes = QString("%1\n").arg(command).toLatin1();
|
QByteArray bytes = QString("%1\n").arg(command).toLatin1();
|
||||||
char *cmd = bytes.data();
|
char *cmd = bytes.data();
|
||||||
viPrintf(session, cmd);
|
status = viPrintf(session, cmd);
|
||||||
if (command.endsWith("?"))
|
if (error && status) {
|
||||||
|
*error = true;
|
||||||
|
}
|
||||||
|
if (command.contains("?"))
|
||||||
{
|
{
|
||||||
char buf[1024] = "";
|
char buf[1024] = "";
|
||||||
char format[] = "%t";
|
char format[] = "%t";
|
||||||
viScanf(session, format, buf);
|
status = viScanf(session, format, buf);
|
||||||
|
if (error && status) {
|
||||||
|
*error = true;
|
||||||
|
}
|
||||||
results.append(buf);
|
results.append(buf);
|
||||||
qDebug() << "VISA <-: " << QString(buf).trimmed();
|
if (m_debugIO) {
|
||||||
|
qDebug() << "VISA <-: " << QString(buf).trimmed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList VISA::findResources()
|
||||||
|
{
|
||||||
|
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 (nMatches > 0)
|
||||||
|
{
|
||||||
|
resources.append(QString(rsrc));
|
||||||
|
while (VI_SUCCESS == viFindNext(list, matches))
|
||||||
|
{
|
||||||
|
resources.append(QString(rsrc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resources;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VISA::identification(ViSession session, QString &manufacturer, QString &model, QString &serialNumber, QString &revision)
|
||||||
|
{
|
||||||
|
QStringList result = processCommands(session, "*IDN?");
|
||||||
|
if ((result.size() == 1) && (!result[0].isEmpty()))
|
||||||
|
{
|
||||||
|
QStringList details = result[0].trimmed().split(',');
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter is a list of resources not to try to open
|
||||||
|
QList<VISA::Instrument> VISA::instruments(QRegularExpression *filter)
|
||||||
|
{
|
||||||
|
QList<VISA::Instrument> instruments;
|
||||||
|
QStringList resourceList = findResources();
|
||||||
|
|
||||||
|
for (auto const &resource : resourceList)
|
||||||
|
{
|
||||||
|
if (filter)
|
||||||
|
{
|
||||||
|
if (filter->match(resource).hasMatch()) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instruments;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
void *VISA::libraryOpen(const char *filename)
|
void *VISA::libraryOpen(const char *filename)
|
||||||
|
@ -23,6 +23,10 @@
|
|||||||
|
|
||||||
#include "export.h"
|
#include "export.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
class QRegularExpression;
|
||||||
|
|
||||||
typedef char ViChar;
|
typedef char ViChar;
|
||||||
typedef ViChar * ViPChar;
|
typedef ViChar * ViPChar;
|
||||||
typedef signed long ViInt32;
|
typedef signed long ViInt32;
|
||||||
@ -35,17 +39,29 @@ typedef ViObject ViSession;
|
|||||||
typedef ViSession * ViPSession;
|
typedef ViSession * ViPSession;
|
||||||
typedef ViString ViRsrc;
|
typedef ViString ViRsrc;
|
||||||
typedef ViUInt32 ViAccessMode;
|
typedef ViUInt32 ViAccessMode;
|
||||||
|
typedef ViUInt32 * ViPUInt32;
|
||||||
|
typedef ViObject ViFindList;
|
||||||
|
typedef ViFindList * ViPFindList;
|
||||||
|
|
||||||
#define VI_SUCCESS 0
|
#define VI_SUCCESS 0
|
||||||
#define VI_TRUE 1
|
#define VI_TRUE 1
|
||||||
#define VI_FALSE 0
|
#define VI_FALSE 0
|
||||||
#define VI_NULL 0
|
#define VI_NULL 0
|
||||||
|
#define VI_FIND_BUFLEN 256
|
||||||
|
|
||||||
// We dynamically load the visa dll, as most users probably do not have it
|
// We dynamically load the visa dll, as most users probably do not have it
|
||||||
// Note: Can't seem to call viOpenDefaultRM/viClose in constructor / destructor of global instance
|
// Note: Can't seem to call viOpenDefaultRM/viClose in constructor / destructor of global instance
|
||||||
class SDRBASE_API VISA {
|
class SDRBASE_API VISA {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
struct Instrument {
|
||||||
|
QString m_resource;
|
||||||
|
QString m_manufacturer;
|
||||||
|
QString m_model;
|
||||||
|
QString m_serial;
|
||||||
|
QString m_revision;
|
||||||
|
};
|
||||||
|
|
||||||
// Default session
|
// Default session
|
||||||
ViSession m_defaultRM;
|
ViSession m_defaultRM;
|
||||||
// Function pointers to VISA API for direct calls
|
// Function pointers to VISA API for direct calls
|
||||||
@ -54,6 +70,8 @@ public:
|
|||||||
ViStatus (*viClose) (ViObject vi);
|
ViStatus (*viClose) (ViObject vi);
|
||||||
ViStatus (*viPrintf) (ViSession vi, ViString writeFmt, ...);
|
ViStatus (*viPrintf) (ViSession vi, ViString writeFmt, ...);
|
||||||
ViStatus (*viScanf) (ViSession vi, ViString readFmt, ...);
|
ViStatus (*viScanf) (ViSession vi, ViString readFmt, ...);
|
||||||
|
ViStatus (*viFindRsrc) (ViSession vi, ViString expr, ViPFindList li, ViPUInt32 retCnt, ViChar desc[]);
|
||||||
|
ViStatus (*viFindNext) (ViSession vi, ViChar desc[]);
|
||||||
|
|
||||||
VISA();
|
VISA();
|
||||||
|
|
||||||
@ -61,7 +79,11 @@ public:
|
|||||||
void closeDefault();
|
void closeDefault();
|
||||||
ViSession open(const QString& device);
|
ViSession open(const QString& device);
|
||||||
void close(ViSession session);
|
void close(ViSession session);
|
||||||
QStringList processCommands(ViSession session, const QString& commands);
|
QStringList processCommands(ViSession session, const QString& commands, bool *error=nullptr);
|
||||||
|
QStringList findResources();
|
||||||
|
bool identification(ViSession session, QString &manufacturer, QString &model, QString &serialNumber, QString &revision);
|
||||||
|
QList<Instrument> instruments(QRegularExpression *filter);
|
||||||
|
void setDebugIO(bool debugIO) { m_debugIO = debugIO; }
|
||||||
|
|
||||||
// Is the VISA library available
|
// Is the VISA library available
|
||||||
bool isAvailable() const
|
bool isAvailable() const
|
||||||
@ -71,6 +93,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_available;
|
bool m_available;
|
||||||
|
bool m_debugIO;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void *visaLibrary;
|
void *visaLibrary;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user