Loading...
Searching...
No Matches
dataOutput.cpp

This example shows how to capture data from the device, and write it to a CSV file using Qt's QFile and QTextStream classes.

#include "AisExperiment.h"
#include "AisDeviceTracker.h"
#include "AisInstrumentHandler.h"
#include "experiments/builder_elements/AisConstantCurrentElement.h"
#include "experiments/builder_elements/AisConstantPotElement.h"
#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
#include <QFile>
#include <QStandardPaths>
// Define relevant device information, for easy access
#define COMPORT "COM1"
#define CHANNEL 0
int main()
{
char** test = nullptr;
int args;
QCoreApplication a(args, test);
auto tracker = AisDeviceTracker::Instance();
// Build the experiment
1, // voltage: 1v
1, // sampling interval: 1s
30 // duration: 30s
);
0.001, // current: 1mA
1, // sampling interval: 1s
60 // duration: 60s
);
auto customExperiment = std::make_shared<AisExperiment>();
customExperiment->appendElement(ccElement, 1);
customExperiment->appendElement(cvElement, 1);
// Static QString variable to store the current elements file path
static QString filePath;
// Create a lambda funnction to connect signals to the handler
auto connectSignals = [=](const AisInstrumentHandler& handler) {
QObject::connect(&handler, &AisInstrumentHandler::experimentNewElementStarting, [=](uint8_t channel, const AisExperimentNode& node) {
// Create a unique file name for each element, with the format "stepNumber_stepName_expStartTime.csv"
static int fileNum = 1;
auto name = "/" + QString::number(fileNum) + "_" + QString::number(node.stepNumber) + "_" + node.stepName + ".csv";
filePath = QString(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)) + name;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
// Writing headers to file
QTextStream out(&file);
out << "Time Stamp,"
<< "Counter Electrode Voltage,"
<< "Working Electrode Voltage,"
<< "Current"
<< "\n";
file.close();
qDebug() << "New element beginning: " << node.stepName << "step: " << node.stepNumber;
});
QObject::connect(&handler, &AisInstrumentHandler::activeDCDataReady, [=](uint8_t channel, const AisDCData& data) {
qDebug() << "current :" << data.current << " voltage: " << data.workingElectrodeVoltage << " counter electrode : " << data.counterElectrodeVoltage << " timestamp : " << data.timestamp;
// Save the DC data to the file created at element beginning
QFile file(filePath);
if (!file.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file);
out << data.timestamp << ","
<< data.counterElectrodeVoltage << ","
<< data.workingElectrodeVoltage << ","
<< data.current
<< "\n";
file.close();
});
// The experiment created only uses DC elements, so this will not be executed.
// However, ac data can be handled and written to a csv like seen above with dc data
QObject::connect(&handler, &AisInstrumentHandler::activeACDataReady, [=](uint8_t channel, const AisACData& data) {
qDebug() << data.frequency << " " << data.absoluteImpedance << " " << data.phaseAngle;
});
QObject::connect(&handler, &AisInstrumentHandler::experimentStopped, [=](uint8_t channel, const QString& reason) {
qDebug() << "Experiment Completed Signal " << channel << "Reason : " << reason;
});
};
// When device is connected, setup connections, and upload/start the experiment
QObject::connect(tracker, &AisDeviceTracker::newDeviceConnected, [=](const QString& deviceName) {
auto& handler = tracker->getInstrumentHandler(deviceName);
connectSignals(handler);
AisErrorCode error = handler.uploadExperimentToChannel(0, customExperiment);
if (error) {
qDebug() << error.message();
return 0;
}
error = handler.startUploadedExperiment(CHANNEL);
if (error) {
qDebug() << error.message();
return 0;
}
});
AisErrorCode error = tracker->connectToDeviceOnComPort(COMPORT);
if (error != error.Success) {
qDebug() << error.message();
return 0;
}
return a.exec();
}
an experiment that simulates a constant current flow with more advance options for stopping the exper...
Definition AisConstantCurrentElement.h:18
an experiment that simulates a constant applied voltage.
Definition AisConstantPotElement.h:18
static AisDeviceTracker * Instance()
get the instance of the device tracker.
void newDeviceConnected(const QString &deviceName)
a signal to be emitted whenever a new connection has been successfully established with a device.
This class contains the possible error codes returned to the user when working with the API....
Definition AisErrorCode.h:18
this class provides control of the device including starting, pausing, resuming and stopping an exper...
Definition AisInstrumentHandler.h:27
void activeACDataReady(uint8_t channel, const AisACData &ACData)
a signal that is emitted whenever new AC data for an active experiment are ready.
void activeDCDataReady(uint8_t channel, const AisDCData &DCData)
a signal that is emitted whenever new DC data for an active experiment are ready.
void experimentNewElementStarting(uint8_t channel, const AisExperimentNode &stepInfo)
a signal that is emitted whenever a new elemental experiment has started.
void experimentStopped(uint8_t channel, const QString &reason)
a signal that is emitted whenever an experiment was stopped manually or has completed.
A structure containing AC data collected from the instrument.
Definition AisDataPoints.h:44
A structure containing DC data collected from the instrument.
Definition AisDataPoints.h:11
A structure containing some information regarding the running element.
Definition AisDataPoints.h:113
QString stepName
This is the name of the current element running.
Definition AisDataPoints.h:118
int stepNumber
this number is the order of the element within the custom experiment.
Definition AisDataPoints.h:123