1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

Updated code for TCP connection to Flight hardware.

Now has ability to change the IP and PORT to connect to.
This is done through an options page in tools->options.
This has been tested on Windows XP and basic testing in Linux (Fedora 11).
This has not been tested with real hardware.

Currently this crashes the GCS when closing the app... something in the clean up algorithm to remove objects and the way I created them trips something up...

As a result, I have not included this plugin in plugins.pro.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@880 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
andrew 2010-06-24 04:58:19 +00:00 committed by andrew
parent 2f0fb8e5d1
commit 3d8290c3a1
8 changed files with 154 additions and 120 deletions

View File

@ -27,25 +27,18 @@
#include "TCPtelemetryconfiguration.h"
#include <QtCore/QDataStream>
#include <coreplugin/icore.h>
TCPtelemetryConfiguration::TCPtelemetryConfiguration(QString classId, const QByteArray &state, QObject *parent) :
IUAVGadgetConfiguration(classId, parent),
m_HostName("127.0.0.1"),
m_Port(1000)
{
if (state.count() > 0) {
QDataStream stream(state);
int Port;
QString HostName;
stream >> Port;
stream >> HostName;
m_Port = Port;
if (HostName != "")
m_HostName = HostName;
}
settings = Core::ICore::instance()->settings();
}
TCPtelemetryConfiguration::~TCPtelemetryConfiguration()
{
}
IUAVGadgetConfiguration *TCPtelemetryConfiguration::clone()
{
TCPtelemetryConfiguration *m = new TCPtelemetryConfiguration(this->classId());
@ -56,10 +49,39 @@ IUAVGadgetConfiguration *TCPtelemetryConfiguration::clone()
QByteArray TCPtelemetryConfiguration::saveState() const
{
QByteArray bytes;
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << m_Port;
stream << m_HostName;
return bytes;
}
void TCPtelemetryConfiguration::savesettings() const
{
settings->beginGroup(QLatin1String("TCPtelemetryconnection"));
settings->beginWriteArray("Current");
settings->setArrayIndex(0);
settings->setValue(QLatin1String("HostName"), m_HostName);
settings->setValue(QLatin1String("Port"), m_Port);
settings->endArray();
settings->endGroup();
}
void TCPtelemetryConfiguration::restoresettings()
{
settings->beginGroup(QLatin1String("TCPtelemetryconnection"));
settings->beginReadArray("Current");
settings->setArrayIndex(0);
m_HostName = (settings->value(QLatin1String("HostName"), tr("")).toString());
m_Port = (settings->value(QLatin1String("Port"), tr("")).toInt());
settings->endArray();
settings->endGroup();
}

View File

@ -30,6 +30,7 @@
#include <coreplugin/iuavgadgetconfiguration.h>
#include <QtCore/QString>
#include <QtCore/QSettings>
using namespace Core;
@ -41,12 +42,18 @@ Q_PROPERTY(int Port READ Port WRITE setPort)
public:
explicit TCPtelemetryConfiguration(QString classId, const QByteArray &state = 0, QObject *parent = 0);
virtual ~TCPtelemetryConfiguration();
QByteArray saveState() const;
IUAVGadgetConfiguration *clone();
//void savesettings(QSettings* settings) const;
//void restoresettings(QSettings* settings);
void savesettings() const;
void restoresettings();
IUAVGadgetConfiguration *clone();
QString HostName() const { return m_HostName; }
int Port() const { return m_Port; }
public slots:
void setHostName(QString HostName) { m_HostName = HostName; }
void setPort(int Port) { m_Port = Port; }
@ -54,6 +61,7 @@ public slots:
private:
QString m_HostName;
int m_Port;
QSettings* settings;
};

View File

@ -41,10 +41,14 @@ TCPtelemetryOptionsPage::TCPtelemetryOptionsPage(TCPtelemetryConfiguration *conf
IOptionsPage(parent),
m_config(config)
{
}
}
TCPtelemetryOptionsPage::~TCPtelemetryOptionsPage()
{
}
QWidget *TCPtelemetryOptionsPage::createPage(QWidget *parent)
{
m_page = new Ui::TCPtelemetryOptionsPage();
QWidget *w = new QWidget(parent);
m_page->setupUi(w);
@ -59,6 +63,12 @@ void TCPtelemetryOptionsPage::apply()
{
m_config->setPort(m_page->Port->value());
m_config->setHostName(m_page->HostName->text());
m_config->savesettings();
emit availableDevChanged();
}
void TCPtelemetryOptionsPage::finish()

View File

@ -29,6 +29,7 @@
#define TCPtelemetryOPTIONSPAGE_H
#include "coreplugin/dialogs/ioptionspage.h"
//#include <QtCore/QSettings>
class TCPtelemetryConfiguration;
@ -47,17 +48,26 @@ class TCPtelemetryOptionsPage : public IOptionsPage
Q_OBJECT
public:
explicit TCPtelemetryOptionsPage(TCPtelemetryConfiguration *config, QObject *parent = 0);
virtual ~TCPtelemetryOptionsPage();
QString id() const { return QLatin1String("settings"); }
QString trName() const { return tr("settings"); }
QString category() const { return "TCP Connection"; };
QString trCategory() const { return "TCP Connection"; };
QWidget *createPage(QWidget *parent);
void apply();
void finish();
signals:
void availableDevChanged();
public slots:
private:
TCPtelemetryConfiguration *m_config;
Ui::TCPtelemetryOptionsPage *m_page;
//QSettings* settings;
};
#endif // TCPtelemetryOPTIONSPAGE_H

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>355</width>
<width>388</width>
<height>300</height>
</rect>
</property>
@ -17,27 +17,7 @@
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Host Name/Number</string>
</property>
</widget>
</item>
<item row="2" column="1" rowspan="3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="3" rowspan="5">
<item row="0" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -50,19 +30,33 @@
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="HostName"/>
<item row="4" column="2">
<widget class="QSpinBox" name="Port">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>999999</number>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="Port"/>
</item>
<item row="1" column="0">
<item row="4" column="1">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="HostName"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Host Name/Number</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View File

