<commit_before>#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/select.h>
#include <unistd.h>
#include <QtCore>

#include "../QSCPIDev/qscpidev.h"

QSCPIDev::Sense_t QSCPIDev::SenseVolt = "CONF:VOLT";
QSCPIDev::Sense_t QSCPIDev::SenseRes = "CONF:RES";
QSCPIDev::Sense_t QSCPIDev::SenseTemp = "CONF:TEMP";

QSCPIDev::QSCPIDev() :
    QSerial(), errorno(0), errorstr("")
{
}

QSCPIDev::~QSCPIDev()
{
    close();
}

void QSCPIDev::close()
{
    QSerial::close();
}

bool QSCPIDev::current(double *i)
{
    QString resp;
    if (!sendQuery(&resp, "SOUR:CURR?"))
        return false;

    bool ok;
    *i = QVariant(resp).toDouble(&ok);

    return ok;
}

int QSCPIDev::error() const
{
    return errorno;
}

QString QSCPIDev::errorString() const
{
    if (errorno == ERR_QSERIAL)
        return QSerial::errorString();
    return errorstr;
}

QString QSCPIDev::formatCmd(const QString &cmd, const QStringList &params, const Channels_t &channels)
{
    if (params.size() == 0 && channels.size() == 0)
        return cmd;

    QStringList params_(params);

    if (channels.size()) {
        QStringList ch;
        foreach(int channel, channels) {
            ch.append(QVariant(channel).toString());
        }

        params_.append(QString("(@").append(ch.join(",")).append(")"));
    }

    QString format("%1 %2");

    return format.arg(cmd).arg(params_.join(","));
}

bool QSCPIDev::idn(QString *resp)
{
    return sendQuery(resp, "*IDN?");
}

bool QSCPIDev::init()
{
    return sendCmd("INIT", 2000000);
}

bool QSCPIDev::open(const QString &port, BaudeRate_t baudeRate, bool setRemote)
{
    const long timeout_usec = (10l * 1000000l) / 9600l;

    close();

    if (!QSerial::open(port, baudeRate, 300000, timeout_usec)) {
        errorno = ERR_QSERIAL;
        return false;
    }

    if (!write("\n")) {
        errorno = ERR_QSERIAL;
        QSerial::close();
        return false;
    }
    if (!sendCmd("*RST;*CLS", 500000))
        return false;
    if (setRemote)
        return systemRemote();

    return true;
}

bool QSCPIDev::output(bool *enabled)
{
    QString resp;

    if (!sendQuery(&resp, "OUTP?"))
        return false;

    if (resp == "1") {
        *enabled = true;
        return true;
    }
    if (resp == "0") {
        *enabled = false;
        return true;
    }

    errorno = ERR_RESULT;
    errorstr = "ScpiDev::output invalid result";
    return false;
}

bool QSCPIDev::read(QStringList *values, long timeout_usec)
{
    QString s;

    if (!sendQuery(&s, "READ?", timeout_usec))
        return false;

    *values = s.split(",", QString::SkipEmptyParts);

    for (QStringList::iterator idata(values->begin()); idata != values->end(); ++idata) {
        *idata = idata->trimmed();
    }

    return true;
}

bool QSCPIDev::recvResponse(QString *resp, long timeout_usec)
{
    errorno = ERR_OK;
    errorstr = "";

    if (!readLine(resp, 1024, timeout_usec)) {
        errorno = ERR_QSERIAL;
        return false;
    }
    *resp = resp->trimmed();

    errorno = ERR_RESULT;
    errorstr = "ScpiDev::recvResponse invalid result";
    return false;
}

