qpidctrl  0.7.1
qptc10simulator.cpp
Go to the documentation of this file.
1 #include "qptc10simulator.h"
2 #include <stdio.h>
3 #include <QWaitCondition>
4 #include <QMutex>
5 #include "qptc10device.h"
6 #include "qpiddebug.h"
7 
9  QPidSimulator(NULL),
10  m_Device(device),
11  m_OutputLevel("75"),
12  m_Temperature("23"),
13  m_SetPoint("42"),
14  m_RampPoint("42"),
15  m_RampRate("10"),
16  m_CtrlMode("Off"),
17  m_Enabled("Off"),
18  m_LoopEnabled("Off")
19 {
20 }
21 
23 {
24 }
25 
26 qint64 QPTC10Simulator::readData(char *data, qint64 maxlen)
27 {
28  if (m_Replies.count() > 0) {
29  strncpy(data, qPrintable(m_Replies.takeFirst()), maxlen);
30 
31  return strlen(data);
32  } else {
33  return -1;
34  }
35 }
36 
37 qint64 QPTC10Simulator::readLineData(char *data, qint64 maxlen)
38 {
39  return readData(data, maxlen);
40 }
41 
42 qint64 QPTC10Simulator::writeData(const char *data, qint64 len)
43 {
44  QString cmd(data);
45 
46  if (qcepDebug(DEBUG_PTC10SIM)) {
47  printMessage(tr("QPTC10Simulator::writeData %1, %2, %3").arg(data).arg(strlen(data)).arg(len));
48  }
49 
50  if (cmd == "\"Control.PID.Mode?\"\n") {
52  } else if (cmd == "\"Control.PID.Setpoint?\"\n") {
54  } else if (cmd == "\"Control.PID.RampT?\"\n") {
56  } else if (cmd == "\"Control.PID.Ramp?\"\n") {
58  } else if (cmd == "outputEnable?\n") {
60  } else if (cmd == "\"Control?\"\n") {
62  } else if (cmd == "\"TC?\"\n") {
64  } else if (cmd == "getOutput\n") {
65  reply(tr("%1, %2, 0, 0").arg(m_OutputLevel).arg(m_Temperature));
66  } else if (cmd == "getOutput.names\n") {
67  reply("Control, TC, Chan C, Chan D");
68  } else if (cmd == "getOutput.units\n") {
69  reply("%, degC, V, W");
70  } else if (cmd == "system.com.verbose medium\n") {
71  } else if (cmd == "\"TC.PID.Mode?\"\n") {
72  reply("Error: non-exist");
73  } else if (cmd == "\"Chan C.PID.Mode?\"\n") {
74  reply("Error: non-exist");
75  } else if (cmd == "\"Chan D.PID.Mode?\"\n") {
76  reply("Off");
77  } else {
78  QRegExp regex("\"Control\\.PID\\.Setpoint\"\\s+(\\S*)\\n");
79 
80  if (regex.exactMatch(cmd)) {
81  m_SetPoint = regex.capturedTexts()[1];
82 
83  return len;
84  } else {
85  QRegExp regex("\"Control\\.PID\\.Ramp\"\\s+(\\S*)\\n");
86 
87  if (regex.exactMatch(cmd)) {
88  m_RampRate = regex.capturedTexts()[1];
89 
90  return len;
91  } else {
92  QRegExp regex("\"Control\\.PID\\.Mode\"\\s+(\\S*)\\n");
93 
94  if (regex.exactMatch(cmd)) {
95  m_LoopEnabled = regex.capturedTexts()[1];
96 
97  return len;
98  } else {
99  QRegExp regex("outputEnable\\s+(\\S*)\\n");
100 
101  if (regex.exactMatch(cmd)) {
102  m_Enabled = regex.capturedTexts()[1];
103 
104  return len;
105  }
106  }
107  }
108  }
109 
110  printMessage(tr("Unrecognized PTC10 Command %1").arg(cmd));
111 
112  reply(tr("Error: %1").arg(cmd));
113  }
114 
115  return len;
116 }
117 
118 void QPTC10Simulator::reply(QString rply)
119 {
120  if (qcepDebug(DEBUG_PTC10SIM)) {
121  printMessage(tr("QPTC10Simulator::reply %1").arg(rply));
122  }
123 
124  m_Replies.append(rply+"\n");
125 
126  emit readyRead();
127 }
128 
130 {
131  if (qcepDebug(DEBUG_PTC10SIM)) {
132  printMessage(tr("QPTC10Simulator::canReadLine %1").arg(m_Replies.count()));
133  }
134 
135  return m_Replies.count();
136 }
137 
139 {
140  if (m_Replies.count()) {
141  return true;
142  } else {
143  QWaitCondition waitCondition;
144  QMutex mutex;
145 
146  waitCondition.wait(&mutex, msecs);
147 
148  return m_Replies.count();
149  }
150 }
151 
152 void QPTC10Simulator::printMessage(QString msg, QDateTime ts) const
153 {
155 
156  if (dev) {
157  dev->printMessage(msg, ts);
158  }
159 }
QWeakPointer< QPTC10Device > QPTC10DeviceWPtr
bool canReadLine() const
qint64 writeData(const char *data, qint64 len)
bool waitForReadyRead(int msecs)
QPTC10Simulator(QPTC10DeviceWPtr device)
QStringList m_Replies
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
QSharedPointer< QPTC10Device > QPTC10DevicePtr
void reply(QString rply)
qint64 readLineData(char *data, qint64 maxlen)
virtual ~QPTC10Simulator()
qint64 readData(char *data, qint64 maxlen)
QPTC10DeviceWPtr m_Device