@ -35,25 +35,39 @@
#include <QtCore/QtPlugin>
#include <QtGui/QMainWindow>
#include <QtGui/QMessageBox>
#include <QtNetwork/QTcpSocket>
#include <QDebug>
TCPtelemetryConnection::TCPtelemetryConnection()
{//no change from serial plugin
{
tcpSocket = new QTcpSocket(this);
//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...
m_config = new TCPtelemetryConfiguration("TCP Connection", NULL, this);
m_config->restoresettings();
m_optionspage = new TCPtelemetryOptionsPage(m_config,this);
//just signal whenever we have a device event...
QMainWindow *mw = Core::ICore::instance()->mainWindow();
QObject::connect(mw, SIGNAL(deviceChange()),
this, SLOT(onEnumerationChanged()));
QObject::connect(m_optionspage, SIGNAL(availableDevChanged()),
this, SLOT(onEnumerationChanged()));
}
TCPtelemetryConnection::~TCPtelemetryConnection()
{//no change from serial plugin
{
/* if (tcpSocket->state()>0){
tcpSocket->disconnectFromHost ();
}*/
tcpSocket->close ();
// delete(m_optionspage);
// delete(m_config);
}
void TCPtelemetryConnection::onEnumerationChanged()
@ -63,69 +77,43 @@ void TCPtelemetryConnection::onEnumerationChanged()
emit availableDevChanged(this);
}
/*bool sortPorts(const QextPortInfo &s1,const QextPortInfo &s2)
{
return s1.portName<s2.portName;
}*/
QStringList TCPtelemetryConnection::availableDevices()
{
QStringList list;
/*QList<QextPortInfo> ports = QextTCPtelemetryEnumerator::getPorts();
//sort the list by port number (nice idea from PT_Dreamer :))
qSort(ports.begin(), ports.end(),sortPorts);
foreach( QextPortInfo port, ports ) {
list.append(port.friendName);
}*/
//for the first attempt just hard code the IP and PORT
list.append((const QString )"Test OpenPilot");
list.append((const QString )m_config->HostName());
return list;
}
QIODevice *TCPtelemetryConnection::openDevice(const QString &deviceName)
{
/*QList<QextPortInfo> ports = QextTCPtelemetryEnumerator::getPorts();
foreach( QextPortInfo port, ports ) {
if(port.friendName == deviceName)
{
//we need to handle port settings here...
PortSettings set;
set.BaudRate = BAUD57600;
set.DataBits = DATA_8;
set.Parity = PAR_NONE;
set.StopBits = STOP_1;
set.FlowControl = FLOW_OFF;
set.Timeout_Millisec = 500;
#ifdef Q_OS_WIN
return new QextTCPtelemetryPort(port.portName, set);
#else
return new QextTCPtelemetryPort(port.physName, set);
#endif
}
}*/
const int Timeout = 5 * 1000;
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost((const QString )"192.168.10.77", 9100);
//if (tcpSocket->state()>0){
// tcpSocket->close();
//}
tcpSocket->connectToHost((const QString )m_config->HostName(), m_config->Port());
if (tcpSocket->waitForConnected(Timeout)) {
return tcpSocket;
}
QMessageBox msgBox;
msgBox.setText((const QString )tcpSocket->errorString ());
msgBox.exec();
return NULL;
}
void TCPtelemetryConnection::closeDevice(const QString &deviceName)
{//no change from serial plugin
//nothing to do here
{
/* if (tcpSocket->state()>0){
tcpSocket->disconnectFromHost ();
}*/
//tcpSocket->close();
}
@ -133,7 +121,7 @@ QString TCPtelemetryConnection::connectionName()
{//updated from serial plugin
return QString("TCPtelemetry port");
return QString("TCP telemetry port");
}
QString TCPtelemetryConnection::shortName()
@ -166,11 +154,8 @@ bool TCPtelemetryPlugin::initialize(const QStringList &arguments, QString *error
Q_UNUSED(arguments);
Q_UNUSED(errorString);
//m_optionspage = new TCPtelemetryOptionsPage(NULL,this);
//addAutoReleasedObject(m_optionspage);
m_connection = new TCPtelemetryConnection();
//m_factory = new TCPtelemetryFactory(this);
//addAutoReleasedObject(m_factory);
addAutoReleasedObject(m_connection->Optionspage());
return true;
}

View File

@ -31,9 +31,12 @@
#include "TCPtelemetry_global.h"
#include "TCPtelemetryoptionspage.h"
#include "TCPtelemetryconfiguration.h"
#include "TCPtelemetryfactory.h"
//#include "TCPtelemetryfactory.h"
#include "coreplugin/iconnection.h"
#include <extensionsystem/iplugin.h>
//#include <QtCore/QSettings>
class QTcpSocket;
class IConnection;
@ -57,10 +60,18 @@ public:
virtual QString connectionName();
virtual QString shortName();
TCPtelemetryConfiguration * Config() const { return m_config; }
TCPtelemetryOptionsPage * Optionspage() const { return m_optionspage; }
protected slots:
void onEnumerationChanged();
private:
QTcpSocket *tcpSocket;
TCPtelemetryConfiguration *m_config;
TCPtelemetryOptionsPage *m_optionspage;
//QSettings* settings;
};
@ -78,9 +89,6 @@ public:
virtual void extensionsInitialized();
private:
//TCPtelemetryConfiguration *m_config;
//TCPtelemetryOptionsPage *m_optionspage;
TCPtelemetryFactory *m_factory;
TCPtelemetryConnection *m_connection;
};

