qpidctrl  0.7.1
qwatlowdevice.cpp
Go to the documentation of this file.
1 #include "qwatlowdevice.h"
2 #include <QTime>
3 #include <stdio.h>
4 #include "qpiddebug.h"
5 #include "qpidcommport.h"
6 
7 QWatlowDevice::QWatlowDevice(QPidControllerWPtr ctrl, QcepSettingsSaverWPtr saver, QString deviceName) :
8  QPidDevice(ctrl, saver, WatlowDevice, deviceName, 1000, 2000),
9  m_ObjectNamer(this, "watlow"),
10  m_TimerID(0)
11 {
12 }
13 
15 {
17 
18  connect(&m_CommandTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
19 
20  m_TimerID = startTimer(2000);
21 }
22 
24 {
25  if (m_TimerID) {
26  killTimer(m_TimerID);
27  m_TimerID = 0;
28  }
29 
31 }
32 
34 {
35  QStringList channels;
36 
37  channels.append("Epoch");
38  channels.append("Day");
39  channels.append("Hours");
40  channels.append("Minutes");
41  channels.append("Seconds");
42  channels.append("Temperature");
43  channels.append("OutputLevel");
44  channels.append("SetPoint");
45 
46  emit loggedChannelNames(channels);
47  emit graphedChannelNames(channels);
48 }
49 
51 {
52 }
53 
55 {
56 }
57 
58 void QWatlowDevice::timerEvent(QTimerEvent *)
59 {
60  if (m_CommandQueue.count() > 2) {
61  if (qcepDebug(DEBUG_WATLOW)) {
62  printMessage(tr("Discarding %1 outstanding commands").arg(m_CommandQueue.count()));
63  // m_CommandQueue.clear();
64  printMessage(tr("%1 outstanding commands, skipping polling commands").arg(m_CommandQueue.count()));
65  }
66  } else {
67  getParameter("SP1");
68  getParameter("ER2");
69  getParameter("C1");
70  getParameter("PWR");
71  }
72 }
73 
75 {
76  printMessage(tr("Timeout on %1").arg(m_CommandQueue.first()));
77 
79 }
80 
82 {
83  QByteArray bytes;
84 
85  int a = commPort() -> bytesAvailable();
86 
87  bytes.resize(a);
88 
89  bytes = commPort() -> read(bytes.size());
90 
91  m_Reply.append(bytes);
92 
93  if (qcepDebug(DEBUG_WATLOW)) {
94  printMessage(tr("Bytes received '%1' (%2)").arg(bytes.data()).arg(bytes.size()));
95  }
96 
97  if (m_CommandQueue.length()) {
98  QString cmd = m_CommandQueue.first();
99 
100  if (cmd[0] == '?') {
101  if (m_Reply.endsWith(QChar(13))) {
102  completeCommand();
103  m_Reply = "";
104  }
105  } else if (cmd[0] == '=') {
106  if (m_Reply.endsWith(QChar(17))) {
107  completeCommand();
108  m_Reply = "";
109  }
110  }
111  }
112 }
113 
114 void QWatlowDevice::sendCommand(QString cmd)
115 {
116  pushCommand(cmd);
117 }
118 
120 {
121  printMessage(tr("DSR Changed %1").arg(status));
122 }
123 
124 void QWatlowDevice::pushCommand(QString cmd)
125 {
126  m_CommandQueue.append(cmd);
127 
128  if (m_CommandQueue.length() == 1) {
129  issueCommand();
130  }
131 }
132 
134 {
135  if (m_CommandQueue.length() > 0) {
136  m_CommandTimer.setSingleShot(true);
137  m_CommandTimer.start(2000);
138 
139  QString cmd = m_CommandQueue[0];
140 
141  if (cmd[0] == '=') {
142  printMessage(tr("Set: %1").arg(cmd));
143  }
144 
145  if (commPort()) {
146  if (qcepDebug(DEBUG_WATLOW)) {
147  printMessage(tr("Sent command: %1").arg(cmd));
148  }
149 
150  commPort() -> write(qPrintable(cmd+"\r"));
151  }
152  }
153 }
154 
156 {
157  if (m_CommandQueue.length() > 0) {
158  m_CommandTimer.stop();
159 
160  QString cmd = m_CommandQueue.takeFirst();
162 
163  if (cmd[0] == '?') {
164  QString parm = cmd.mid(2);
165  bool ok;
166  double val = m_Reply.mid(2).toDouble(&ok);
167 
168  if (ok) {
169  if (parm == "SP1") {
170  if (ctl) {
171  ctl->set_RampPoint(val);
172  }
173  } else if (parm == "PWR") {
174  if (ctl) {
175  ctl->set_OutputLevel(val);
176  }
177  } else if (parm == "C1") {
178  if (ctl) {
179  ctl->set_Temperature(val);
180  }
181  } else if (parm == "RATE") {
182  if (ctl) {
183  ctl->set_RampRate(val);
184  }
185  } else if (parm == "ER2") {
186  if (val != 0) {
187  printMessage(tr("ER2 = %1 : %2").arg(val).arg(errorMessage(val)));
188  }
189  }
190 
191  emit newParameterValue(cmd.mid(2), val);
192  }
193  }
194 
195  issueCommand();
196  }
197 }
198 
200 {
201  if (m_CommandQueue.length() > 0) {
202  m_CommandTimer.stop();
203 
204  QString cmd = m_CommandQueue.takeFirst();
205 
206  issueCommand();
207  }
208 }
209 
210 void QWatlowDevice::setParameter(QString param, double val)
211 {
212  pushCommand(tr("= %1 %2").arg(param).arg(val));
213 }
214 
215 void QWatlowDevice::getParameter(QString param)
216 {
217  pushCommand(tr("? %1").arg(param));
218 }
219 
221 {
222  switch (code) {
223  case 0: return "No error";
224  case 1: return "Transmit buffer overflow";
225  case 2: return "Receive buffer overflow";
226  case 3: return "Framing error";
227  case 4: return "Overrun error";
228  case 5: return "Parity error";
229  case 6: return "Talking out of turn";
230  case 7: return "Invalid reply error";
231  case 8: return "Noise error";
232  case 20: return "Command not found";
233  case 21: return "Prompt not found";
234  case 22: return "Incomplete command line";
235  case 23: return "Invalid character";
236  case 24: return "Number of chars. overflow";
237  case 25: return "Input out of limit";
238  case 26: return "Read only command";
239  case 27: return "Write allowed only";
240  case 28: return "Prompt not active";
241  case 30: return "Request to RUN invalid";
242  case 31: return "Request to HOLD invalid";
243  case 32: return "Command invalid in RUN Mode";
244  case 33: return "Command invalid in HOLD Mode";
245  case 34: return "Output 3 is not an Event";
246  case 35: return "Output 4 is not an Event";
247  case 38: return "Asterisk not allowed";
248  case 39: return "Infinite loop error";
249  default:
250  return "Unkown Error";
251  }
252 }
253 
254 void QWatlowDevice::writeSettings(QSettings *set, QString section)
255 {
256  QPidDevice::writeSettings(set, section+"/watlow");
257 
258  QcepProperty::writeSettings(this, &staticMetaObject, section+"/watlow", set);
259 }
260 
261 void QWatlowDevice::readSettings(QSettings *set, QString section)
262 {
263  QPidDevice::readSettings(set, section+"/watlow");
264 
265  QcepProperty::readSettings(this, &staticMetaObject, section+"/watlow", set);
266 }
267 
269 {
270  if (m_DeviceDialog == NULL) {
272  }
273 
274  if (m_DeviceDialog) {
275  m_DeviceDialog->show();
276  m_DeviceDialog->raise();
277  }
278 }
279 
280 void QWatlowDevice::changeSetPoint(double setPoint)
281 {
282  setParameter("SP1", setPoint);
283 }
284 
286 {
287  if (rate <= 0) {
288  setParameter("RP", 0);
289  } else {
290  setParameter("RP", 2);
291  setParameter("RATE", rate);
292  }
293 }
294 
296 {
297 }
298 
299 void QWatlowDevice::changeIntegral(double /*integ*/)
300 {
301 }
302 
303 void QWatlowDevice::changeDerivative(double /*deriv*/)
304 {
305 }
306 
307 void QWatlowDevice::enable(bool /*on*/)
308 {
309  printMessage("<i>Enable not implemented</i>");
310 }
311 
313 {
314  return true;
315 }
316 
318 {
319  setParameter("AUT", 1);
320 }
321 
322 void QWatlowDevice::writeOutput(QString /*cmd*/)
323 {
324 }
325 
326 template <class T>
327 QWatlowCommand<T>::QWatlowCommand(QWatlowDevice *device, QString cmd, T *property) :
328  m_Device(device),
329  m_Command(cmd),
330  m_Property(property)
331 {
332 }
333 
334 template <class T>
336 {
337  return m_Property;
338 }
339 
340 template<>
341 bool QWatlowCommand<void>::processReply(QString /*reply*/)
342 {
343  return false;
344 }
345 
346 template<>
348 {
349  bool ok;
350  double value = reply.toDouble(&ok);
351 
352  if (ok && m_Property) {
353  m_Property->setValue(value);
354  }
355 
356  return (ok && m_Property);
357 }
358 
359 template<>
361 {
362  bool value = reply.startsWith("On");
363 
364  if (m_Property) {
365  m_Property->setValue(value);
366  }
367 
368  return (value && m_Property);
369 }
370 
371 template <class T>
372 QString QWatlowCommand<T>::cmd() const
373 {
374  return m_Command;
375 }
376 
377 template <class T>
379 {
380  m_Device->writeOutput(cmd());
381 }
382 
384 {
385  QcepDoubleList res;
386 
388  QDateTime ts(QDateTime::currentDateTime());
389 
390  res.append(ts.toTime_t());
391  res.append(ts.date().day());
392  res.append(ts.time().hour());
393  res.append(ts.time().minute());
394  res.append(ts.time().second());
395 
396  res.append(ctl->get_Temperature());
397  res.append(ctl->get_OutputLevel());
398  res.append(ctl->get_RampPoint());
399 
400  emit graphedChannelValues(res);
401 }
402 
404 {
405  QcepDoubleList res;
406 
408  QDateTime ts(QDateTime::currentDateTime());
409 
410  res.append(ts.toTime_t());
411  res.append(ts.date().day());
412  res.append(ts.time().hour());
413  res.append(ts.time().minute());
414  res.append(ts.time().second());
415 
416  res.append(ctl->get_Temperature());
417  res.append(ctl->get_OutputLevel());
418  res.append(ctl->get_RampPoint());
419 
420  emit loggedChannelValues(res);
421 }
QStringList m_CommandQueue
Definition: qwatlowdevice.h:71
virtual void write(QString cmd)
Definition: qpiddevice.cpp:525
QWatlowCommand(QWatlowDevice *device, QString cmd, T *property)
QPidControllerWPtr controller() const
Definition: qpiddevice.cpp:316
void completeCommand()
virtual void writeSettings(QSettings *set, QString section)
Definition: qpiddevice.cpp:366
QWeakPointer< QPidController > QPidControllerWPtr
void readoutTimeout()
QString errorMessage(int val)
void graphedChannelValues(QcepDoubleList values)
QSharedPointer< QWatlowDeviceDialog > QWatlowDeviceDialogPtr
QSharedPointer< QPidController > QPidControllerPtr
void graphedChannelNames(QStringList names)
QTimer m_CommandTimer
Definition: qwatlowdevice.h:72
void onDsrChanged(bool status)
void loggedChannelNames(QStringList names)
void sendCommand(QString cmd)
QWatlowDevice(QPidControllerWPtr ctrl, QcepSettingsSaverWPtr saver, QString deviceName)
void changeIntegral(double integ)
void changeRampRate(double rate)
bool replyExpected() const
virtual int bytesAvailable()
Definition: qpiddevice.cpp:540
QPidCommPortPtr commPort()
Definition: qpiddevice.cpp:121
void writeOutput(QString cmd)
void onGraphingTimeout()
void configurationDialog()
void changeProportional(double prop)
virtual void writeSettings(QSettings *set, QString section)
void setParameter(QString param, double val)
void changeSetPoint(double setPoint)
virtual void readSettings(QSettings *set, QString section)
Definition: qpiddevice.cpp:375
virtual void start()
Definition: qpiddevice.cpp:139
void pushCommand(QString cmd)
virtual void stop()
Definition: qpiddevice.cpp:189
QSharedPointer< QWatlowDevice > QWatlowDevicePtr
virtual void readSettings(QSettings *set, QString section)
void enable(bool on)
void readoutPolling()
QString cmd() const
bool processReply(QString reply)
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
Definition: qpiddevice.cpp:321
void readoutInitial()
QString deviceName
Definition: qpiddevice.h:133
QString m_Reply
Definition: qwatlowdevice.h:74
void timerEvent(QTimerEvent *)
QPidObjectNamer m_ObjectNamer
Definition: qwatlowdevice.h:68
void newParameterValue(QString param, double value)
void loggedChannelValues(QcepDoubleList values)
void getParameter(QString param)
QWatlowDeviceDialogPtr m_DeviceDialog
Definition: qwatlowdevice.h:75
void changeDerivative(double deriv)
void onLoggingTimeout()