dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
serialplugin.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "serialplugin.h"
28 #include "serialdevice.h"
29 
31 #include <coreplugin/icore.h>
32 
33 #include <QtCore/QtPlugin>
34 #include <QMainWindow>
35 #include <coreplugin/icore.h>
36 #include <QDebug>
37 #include <QFontMetrics>
38 
40  : enablePolling(true)
41  , m_deviceOpened(false)
42 {
43  serialHandle = NULL;
44  m_config = new SerialPluginConfiguration("Serial Telemetry", NULL, this);
45  m_config->restoresettings();
46 
47  m_optionspage = new SerialPluginOptionsPage(m_config, this);
48 
49  connect(&periodicTimer, &QTimer::timeout, this, &SerialConnection::periodic);
50  periodicTimer.start(1000);
51 }
52 
54 {
55 }
56 
58 {
59 }
60 
61 bool sortPorts(const QSerialPortInfo &s1, const QSerialPortInfo &s2)
62 {
63  return s1.portName() < s2.portName();
64 }
65 
66 void SerialConnection::periodic()
67 {
68  if (!this->deviceOpened()) {
70 
71  // Ignore the output, as now availableDevices signals!
72  }
73 }
74 
76 {
77  static QList<Core::IDevice *> m_available_device_list;
78  if (enablePolling) {
79  bool changed = false;
80 
81  QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
82 
83  // sort the list by port number (nice idea from PT_Dreamer :))
84  std::sort(ports.begin(), ports.end(), sortPorts);
85  bool port_exists;
86  foreach (QSerialPortInfo port, ports) {
87  port_exists = false;
88  foreach (IDevice *device, m_available_device_list) {
89  if (device->getName() == port.portName()) {
90  port_exists = true;
91  break;
92  }
93  }
94  if (!port_exists) {
95  SerialDevice *d = new SerialDevice();
96  QStringList disp;
97  if (port.description().length()) {
98  QFontMetrics font((QFont()));
99  disp.append(font.elidedText(port.description(), Qt::ElideRight, 150));
100  }
101  disp.append(port.portName());
102  d->setDisplayName(disp.join(" - "));
103  d->setName(port.portName());
104  m_available_device_list.append(d);
105 
106  changed = true;
107  }
108  }
109  foreach (IDevice *device, m_available_device_list) {
110  port_exists = false;
111  foreach (QSerialPortInfo port, ports) {
112  if (device->getName() == port.portName()) {
113  port_exists = true;
114  break;
115  }
116  }
117  if (!port_exists) {
118  m_available_device_list.removeOne(device);
119  device->deleteLater();
120 
121  changed = true;
122  }
123  }
124 
125  if (changed) {
126  emit availableDevChanged(this);
127  }
128  }
129 
130  return m_available_device_list;
131 }
132 
133 QIODevice *SerialConnection::openDevice(IDevice *deviceName)
134 {
135  if (serialHandle) {
136  closeDevice(deviceName->getName());
137  }
138  QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
139  foreach (QSerialPortInfo port, ports) {
140  if (port.portName() == deviceName->getName()) {
141  // we need to handle port settings here...
142 
143  serialHandle = new QSerialPort(port);
144  if (serialHandle->open(QIODevice::ReadWrite)) {
145  if (serialHandle->setBaudRate(m_config->speed().toInt())
146  && serialHandle->setDataBits(QSerialPort::Data8)
147  && serialHandle->setParity(QSerialPort::NoParity)
148  && serialHandle->setStopBits(QSerialPort::OneStop)
149  && serialHandle->setFlowControl(QSerialPort::NoFlowControl)) {
150  m_deviceOpened = true;
151  }
152  }
153  return serialHandle;
154  }
155  }
156  return NULL;
157 }
158 
159 void SerialConnection::closeDevice(const QString &deviceName)
160 {
161  Q_UNUSED(deviceName);
162  // we have to delete the serial connection we created
163  if (serialHandle) {
164  serialHandle->deleteLater();
165  serialHandle = NULL;
166  m_deviceOpened = false;
167  }
168 }
169 
171 {
172  return QString("Serial port");
173 }
174 
176 {
177  return QString("Serial");
178 }
179 
184 {
185  enablePolling = false;
186 }
187 
192 {
193  enablePolling = true;
194 }
195 
196 SerialPlugin::SerialPlugin()
197 {
198 }
199 
200 SerialPlugin::~SerialPlugin()
201 {
202  removeObject(m_connection->Optionspage());
203 }
204 
205 void SerialPlugin::extensionsInitialized()
206 {
207  addAutoReleasedObject(m_connection);
208 }
209 
210 bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString)
211 {
212  Q_UNUSED(arguments);
213  Q_UNUSED(errorString);
214  m_connection = new SerialConnection();
215  // must manage this registration of child object ourselves
216  // if we use an autorelease here it causes the GCS to crash
217  // as it is deleting objects as the app closes...
218  addObject(m_connection->Optionspage());
219  return true;
220 }
void availableDevChanged(IConnection *)
s1
Definition: OPPlots.m:19
void addObject(QObject *obj)
Definition: iplugin.cpp:291
virtual ~SerialConnection()
virtual QString shortName()
The SerialDevice class.
Definition: serialdevice.h:37
QTimer periodicTimer
Definition: serialplugin.h:83
void setName(QString theName)
Definition: idevice.h:50
virtual void closeDevice(const QString &deviceName)
bool sortPorts(const QSerialPortInfo &s1, const QSerialPortInfo &s2)
SerialPluginOptionsPage * Optionspage() const
Definition: serialplugin.h:66
void onEnumerationChanged()
virtual QIODevice * openDevice(Core::IDevice *deviceName)
void setDisplayName(QString dn)
Definition: idevice.h:52
virtual QList< Core::IDevice * > availableDevices()
s2
Definition: OPPlots.m:20
void removeObject(QObject *obj)
Definition: iplugin.cpp:317
virtual void suspendPolling()
virtual QString connectionName()
void addAutoReleasedObject(QObject *obj)
Definition: iplugin.cpp:306
virtual void resumePolling()
end for as it was simply a dummy placeholder disp(['Unknown object ID:0x'dec2hex(unknownObjIDList(i, 1), 8) 'appeared 'int2str(unknownObjIDList(i, 2)) 'times.'])