The following example demonstrates how to set up and run a basic experiment using the API. This example simply outlines the steps needed to connect to a Squidstat, build an experiment, and start it. Notably, it does not cover handling and reporting data from the Squidstat, which will be addressed in the Example on handling signals.
Setup
To begin, we need to include all neccessary headers or libraries, and setup the Qt event loop. At this stage, you can also retrieve the AisDeviceTracker Instance.
- C++
#include "AisDeviceTracker.h"
#include "AisExperiment.h"
#include "AisInstrumentHandler.h"
#include "experiments/builder_elements/AisConstantPotElement.h"
#include <QCoreApplication>
#include <QDebug>
#define COMPORT "COM1"
#define CHANNEL 0
int main()
{
char** test = nullptr;
int args;
QCoreApplication a(args, test);
- Python
import sys
from PySide6.QtWidgets import QApplication
from SquidstatPyLibrary import AisDeviceTracker
from SquidstatPyLibrary import AisExperiment, AisErrorCode
from SquidstatPyLibrary import AisInstrumentHandler
from SquidstatPyLibrary import AisConstantPotElement
COMPORT = "COM16"
CHANNEL = 0
app = QApplication()
Reference: https://doc.qt.io/qt-5/qcoreapplication.html
Building the experiment
Next, create an element (e.g. AisConstantPotElement) and pass in the required parameters. Then, we will create the experiment object AisExperiment and add the element to it using AisExperiment::appendElement.
- C++
auto customExperiment = std::make_shared<AisExperiment>();
customExperiment->appendElement(cvElement, 1);
- Python
success = experiment.appendElement(cvElement, 1)
if not success:
print("Error adding element to experiment")
app.quit()
Reference: https://en.cppreference.com/w/cpp/memory/shared_ptr
Connecting to the Device
Connect to the instrument by calling AisDeviceTracker::connectToDeviceOnComPort with the desired COM port to link the AisDeviceTracker to the device Alternatively, use AisDeviceTracker::connectAllPluggedInDevices to connect to all available devices.
- C++
AisErrorCode error = tracker->connectToDeviceOnComPort(COMPORT);
if (error != error.Success) {
qDebug() << error.message();
return 0;
}
- Python
error = tracker.connectToDeviceOnComPort(COMPORT)
if error.value() != AisErrorCode.Success:
print(error.message())
sys.exit()
Note: Any command to the instrument should always be checked for errors via the returned AisErrorCode
Starting Experiment
Finally, to start the experiment:
- Grab the instrument handler from the device tracker after establishing a connection to the instrument.
- See Next Example for more details on establishing connections
- Upload the experiment to the desired channel and check for errors
- Start the experiment, again checking for any errors
- Run the QApplication event loop to allow all relevant signals to be processed (see the next example "Handling Signals")
- C++
auto& handler = tracker->getInstrumentHandler(deviceName);
connectSignals(handler);
AisErrorCode error = handler.uploadExperimentToChannel(CHANNEL, customExperiment);
if (error) {
qDebug() << error.message();
return;
}
error = handler.startUploadedExperiment(CHANNEL);
if (error) {
qDebug() << error.message();
return;
}
- Python
handler = tracker.getInstrumentHandler(deviceName)
connectSignals(handler)
error = handler.uploadExperimentToChannel(CHANNEL, experiment)
if error.value() != AisErrorCode.Success:
print(error.message())
app.quit()
error = handler.startUploadedExperiment(CHANNEL)
if error.value() != AisErrorCode.Success:
print(error.message())
app.quit()
See the full example here
Now that we have covered the basics of connecting to a Squidstat, building an experiment, and starting it, we can proceed to the next example. It demonstrates how to combine these steps and handle the signals emitted by the Squidstat.