View File

@ -1,7 +1,7 @@
/********************************************************************************
** Form generated from reading UI file 'TCPtelemetryoptionspage.ui'
**
** Created: Tue 22. Jun 21:46:31 2010
** Created: Wed 23. Jun 00:36:03 2010
** by: Qt User Interface Compiler version 4.6.3
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
@ -28,48 +28,45 @@ class Ui_TCPtelemetryOptionsPage
{
public:
QGridLayout *gridLayout;
QLabel *label_2;
QSpacerItem *verticalSpacer;
QSpacerItem *horizontalSpacer;
QLineEdit *HostName;
QSpinBox *Port;
QLabel *label_3;
QLineEdit *HostName;
QLabel *label_2;
void setupUi(QWidget *TCPtelemetryOptionsPage)
{
if (TCPtelemetryOptionsPage->objectName().isEmpty())
TCPtelemetryOptionsPage->setObjectName(QString::fromUtf8("TCPtelemetryOptionsPage"));
TCPtelemetryOptionsPage->resize(355, 300);
TCPtelemetryOptionsPage->resize(388, 300);
gridLayout = new QGridLayout(TCPtelemetryOptionsPage);
gridLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
label_2 = new QLabel(TCPtelemetryOptionsPage);
label_2->setObjectName(QString::fromUtf8("label_2"));
gridLayout->addWidget(label_2, 0, 0, 1, 1);
verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(verticalSpacer, 2, 1, 3, 1);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 0, 3, 5, 1);
HostName = new QLineEdit(TCPtelemetryOptionsPage);
HostName->setObjectName(QString::fromUtf8("HostName"));
gridLayout->addWidget(HostName, 0, 1, 1, 1);
gridLayout->addItem(horizontalSpacer, 0, 3, 1, 1);
Port = new QSpinBox(TCPtelemetryOptionsPage);
Port->setObjectName(QString::fromUtf8("Port"));
Port->setMinimum(1);
Port->setMaximum(999999);
gridLayout->addWidget(Port, 1, 1, 1, 1);
gridLayout->addWidget(Port, 4, 2, 1, 1);
label_3 = new QLabel(TCPtelemetryOptionsPage);
label_3->setObjectName(QString::fromUtf8("label_3"));
gridLayout->addWidget(label_3, 1, 0, 1, 1);
gridLayout->addWidget(label_3, 4, 1, 1, 1);
HostName = new QLineEdit(TCPtelemetryOptionsPage);
HostName->setObjectName(QString::fromUtf8("HostName"));
gridLayout->addWidget(HostName, 0, 2, 1, 1);
label_2 = new QLabel(TCPtelemetryOptionsPage);
label_2->setObjectName(QString::fromUtf8("label_2"));
gridLayout->addWidget(label_2, 0, 1, 1, 1);
retranslateUi(TCPtelemetryOptionsPage);
@ -80,8 +77,8 @@ public:
void retranslateUi(QWidget *TCPtelemetryOptionsPage)
{
TCPtelemetryOptionsPage->setWindowTitle(QApplication::translate("TCPtelemetryOptionsPage", "Form", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("TCPtelemetryOptionsPage", "Host Name/Number", 0, QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("TCPtelemetryOptionsPage", "Port", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("TCPtelemetryOptionsPage", "Host Name/Number", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};