Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / experimental / USB_UPLOAD_TOOL / SSP / port.cpp
blob23700fb46a30e7123a1c472c88c30cc3537ee968
1 /**
2 ******************************************************************************
4 * @file port.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup Uploader Serial and USB Uploader Plugin
10 * @{
11 * @brief The USB and Serial protocol uploader plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "port.h"
30 #include <QSerialPort>
31 #include <QDebug>
33 port::port(QString name, bool debug) : mstatus(port::closed), debug(debug)
35 timer.start();
36 sport = new QSerialPort(name, this);
37 if (sport->open(QIODevice::ReadWrite)) {
38 if (sport->setBaudRate(QSerialPort::Baud57600)
39 && sport->setDataBits(QSerialPort::Data8)
40 && sport->setParity(QSerialPort::NoParity)
41 && sport->setStopBits(QSerialPort::OneStop)
42 && sport->setFlowControl(QSerialPort::NoFlowControl)) {
43 mstatus = port::open;
45 } else {
46 qDebug() << sport->error();
47 mstatus = port::error;
51 port::~port()
53 sport->close();
56 port::portstatus port::status()
58 return mstatus;
61 int16_t port::pfSerialRead(void)
63 char c[1];
65 // TODO why the wait ? (gcs uploader dfu does not have it)
66 sport->waitForBytesWritten(1);
67 if (sport->bytesAvailable() || sport->waitForReadyRead(0)) {
68 sport->read(c, 1);
69 if (debug) {
70 if (((uint8_t)c[0]) == 0xe1 || rxDebugBuff.count() > 50) {
71 qDebug() << "PORT R " << rxDebugBuff.toHex();
72 rxDebugBuff.clear();
74 rxDebugBuff.append(c[0]);
76 } else {
77 return -1;
79 return (uint8_t)c[0];
82 void port::pfSerialWrite(uint8_t c)
84 char cc[1];
86 cc[0] = c;
87 sport->write(cc, 1);
88 if (debug) {
89 if (((uint8_t)cc[0]) == 0xe1 || rxDebugBuff.count() > 50) {
90 qDebug() << "PORT T " << txDebugBuff.toHex();
91 txDebugBuff.clear();
93 txDebugBuff.append(cc[0]);
95 // TODO why the wait ? (gcs uploader dfu does not have it)
96 sport->waitForBytesWritten(1);
99 uint32_t port::pfGetTime(void)
101 return timer.elapsed();