2011-03-29 20:25:24 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file serialplugin.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup SerialPlugin Serial Connection Plugin
|
|
|
|
* @{
|
|
|
|
* @brief Impliments serial connection to the flight hardware for Telemetry
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
2013-05-19 16:37:30 +02:00
|
|
|
* 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 3 of the License, or
|
2011-03-29 20:25:24 +02:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
2011-03-29 20:25:24 +02:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* 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.,
|
2011-03-29 20:25:24 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serialplugin.h"
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
#include <QtCore/QtPlugin>
|
2013-09-15 23:06:25 +02:00
|
|
|
#include <QMainWindow>
|
2011-03-29 20:25:24 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
|
|
SerialEnumerationThread::SerialEnumerationThread(SerialConnection *serial)
|
|
|
|
: m_serial(serial),
|
|
|
|
m_running(true)
|
2013-05-19 16:37:30 +02:00
|
|
|
{}
|
2011-03-29 20:25:24 +02:00
|
|
|
|
|
|
|
SerialEnumerationThread::~SerialEnumerationThread()
|
|
|
|
{
|
|
|
|
m_running = false;
|
2013-05-19 16:37:30 +02:00
|
|
|
// wait for the thread to terminate
|
|
|
|
if (wait(2100) == false) {
|
2011-03-29 20:25:24 +02:00
|
|
|
qDebug() << "Cannot terminate SerialEnumerationThread";
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialEnumerationThread::run()
|
|
|
|
{
|
|
|
|
QList <Core::IConnection::device> devices = m_serial->availableDevices();
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
while (m_running) {
|
|
|
|
if (!m_serial->deviceOpened()) {
|
2011-03-29 20:25:24 +02:00
|
|
|
QList <Core::IConnection::device> newDev = m_serial->availableDevices();
|
2013-05-19 16:37:30 +02:00
|
|
|
if (devices != newDev) {
|
2011-03-29 20:25:24 +02:00
|
|
|
devices = newDev;
|
|
|
|
emit enumerationChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
msleep(2000); // update available devices every two seconds (doesn't need more)
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerialConnection::SerialConnection()
|
|
|
|
: enablePolling(true), m_enumerateThread(this)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
serialHandle = NULL;
|
|
|
|
m_config = new SerialPluginConfiguration("Serial Telemetry", NULL, this);
|
2011-11-12 00:09:44 +01:00
|
|
|
m_config->restoresettings();
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
m_optionspage = new SerialPluginOptionsPage(m_config, this);
|
2011-11-12 00:09:44 +01:00
|
|
|
|
2011-03-29 20:25:24 +02:00
|
|
|
|
|
|
|
// Experimental: enable polling on all OS'es since there
|
|
|
|
// were reports that autodetect does not work on XP amongst
|
|
|
|
// others.
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// #ifdef Q_OS_WIN
|
|
|
|
////I'm cheating a little bit here:
|
|
|
|
////Knowing if the device enumeration really changed is a bit complicated
|
|
|
|
////so I just signal it whenever we have a device event...
|
|
|
|
// QMainWindow *mw = Core::ICore::instance()->mainWindow();
|
|
|
|
// QObject::connect(mw, SIGNAL(deviceChange()),
|
|
|
|
// this, SLOT(onEnumerationChanged()));
|
|
|
|
// #else
|
2011-03-29 20:25:24 +02:00
|
|
|
// Other OSes do not send such signals:
|
|
|
|
QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()),
|
|
|
|
this, SLOT(onEnumerationChanged()));
|
|
|
|
m_enumerateThread.start();
|
2013-05-19 16:37:30 +02:00
|
|
|
// #endif
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SerialConnection::~SerialConnection()
|
2013-05-19 16:37:30 +02:00
|
|
|
{}
|
2011-03-29 20:25:24 +02:00
|
|
|
|
|
|
|
void SerialConnection::onEnumerationChanged()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (enablePolling) {
|
2011-03-29 20:25:24 +02:00
|
|
|
emit availableDevChanged(this);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 23:06:25 +02:00
|
|
|
bool sortPorts(const QSerialPortInfo &s1, const QSerialPortInfo &s2)
|
2011-03-29 20:25:24 +02:00
|
|
|
{
|
2013-09-15 23:06:25 +02:00
|
|
|
return s1.portName() < s2.portName();
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList <Core::IConnection::device> SerialConnection::availableDevices()
|
|
|
|
{
|
|
|
|
QList <Core::IConnection::device> list;
|
|
|
|
|
|
|
|
if (enablePolling) {
|
2013-09-15 23:06:25 +02:00
|
|
|
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
|
2011-03-29 20:25:24 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// sort the list by port number (nice idea from PT_Dreamer :))
|
|
|
|
qSort(ports.begin(), ports.end(), sortPorts);
|
2013-09-15 23:06:25 +02:00
|
|
|
foreach(QSerialPortInfo port, ports) {
|
2013-05-19 16:37:30 +02:00
|
|
|
device d;
|
|
|
|
|
2013-09-15 23:06:25 +02:00
|
|
|
d.displayName = port.portName();
|
|
|
|
d.name = port.portName();
|
2013-05-19 16:37:30 +02:00
|
|
|
list.append(d);
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QIODevice *SerialConnection::openDevice(const QString &deviceName)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (serialHandle) {
|
2011-03-29 20:25:24 +02:00
|
|
|
closeDevice(deviceName);
|
|
|
|
}
|
2013-09-15 23:06:25 +02:00
|
|
|
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
|
|
|
|
foreach(QSerialPortInfo port, ports) {
|
|
|
|
if (port.portName() == deviceName) {
|
2013-05-19 16:37:30 +02:00
|
|
|
// we need to handle port settings here...
|
|
|
|
qDebug() << "Serial telemetry running at " << m_config->speed();
|
2013-09-15 23:06:25 +02:00
|
|
|
serialHandle = new QSerialPort(port, this);
|
|
|
|
if (serialHandle->open(QIODevice::ReadWrite)) {
|
|
|
|
if (serialHandle->setBaudRate(m_config->speed().toInt())
|
|
|
|
&& serialHandle->setDataBits(QSerialPort::Data8)
|
|
|
|
&& serialHandle->setParity(QSerialPort::NoParity)
|
|
|
|
&& serialHandle->setStopBits(QSerialPort::OneStop)
|
|
|
|
&& serialHandle->setFlowControl(QSerialPort::NoFlowControl)) {
|
|
|
|
m_deviceOpened = true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 20:25:24 +02:00
|
|
|
return serialHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialConnection::closeDevice(const QString &deviceName)
|
|
|
|
{
|
|
|
|
Q_UNUSED(deviceName);
|
2013-05-19 16:37:30 +02:00
|
|
|
// we have to delete the serial connection we created
|
|
|
|
if (serialHandle) {
|
2011-07-24 23:43:59 +02:00
|
|
|
serialHandle->deleteLater();
|
2013-05-19 16:37:30 +02:00
|
|
|
serialHandle = NULL;
|
2011-03-29 20:25:24 +02:00
|
|
|
m_deviceOpened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString SerialConnection::connectionName()
|
|
|
|
{
|
|
|
|
return QString("Serial port");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SerialConnection::shortName()
|
|
|
|
{
|
|
|
|
return QString("Serial");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
Tells the Serial plugin to stop polling for serial devices
|
2011-03-29 20:25:24 +02:00
|
|
|
*/
|
|
|
|
void SerialConnection::suspendPolling()
|
|
|
|
{
|
|
|
|
enablePolling = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
Tells the Serial plugin to resume polling for serial devices
|
2011-03-29 20:25:24 +02:00
|
|
|
*/
|
|
|
|
void SerialConnection::resumePolling()
|
|
|
|
{
|
|
|
|
enablePolling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SerialPlugin::SerialPlugin()
|
2013-05-19 16:37:30 +02:00
|
|
|
{}
|
2011-03-29 20:25:24 +02:00
|
|
|
|
|
|
|
SerialPlugin::~SerialPlugin()
|
|
|
|
{
|
2011-11-12 00:09:44 +01:00
|
|
|
removeObject(m_connection->Optionspage());
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialPlugin::extensionsInitialized()
|
|
|
|
{
|
2011-11-12 00:09:44 +01:00
|
|
|
addAutoReleasedObject(m_connection);
|
2011-03-29 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString)
|
|
|
|
{
|
|
|
|
Q_UNUSED(arguments);
|
|
|
|
Q_UNUSED(errorString);
|
2011-11-12 00:09:44 +01:00
|
|
|
m_connection = new SerialConnection();
|
2013-05-19 16:37:30 +02:00
|
|
|
// must manage this registration of child object ourselves
|
|
|
|
// if we use an autorelease here it causes the GCS to crash
|
|
|
|
// as it is deleting objects as the app closes...
|
2011-11-12 00:09:44 +01:00
|
|
|
addObject(m_connection->Optionspage());
|
2011-03-29 20:25:24 +02:00
|
|
|
return true;
|
|
|
|
}
|