From 09c89f6242ecf2495f86140a3f53ef4678f8563b Mon Sep 17 00:00:00 2001 From: SudhakarKuma Date: Wed, 25 Oct 2017 15:53:45 +0530 Subject: Updated OSP-plugin --- src/SerialCom.cpp~ | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100755 src/SerialCom.cpp~ (limited to 'src/SerialCom.cpp~') diff --git a/src/SerialCom.cpp~ b/src/SerialCom.cpp~ new file mode 100755 index 0000000..0edd712 --- /dev/null +++ b/src/SerialCom.cpp~ @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2016 Dhiraj Salian + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. + */ +#include "SerialCom.hpp" +#include +#include + +QT_USE_NAMESPACE + +/** +Constructor: +*/ +SerialCom::SerialCom(QObject *parent) + : QThread(parent), waitTime(0), quit(false) +{ +} + +/** +Destructor: +*/ +SerialCom::~SerialCom() +{ + mutex.lock(); + quit = true; + cond.wakeOne(); + mutex.unlock(); + wait(); +} + +/** +sendRequest(const QString, int, const QString): + Send a request to arduino +*/ +void SerialCom::sendRequest(const QString &port, int waitTime, const QString &request) +{ + QMutexLocker locker(&mutex); + this->port = port; + this->waitTime = waitTime; + this->request = request; + if (!isRunning()) + start(); + else + cond.wakeOne(); +} + + +void SerialCom::run() +{ + bool myPortNameChanged = false; + + mutex.lock(); + QString myPortName; + if (myPortName != port) { + myPortName = port; + myPortNameChanged = true; + } + + int myWaitTimeout = waitTime; + QString myRequest = request; + mutex.unlock(); + QSerialPort serial; + + while (!quit) { + if (myPortNameChanged) { + serial.close(); + serial.setPortName(myPortName); + + if (!serial.open(QIODevice::ReadWrite)) { + emit error(tr("Can't open %1, error code %2") + .arg(port).arg(serial.error())); + return; + } + } + QByteArray requestData = myRequest.toLocal8Bit(); + serial.write(requestData); + if (serial.waitForBytesWritten(waitTime)) { + if (serial.waitForReadyRead(myWaitTimeout)) { + QByteArray responseData = serial.readAll(); + while (serial.waitForReadyRead(10)) + responseData += serial.readAll(); + QString response(responseData); + emit this->response(response); + } else { + emit timeout(tr("Wait Read Request Timed Out %1") + .arg(QTime::currentTime().toString())); + } + } else { + emit timeout(tr("Wait Write Request Timed Out %1") + .arg(QTime::currentTime().toString())); + } + mutex.lock(); + cond.wait(&mutex); + if (myPortName != port) { + myPortName = port; + myPortNameChanged = true; + } else { + myPortNameChanged = false; + } + myWaitTimeout = waitTime; + myRequest = request; + mutex.unlock(); + } +} -- cgit