2018-06-19 19:58:52 +00:00
|
|
|
#include "commhandler.h"
|
2021-02-23 21:21:22 +00:00
|
|
|
#include "logcategories.h"
|
2018-06-19 19:58:52 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2020-03-12 03:58:31 +00:00
|
|
|
// Copytight 2017-2020 Elliott H. Liggett
|
|
|
|
|
2018-06-19 19:58:52 +00:00
|
|
|
commHandler::commHandler()
|
|
|
|
{
|
|
|
|
//constructor
|
|
|
|
// grab baud rate and other comm port details
|
|
|
|
// if they need to be changed later, please
|
|
|
|
// destroy this and create a new one.
|
|
|
|
port = new QSerialPort();
|
|
|
|
|
2019-12-26 08:27:14 +00:00
|
|
|
|
2018-06-25 07:34:33 +00:00
|
|
|
// TODO: The following should become arguments and/or functions
|
|
|
|
// Add signal/slot everywhere for comm port setup.
|
|
|
|
// Consider how to "re-setup" and how to save the state for next time.
|
2018-06-19 19:58:52 +00:00
|
|
|
baudrate = 115200;
|
|
|
|
stopbits = 1;
|
2018-07-04 00:20:03 +00:00
|
|
|
portName = "/dev/ttyUSB0";
|
2021-11-07 05:49:10 +00:00
|
|
|
this->PTTviaRTS = false;
|
2018-06-19 19:58:52 +00:00
|
|
|
|
|
|
|
setupComm(); // basic parameters
|
|
|
|
openPort();
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
|
2018-06-22 23:31:52 +00:00
|
|
|
//port->setReadBufferSize(1024); // manually. 256 never saw any return from the radio. why...
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
|
2018-06-22 23:31:52 +00:00
|
|
|
|
2018-06-19 19:58:52 +00:00
|
|
|
connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn()));
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:09:52 +00:00
|
|
|
commHandler::commHandler(QString portName, quint32 baudRate)
|
2018-11-16 22:08:21 +00:00
|
|
|
{
|
|
|
|
//constructor
|
|
|
|
// grab baud rate and other comm port details
|
|
|
|
// if they need to be changed later, please
|
|
|
|
// destroy this and create a new one.
|
|
|
|
|
|
|
|
port = new QSerialPort();
|
|
|
|
|
|
|
|
// TODO: The following should become arguments and/or functions
|
|
|
|
// Add signal/slot everywhere for comm port setup.
|
|
|
|
// Consider how to "re-setup" and how to save the state for next time.
|
2020-03-30 21:09:52 +00:00
|
|
|
baudrate = baudRate;
|
2018-11-16 22:08:21 +00:00
|
|
|
stopbits = 1;
|
|
|
|
this->portName = portName;
|
2021-11-06 06:21:36 +00:00
|
|
|
this->PTTviaRTS = false;
|
2018-11-16 22:08:21 +00:00
|
|
|
|
|
|
|
setupComm(); // basic parameters
|
|
|
|
openPort();
|
2021-05-15 17:53:16 +00:00
|
|
|
// qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
|
2018-11-16 22:08:21 +00:00
|
|
|
//port->setReadBufferSize(1024); // manually. 256 never saw any return from the radio. why...
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
|
2018-11-16 22:08:21 +00:00
|
|
|
|
|
|
|
connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn()));
|
2019-01-10 06:44:48 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 22:08:21 +00:00
|
|
|
|
2018-06-19 19:58:52 +00:00
|
|
|
commHandler::~commHandler()
|
|
|
|
{
|
|
|
|
this->closePort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::setupComm()
|
|
|
|
{
|
2021-02-02 09:05:59 +00:00
|
|
|
serialError = false;
|
2018-06-19 19:58:52 +00:00
|
|
|
port->setPortName(portName);
|
|
|
|
port->setBaudRate(baudrate);
|
|
|
|
port->setStopBits(QSerialPort::OneStop);// OneStop is other option
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::receiveDataFromUserToRig(const QByteArray &data)
|
|
|
|
{
|
|
|
|
sendDataOut(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::sendDataOut(const QByteArray &writeData)
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex.lock();
|
|
|
|
|
2019-12-26 08:27:14 +00:00
|
|
|
qint64 bytesWritten;
|
2021-11-07 05:49:10 +00:00
|
|
|
|
|
|
|
if(PTTviaRTS)
|
|
|
|
{
|
|
|
|
// Size: 1 2 3 4 5 6 7 8
|
|
|
|
//index: 0 1 2 3 4 5 6 7
|
|
|
|
//Query: FE FE TO FROM 0x1C 0x00 0xFD
|
|
|
|
//PTT On: FE FE TO FROM 0x1C 0x00 0x01 0xFD
|
|
|
|
//PTT Off: FE FE TO FROM 0x1C 0x00 0x00 0xFD
|
|
|
|
|
|
|
|
if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\xFD")))
|
|
|
|
{
|
|
|
|
// Query
|
2021-11-07 08:28:35 +00:00
|
|
|
//qDebug(logSerial()) << "Looks like PTT Query";
|
2021-11-07 05:49:10 +00:00
|
|
|
bool pttOn = this->rtsStatus();
|
|
|
|
QByteArray pttreturncmd = QByteArray("\xFE\xFE");
|
|
|
|
pttreturncmd.append(writeData.at(3));
|
|
|
|
pttreturncmd.append(writeData.at(2));
|
|
|
|
pttreturncmd.append(QByteArray("\x1C\x00", 2));
|
|
|
|
pttreturncmd.append((char)pttOn);
|
|
|
|
pttreturncmd.append("\xFD");
|
2021-11-07 08:28:35 +00:00
|
|
|
//qDebug(logSerial()) << "Sending fake PTT query result: " << (bool)pttOn;
|
2021-11-07 05:49:10 +00:00
|
|
|
printHex(pttreturncmd, false, true);
|
|
|
|
emit haveDataFromPort(pttreturncmd);
|
|
|
|
|
|
|
|
|
|
|
|
mutex.unlock();
|
|
|
|
return;
|
|
|
|
} else if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\x01\xFD")))
|
|
|
|
{
|
|
|
|
// PTT ON
|
2021-11-07 08:28:35 +00:00
|
|
|
//qDebug(logSerial()) << "Looks like PTT ON";
|
2021-11-07 05:49:10 +00:00
|
|
|
setRTS(true);
|
|
|
|
mutex.unlock();
|
|
|
|
return;
|
|
|
|
} else if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\x00\xFD")))
|
|
|
|
{
|
|
|
|
// PTT OFF
|
2021-11-07 08:28:35 +00:00
|
|
|
//qDebug(logSerial()) << "Looks like PTT OFF";
|
2021-11-07 05:49:10 +00:00
|
|
|
setRTS(false);
|
|
|
|
mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:27:14 +00:00
|
|
|
bytesWritten = port->write(writeData);
|
2021-05-15 17:53:16 +00:00
|
|
|
|
2021-03-31 04:54:27 +00:00
|
|
|
if(bytesWritten != (qint64)writeData.size())
|
|
|
|
{
|
|
|
|
qDebug(logSerial()) << "bytesWritten: " << bytesWritten << " length of byte array: " << writeData.length()\
|
|
|
|
<< " size of byte array: " << writeData.size()\
|
|
|
|
<< " Wrote all bytes? " << (bool) (bytesWritten == (qint64)writeData.size());
|
|
|
|
}
|
2018-06-19 19:58:52 +00:00
|
|
|
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::receiveDataIn()
|
|
|
|
{
|
|
|
|
// connected to comm port data signal
|
|
|
|
|
2018-06-25 07:34:33 +00:00
|
|
|
// Here we get a little specific to CIV radios
|
|
|
|
// because we know what constitutes a valid "frame" of data.
|
2018-06-22 23:31:52 +00:00
|
|
|
|
|
|
|
// new code:
|
|
|
|
port->startTransaction();
|
2018-06-19 19:58:52 +00:00
|
|
|
inPortData = port->readAll();
|
2021-05-21 18:35:13 +00:00
|
|
|
|
|
|
|
if(inPortData.size() == 1)
|
|
|
|
{
|
|
|
|
// Generally for baud <= 9600
|
|
|
|
if (inPortData == "\xFE")
|
|
|
|
{
|
|
|
|
// This will get hit twice.
|
|
|
|
// After the FE FE, we transition into
|
|
|
|
// the normal .startsWith FE FE block
|
|
|
|
// where the normal rollback code can handle things.
|
|
|
|
port->rollbackTransaction();
|
|
|
|
rolledBack = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-15 03:55:28 +00:00
|
|
|
|
2021-05-21 18:35:13 +00:00
|
|
|
|
2018-06-22 23:31:52 +00:00
|
|
|
if(inPortData.startsWith("\xFE\xFE"))
|
|
|
|
{
|
2021-06-15 03:55:28 +00:00
|
|
|
if(inPortData.contains("\xFC"))
|
|
|
|
{
|
|
|
|
//qInfo(logSerial()) << "Transaction contains collision data. Dumping.";
|
|
|
|
//printHex(inPortData, false, true);
|
|
|
|
port->commitTransaction();
|
|
|
|
return;
|
|
|
|
}
|
2018-06-22 23:31:52 +00:00
|
|
|
if(inPortData.endsWith("\xFD"))
|
|
|
|
{
|
|
|
|
// good!
|
|
|
|
port->commitTransaction();
|
|
|
|
emit haveDataFromPort(inPortData);
|
2018-06-25 07:34:33 +00:00
|
|
|
|
2018-06-22 23:31:52 +00:00
|
|
|
if(rolledBack)
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
// qInfo(logSerial()) << "Rolled back and was successfull. Length: " << inPortData.length();
|
2018-06-22 23:31:52 +00:00
|
|
|
//printHex(inPortData, false, true);
|
|
|
|
rolledBack = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// did not receive the entire thing so roll back:
|
2021-05-15 17:53:16 +00:00
|
|
|
// qInfo(logSerial()) << "Rolling back transaction. End not detected. Lenth: " << inPortData.length();
|
2018-06-22 23:31:52 +00:00
|
|
|
//printHex(inPortData, false, true);
|
|
|
|
port->rollbackTransaction();
|
|
|
|
rolledBack = true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-07 23:54:03 +00:00
|
|
|
port->commitTransaction(); // do not emit data, do not keep data.
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logSerial()) << "Warning: received data with invalid start. Dropping data.";
|
|
|
|
//qInfo(logSerial()) << "THIS SHOULD ONLY HAPPEN ONCE!!";
|
2018-06-22 23:31:52 +00:00
|
|
|
// THIS SHOULD ONLY HAPPEN ONCE!
|
|
|
|
|
|
|
|
// unrecoverable. We did not receive the start and must
|
|
|
|
// have missed it earlier because we did not roll back to
|
|
|
|
// preserve the beginning.
|
2018-06-25 07:34:33 +00:00
|
|
|
|
2018-06-22 23:31:52 +00:00
|
|
|
//printHex(inPortData, false, true);
|
|
|
|
|
|
|
|
}
|
2018-06-19 19:58:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 06:21:36 +00:00
|
|
|
void commHandler::setRTS(bool rtsOn)
|
|
|
|
{
|
|
|
|
bool success = port->setRequestToSend(rtsOn);
|
|
|
|
if(!success)
|
|
|
|
{
|
|
|
|
qInfo(logSerial()) << "Error, could not set RTS on port " << portName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool commHandler::rtsStatus()
|
|
|
|
{
|
|
|
|
return port->isRequestToSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::setUseRTSforPTT(bool PTTviaRTS)
|
|
|
|
{
|
|
|
|
this->PTTviaRTS = PTTviaRTS;
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:58:52 +00:00
|
|
|
void commHandler::openPort()
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
// port->open();
|
|
|
|
success = port->open(QIODevice::ReadWrite);
|
|
|
|
if(success)
|
|
|
|
{
|
2021-05-06 19:41:32 +00:00
|
|
|
port->setDataTerminalReady(false);
|
|
|
|
port->setRequestToSend(false);
|
2018-06-19 19:58:52 +00:00
|
|
|
isConnected = true;
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logSerial()) << "Opened port: " << portName;
|
2018-06-19 19:58:52 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logSerial()) << "Could not open serial port " << portName << " , please restart.";
|
2018-06-19 19:58:52 +00:00
|
|
|
isConnected = false;
|
2021-02-02 09:05:59 +00:00
|
|
|
serialError = true;
|
|
|
|
emit haveSerialPortError(portName, "Could not open port. Please restart.");
|
2018-06-19 19:58:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::closePort()
|
|
|
|
{
|
2021-03-04 07:25:01 +00:00
|
|
|
if(port)
|
|
|
|
{
|
|
|
|
port->close();
|
|
|
|
delete port;
|
|
|
|
}
|
2018-06-19 19:58:52 +00:00
|
|
|
isConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commHandler::debugThis()
|
|
|
|
{
|
2018-11-07 23:54:03 +00:00
|
|
|
// Do not use, function is for debug only and subject to change.
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logSerial()) << "comm debug called.";
|
2018-06-19 19:58:52 +00:00
|
|
|
|
|
|
|
inPortData = port->readAll();
|
|
|
|
emit haveDataFromPort(inPortData);
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:31:52 +00:00
|
|
|
|
|
|
|
void commHandler::printHex(const QByteArray &pdata, bool printVert, bool printHoriz)
|
|
|
|
{
|
2021-02-23 21:21:22 +00:00
|
|
|
qDebug(logSerial()) << "---- Begin hex dump -----:";
|
2018-06-22 23:31:52 +00:00
|
|
|
QString sdata("DATA: ");
|
|
|
|
QString index("INDEX: ");
|
|
|
|
QStringList strings;
|
|
|
|
|
|
|
|
for(int i=0; i < pdata.length(); i++)
|
|
|
|
{
|
|
|
|
strings << QString("[%1]: %2").arg(i,8,10,QChar('0')).arg((unsigned char)pdata[i], 2, 16, QChar('0'));
|
|
|
|
sdata.append(QString("%1 ").arg((unsigned char)pdata[i], 2, 16, QChar('0')) );
|
|
|
|
index.append(QString("%1 ").arg(i, 2, 10, QChar('0')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(printVert)
|
|
|
|
{
|
|
|
|
for(int i=0; i < strings.length(); i++)
|
|
|
|
{
|
2021-02-23 21:21:22 +00:00
|
|
|
qDebug(logSerial()) << strings.at(i);
|
2018-06-22 23:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(printHoriz)
|
|
|
|
{
|
2021-02-23 21:21:22 +00:00
|
|
|
qDebug(logSerial()) << index;
|
|
|
|
qDebug(logSerial()) << sdata;
|
2018-06-22 23:31:52 +00:00
|
|
|
}
|
2021-02-23 21:21:22 +00:00
|
|
|
qDebug(logSerial()) << "----- End hex dump -----";
|
2018-06-22 23:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|