bool QSCPIDev::sendCmd(const QString &cmd, long timeout_usec)
{
    QString resp;

    if (!sendQuery(&resp, cmd, timeout_usec))<commit_msg>fix recieving response<commit_after>#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/select.h>
#include <unistd.h>
#include <QtCore>

#include "../QSCPIDev/qscpidev.h"

QSCPIDev::Sense_t QSCPIDev::SenseVolt = "CONF:VOLT";
QSCPIDev::Sense_t QSCPIDev::SenseRes = "CONF:RES";
QSCPIDev::Sense_t QSCPIDev::SenseTemp = "CONF:TEMP";

QSCPIDev::QSCPIDev() :
    QSerial(), errorno(0), errorstr("")
{
}

QSCPIDev::~QSCPIDev()
{
    close();
}

void QSCPIDev::close()
{
    QSerial::close();
}

bool QSCPIDev::current(double *i)
{
    QString resp;
    if (!sendQuery(&resp, "SOUR:CURR?"))
        return false;

    bool ok;
    *i = QVariant(resp).toDouble(&ok);

    return ok;
}

int QSCPIDev::error() const
{
    return errorno;
}

QString QSCPIDev::errorString() const
{
    if (errorno == ERR_QSERIAL)
        return QSerial::errorString();
    return errorstr;
}

QString QSCPIDev::formatCmd(const QString &cmd, const QStringList &params, const Channels_t &channels)
{
    if (params.size() == 0 && channels.size() == 0)
        return cmd;

    QStringList params_(params);

    if (channels.size()) {
        QStringList ch;
        foreach(int channel, channels) {
            ch.append(QVariant(channel).toString());
        }

        params_.append(QString("(@").append(ch.join(",")).append(")"));
    }

    QString format("%1 %2");

    return format.arg(cmd).arg(params_.join(","));
}

bool QSCPIDev::idn(QString *resp)
{
    return sendQuery(resp, "*IDN?");
}

bool QSCPIDev::init()
{
    return sendCmd("INIT", 2000000);
}

bool QSCPIDev::open(const QString &port, BaudeRate_t baudeRate, bool setRemote)
{
    const long timeout_usec = (10l * 1000000l) / 9600l;

    close();

    if (!QSerial::open(port, baudeRate, 300000, timeout_usec)) {
        errorno = ERR_QSERIAL;
        return false;
    }

    if (!write("\n")) {
        errorno = ERR_QSERIAL;
        QSerial::close();
        return false;
    }
    if (!sendCmd("*RST;*CLS", 500000))
        return false;
    if (setRemote)
        return systemRemote();

    return true;
}

bool QSCPIDev::output(bool *enabled)
{
    QString resp;

    if (!sendQuery(&resp, "OUTP?"))
        return false;

    if (resp == "1") {
        *enabled = true;
        return true;
    }
    if (resp == "0") {
        *enabled = false;
        return true;
    }

    errorno = ERR_RESULT;
    errorstr = "ScpiDev::output invalid result";
    return false;
}

bool QSCPIDev::read(QStringList *values, long timeout_usec)
{
    QString s;

    if (!sendQuery(&s, "READ?", timeout_usec))
        return false;

    *values = s.split(",", QString::SkipEmptyParts);

    for (QStringList::iterator idata(values->begin()); idata != values->end(); ++idata) {
        *idata = idata->trimmed();
    }

    return true;
}

bool QSCPIDev::recvResponse(QString *resp, long timeout_usec)
{
    errorno = ERR_OK;
    errorstr = "";

    if (!readLine(resp, 1024, timeout_usec)) {
        errorno = ERR_QSERIAL;
        return false;
    }
    *resp = resp->trimmed();
    if (!resp->isEmpty()) {
        return true;
    }

    errorno = ERR_RESULT;
    errorstr = "ScpiDev::recvResponse invalid result";
    return false;
}

bool QSCPIDev::sendCmd(const QString &cmd, long timeout_usec)
{
    QString resp;

    QString _cmd(cmd + ";*OPC?");
    if (!sendQuery(&resp, _cmd, timeout_usec))
{'fraction_non_alphanumeric': 0.16047171219585013, 'fraction_numerical': 0.01283773697566801, 'mean_word_length': 3.1003671970624236, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6089078', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2391, 'n_words': 506}