<commit_before>#include "serialworker.h"

#include <QSerialPort>
#include <QDebug>
#include <QTimer>

SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(parent)
{
    serial = NULL;
    rx_state = IDLE;
    rx_step = 0;
    buildFrame = new CANFrame;
    canModel = model;
    gotFrames = 0;
    ticker = NULL;
    elapsedTime = NULL;
    framesPerSec = 0;
    capturing = true;
}

SerialWorker::~SerialWorker()
{
    if (serial != NULL)
    {
        if (serial->isOpen())
        {
            serial->clear();
            serial->close();

        }
        serial->disconnect(); //disconnect all signals
        delete serial;
    }
    if (ticker != NULL) ticker->stop();
}

void SerialWorker::setSerialPort(QSerialPortInfo *port)
{
    if (serial != NULL)
    {
        if (serial->isOpen())
        {
            serial->clear();
            serial->close();
        }
        serial->disconnect(); //disconnect all signals
        delete serial;
    }

    serial = new QSerialPort(*port);

    qDebug() << "Serial port name is " << port->portName();
    serial->setBaudRate(serial->Baud115200);
    serial->setDataBits(serial->Data8);
    serial->setFlowControl(serial->HardwareControl);
    serial->open(QIODevice::ReadWrite);
    serial->setDataTerminalReady(true);
    serial->setRequestToSend(true);
    QByteArray output;
    output.append(0xE7); //this puts the device into binary comm mode
    output.append(0xE7);
    output.append(0xF1); //signal we want to issue a command
    output.append(0x06); //request canbus stats from the board
    output.append(0xF1); //another command to the GVRET
    output.append(0x07); //request device information
    serial->write(output);
    connected = true; //should be false. only set true to test old firmware
    connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData()));
    QTimer::singleShot(1000, this, SLOT(connectionTimeout()));
    if (ticker == NULL)
    {
        ticker = new QTimer;
        connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick()));
    }
    if (elapsedTime == NULL)
    {
        elapsedTime = new QTime;
        elapsedTime->start();
    }
    ticker->setInterval(250); //tick four times per second
    ticker->setSingleShot(false); //keep ticking
    ticker->start();
}

void SerialWorker::connectionTimeout()
{
    //one second after trying to connect are we actually connected?
    if (!connected) //no?
    {
        //then emit the the failure signal and see if anyone cares
        //qDebug() << "Failed to connect to GVRET at that com port";
        //ticker->stop();
        //emit connectionFailure();
    }
}

void SerialWorker::readSerialData()
{
    QByteArray data = serial->readAll();
    unsigned char c;
    //qDebug() << (tr("Got data from serial. Len = %0").arg(data.length()));
    for (int i = 0; i < data.length(); i++)<commit_msg>Added back in the validation of comm to GVRET.<commit_after>#include "serialworker.h"

#include <QSerialPort>
#include <QDebug>
#include <QTimer>

SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(parent)
{
    serial = NULL;
    rx_state = IDLE;
    rx_step = 0;
    buildFrame = new CANFrame;
    canModel = model;
    gotFrames = 0;
    ticker = NULL;
    elapsedTime = NULL;
    framesPerSec = 0;
    capturing = true;
}

SerialWorker::~SerialWorker()
{
    if (serial != NULL)
    {
        if (serial->isOpen())
        {
            serial->clear();
            serial->close();

        }
        serial->disconnect(); //disconnect all signals
        delete serial;
    }
    if (ticker != NULL) ticker->stop();
}

void SerialWorker::setSerialPort(QSerialPortInfo *port)
{
    if (serial != NULL)
    {
        if (serial->isOpen())
        {
            serial->clear();
            serial->close();
        }
        serial->disconnect(); //disconnect all signals
        delete serial;
    }

    serial = new QSerialPort(*port);

    qDebug() << "Serial port name is " << port->portName();
    serial->setBaudRate(serial->Baud115200);
    serial->setDataBits(serial->Data8);
    serial->setFlowControl(serial->HardwareControl);
    serial->open(QIODevice::ReadWrite);
    serial->setDataTerminalReady(true);
    serial->setRequestToSend(true);
    QByteArray output;
    output.append(0xE7); //this puts the device into binary comm mode
    output.append(0xE7);
    output.append(0xF1); //signal we want to issue a command
    output.append(0x06); //request canbus stats from the board
    output.append(0xF1); //another command to the GVRET
    output.append(0x07); //request device information
    serial->write(output);
    connected = false;
    connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData()));
    QTimer::singleShot(1000, this, SLOT(connectionTimeout()));
    if (ticker == NULL)
    {
        ticker = new QTimer;
        connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick()));
    }
    if (elapsedTime == NULL)
    {
        elapsedTime = new QTime;
        elapsedTime->start();
    }
    ticker->setInterval(250); //tick four times per second
    ticker->setSingleShot(false); //keep ticking
    ticker->start();
}

void SerialWorker::connectionTimeout()
{
    //one second after trying to connect are we actually connected?
    if (!connected) //no?
    {
        //then emit the the failure signal and see if anyone cares
        qDebug() << "Failed to connect to GVRET at that com port";
        ticker->stop();
        emit connectionFailure();
    }
}

void SerialWorker::readSerialData()
{
    QByteArray data = serial->readAll();
    unsigned char c;
    //qDebug() << (tr("Got data from serial. Len = %0").arg(data.length()));
    for (int i = 0; i < data.length(); i++)
{'fraction_non_alphanumeric': 0.13817166782972784, 'fraction_numerical': 0.011514305652477321, 'mean_word_length': 2.774193548387097, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 61, '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': '3778433', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1726, 'n_words': 453}