qpidctrl  0.7.1
qpidcommport.cpp
Go to the documentation of this file.
1 #include "qpidcommport.h"
2 #include "qpidcontroller.h"
3 #include "qextserialport.h"
4 #include <QTcpSocket>
5 #include "qpiddebug.h"
6 #include "qextserialenumerator.h"
7 
8 QPidCommPort::QPidCommPort(QcepSettingsSaverPtr saver, QPidControllerPtr ctl) :
9  QObject(),
10  m_CommPortType(saver, this, "commPortType", 0, "Comm Port Type"),
11  m_SerialPortName(saver, this, "serialPortName", "", "Serial Port Name"),
12  m_SerialPortBaud(saver, this, "serialPortBaud", 0, "Serial Port Baud Rate"),
13  m_SerialPortBits(saver, this, "serialPortBits", 1, "Serial Port Data Bits"),
14  m_SerialPortParity(saver, this, "serialPortParity", 0, "Serial Port Parity"),
15  m_SerialPortStopBits(saver, this, "serialPortStopBits", 0, "Serial Port Stop Bits"),
16  m_SerialPortFlowControl(saver, this, "serialPortFlowControl", 0, "Serial Port Flow Control"),
17  m_SocketHostAddress(saver, this, "socketHostAddress", "", "Socket Host Address"),
18  m_SocketPortNumber(saver, this, "socketPortNumber", 0, "Socket Port Number"),
19  m_SocketTimeout(saver, this, "socketTimeout", 5000, "Socket timeout (msec)"),
20  m_EpicsPVName(saver, this, "epicsPVName", "", "Epics PV Name"),
21  m_Connected(QcepSettingsSaverWPtr(), this, "connected", 0, "Is comm port connected"),
22  m_Successful(QcepSettingsSaverWPtr(), this, "successful", 0, "Successful communication taken place"),
23  m_Controller(ctl)
24 {
25  if (qcepDebug(DEBUG_CONSTRUCTORS)) {
26  printf("QPidCommPort::QPidCommPort(%p)\n", this);
27  }
28 }
29 
31 {
32  if (qcepDebug(DEBUG_CONSTRUCTORS)) {
33  printf("QPidCommPort::~QPidCommPort(%p)\n", this);
34  }
35 }
36 
38 {
39  QStringList res;
40 
41  res << "No Port"
42  << "Local Serial Port"
43  << "Network Socket"
44  << "Epics PV";
45 
46  return res;
47 }
48 
50 {
51  QStringList res;
52 
53  res << "1200 Baud"
54  << "2400 Baud"
55  << "4800 Baud"
56  << "9600 Baud"
57  << "19200 Baud";
58 
59  return res;
60 }
61 
62 BaudRateType QPidCommPort::baudRate(int idx)
63 {
64  switch(idx) {
65  case 0:
66  return BAUD1200;
67 
68  case 1:
69  return BAUD2400;
70 
71  case 2:
72  return BAUD4800;
73 
74  case 3:
75  return BAUD9600;
76 
77  case 4:
78  return BAUD19200;
79 
80  default:
81  return BAUD9600;
82  }
83 }
84 
86 {
87  QStringList res;
88 
89  res << "7 Bits"
90  << "8 Bits";
91 
92  return res;
93 }
94 
95 DataBitsType QPidCommPort::dataBits(int idx)
96 {
97  switch (idx) {
98  case 0:
99  return DATA_7;
100 
101  case 1:
102  return DATA_8;
103 
104  default:
105  return DATA_8;
106  }
107 }
108 
110 {
111  QStringList res;
112 
113  res << "None"
114  << "Hardware"
115  << "XON/XOFF";
116 
117  return res;
118 }
119 
120 FlowType QPidCommPort::flowControl(int idx)
121 {
122  switch (idx) {
123  case 0:
124  return FLOW_OFF;
125 
126  case 1:
127  return FLOW_HARDWARE;
128 
129  case 2:
130  return FLOW_XONXOFF;
131 
132  default:
133  return FLOW_OFF;
134  }
135 }
136 
138 {
139  QStringList res;
140 
141  res << "None"
142  << "Odd"
143  << "Even"
144  << "Space";
145 
146  return res;
147 }
148 
149 ParityType QPidCommPort::parity(int idx)
150 {
151  switch(idx) {
152  case 0:
153  return PAR_NONE;
154 
155  case 1:
156  return PAR_ODD;
157 
158  case 2:
159  return PAR_EVEN;
160 
161  case 3:
162  return PAR_SPACE;
163 
164  default:
165  return PAR_NONE;
166  }
167 }
168 
170 {
171  QStringList res;
172 
173  res << "1 Bits"
174  << "2 Bits";
175 
176  return res;
177 }
178 
179 StopBitsType QPidCommPort::stopBits(int idx)
180 {
181  switch(idx) {
182  case 0:
183  return STOP_1;
184 
185  case 1:
186  return STOP_2;
187 
188  default:
189  return STOP_1;
190  }
191 }
192 
193 void QPidCommPort::printMessage(QString msg, QDateTime ts)
194 {
196 
197  if (ctl) {
198  ctl->printMessage(msg, ts);
199  }
200 }
201 
202 void QPidCommPort::writeSettings(QSettings *set, QString section)
203 {
204  QcepProperty::writeSettings(this, &staticMetaObject, section, set);
205 }
206 
207 void QPidCommPort::readSettings(QSettings *set, QString section)
208 {
209  QcepProperty::readSettings(this, &staticMetaObject, section, set);
210 }
211 
213 {
214  if (openIfNeeded()) {
215  QByteArray res = m_IODevice->readAll();
216 
217  set_Successful(res.count());
218 
219  if (qcepDebug(DEBUG_COMMPORT)) {
220  if (qcepDebug(DEBUG_LEVEL1)) {
221  printMessage(tr("QPidCommPort::readAll: %1 (%2)").arg((QString) res).arg((QString) res.toHex()));
222  } else {
223  printMessage(tr("QPidCommPort::readAll: %1").arg((QString) res));
224  }
225  }
226  return res;
227  } else {
228  return QByteArray();
229  }
230 }
231 
232 QByteArray QPidCommPort::read(int sz)
233 {
234  if (openIfNeeded()) {
235  QByteArray res = m_IODevice->read(sz);
236 
237  set_Successful(res.count());
238 
239  if (qcepDebug(DEBUG_COMMPORT)) {
240  if (qcepDebug(DEBUG_LEVEL1)) {
241  printMessage(tr("QPidCommPort::read(%1): %2 (%3)").arg(sz).arg((QString) res).arg((QString) res.toHex()));
242  } else {
243  printMessage(tr("QPidCommPort::read(%1): %2").arg(sz).arg((QString) res));
244  }
245  }
246 
247  return res;
248  } else {
249  return QByteArray();
250  }
251 }
252 
253 QByteArray QPidCommPort::readLine(int maxlen)
254 {
255  if (openIfNeeded()) {
256  QByteArray res = m_IODevice->readLine(maxlen);
257 
258  set_Successful(res.count());
259 
260  if (qcepDebug(DEBUG_COMMPORT)) {
261  if (qcepDebug(DEBUG_LEVEL1)) {
262  printMessage(tr("QPidCommPort::readLine(%1): %2 (%3)").arg(maxlen).arg((QString) res).arg((QString) res.toHex()));
263  } else {
264  printMessage(tr("QPidCommPort::readLine(%1): %2").arg(maxlen).arg((QString) res));
265  }
266  }
267 
268  return res;
269  } else {
270  return QByteArray();
271  }
272 }
273 
275 {
276  if (openIfNeeded()) {
277  bool res = m_IODevice->isWritable();
278 
279  if (qcepDebug(DEBUG_COMMPORT) && qcepDebug(DEBUG_LEVEL1)) {
280  printMessage(tr("QPidCommPort::isWritable(): %1").arg(res));
281  }
282  return res;
283  } else {
284  return false;
285  }
286 }
287 
289 {
290  return get_Successful();
291 }
292 
293 void QPidCommPort::write(QByteArray msg)
294 {
295  if (openIfNeeded()) {
296  m_IODevice->write(msg);
297 
298  if (qcepDebug(DEBUG_COMMPORT)) {
299  if (qcepDebug(DEBUG_LEVEL1)) {
300  printMessage(tr("QPidCommPort::write(%1 (%3))").arg((QString) msg).arg((QString) msg.toHex()));
301  } else {
302  printMessage(tr("QPidCommPort::write(%1)").arg((QString) msg));
303  }
304  }
305  }
306 }
307 
309 {
310  if (openIfNeeded()) {
311  int res = m_IODevice->waitForBytesWritten(tmout);
312 
313  if (qcepDebug(DEBUG_COMMPORT) && qcepDebug(DEBUG_LEVEL1)) {
314  printMessage(tr("QPidCommPort::waitForBytesWritten(%1): %2").arg(tmout).arg(res));
315  }
316 
317  return res;
318  } else {
319  return false;
320  }
321 }
322 
324 {
325  if (openIfNeeded()) {
326  int res = m_IODevice->waitForReadyRead(tmout);
327 
328  set_Successful(res);
329 
330  if (qcepDebug(DEBUG_COMMPORT) && qcepDebug(DEBUG_LEVEL1)) {
331  printMessage(tr("QPidCommPort::waitForReadyRead(%1): %2").arg(tmout).arg(res));
332  }
333 
334  return res;
335  } else {
336  return false;
337  }
338 }
339 
341 {
342  if (openIfNeeded()) {
343  int res = m_IODevice->bytesAvailable();
344 
345  if (qcepDebug(DEBUG_COMMPORT) && qcepDebug(DEBUG_LEVEL1)) {
346  printMessage(tr("QPidCommPort::bytesAvailable(): %1").arg(res));
347  }
348 
349  return res;
350  } else {
351  return 0;
352  }
353 }
354 
356 {
357  if (openIfNeeded()) {
358  int res = m_IODevice->canReadLine();
359 
360  if (qcepDebug(DEBUG_COMMPORT) && qcepDebug(DEBUG_LEVEL1)) {
361  printMessage(tr("QPidCommPort::canReadLine(): %1").arg(res));
362  }
363 
364  return res;
365  } else {
366  return 0;
367  }
368 }
369 
371 {
372  bool res;
373 
374  QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
375 
376  QString portName = get_SerialPortName();
377  QString deviceName;
378 
379  for (int i=0; i<ports.count(); i++) {
380  QextPortInfo port = ports.value(i);
381 
382  QString aPortName = port.portName;
383 
384  if (portName == aPortName) {
385  deviceName = port.physName;
386  }
387  }
388 
389  QextSerialPort *dev = new QextSerialPort(deviceName, QextSerialPort::Polling);
390 
391  dev->setBaudRate(baudRate(get_SerialPortBaud()));
392  dev->setDataBits(dataBits(get_SerialPortBits()));
393  dev->setStopBits(stopBits(get_SerialPortStopBits()));
394  dev->setFlowControl(flowControl(get_SerialPortFlowControl()));
395  dev->setParity(parity(get_SerialPortParity()));
396 
397  res = dev->open(QIODevice::ReadWrite);
398 
399  if (qcepDebug(DEBUG_SERIAL)) {
400  printMessage(tr("New serial port on device %1, port name %2").arg(deviceName).arg(portName));
401 
402  if (qcepDebug(DEBUG_LEVEL1)) {
403  printMessage(tr("Baud:%1 Bits:%2 Parity:%3 Stop:%4 HShake:%5")
404  .arg(dev->baudRate())
405  .arg(dev->dataBits())
406  .arg(dev->parity())
407  .arg(dev->stopBits())
408  .arg(dev->flowControl()));
409  }
410  }
411 
412 // dev->setQueryMode(QextSerialPort::EventDriven);
413 
414  if (!res) {
415  printMessage(tr("Serial Port open failed %1").arg(dev->errorString()));
416  }
417 
418  m_IODevice = dev;
419 
420  return res;
421 }
422 
424 {
425  bool res;
426 
427  QTcpSocket *sock = new QTcpSocket();
428  sock->connectToHost(get_SocketHostAddress(), get_SocketPortNumber());
429  res = sock->waitForConnected(get_SocketTimeout());
430  m_IODevice = sock;
431 
432  if (!res) {
433  printMessage(tr("Socket connection failed %1").arg(sock->errorString()));
434  }
435 
436  return res;
437 }
438 
440 {
441  if (get_Connected()) {
442  return true;
443  } else {
444  bool res = false;
445 
446  switch (get_CommPortType()) {
447  case LocalSerialPort:
448  res = openLocalSerialPort();
449 
450  if (qcepDebug(DEBUG_SERIAL)) {
451  printMessage(tr("Serial Port Opened = %1").arg(res));
452  }
453  break;
454 
455  case NetworkSocket:
456  res = openNetworkSocket();
457 
458  if (qcepDebug(DEBUG_SOCKET)) {
459  printMessage(tr("Socket opened: connected = %1").arg(res));
460  }
461  break;
462  }
463 
464  set_Connected(res);
465 
466  return res;
467  }
468 }
DataBitsType dataBits(int idx)
QStringList commPortTypes()
bool openLocalSerialPort()
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
int bytesAvailable()
QStringList commPortDataBits()
QByteArray readLine(int maxlen=0)
BaudRateType baudRate(int idx)
QByteArray readAll()
QSharedPointer< QPidController > QPidControllerPtr
StopBitsType stopBits(int idx)
int waitForBytesWritten(int tmout=0)
QByteArray read(int sz)
QPidControllerWPtr m_Controller
Definition: qpidcommport.h:108
virtual ~QPidCommPort()
void write(QByteArray msg)
QPidCommPort(QcepSettingsSaverPtr saver, QPidControllerPtr ctl)
Definition: qpidcommport.cpp:8
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QStringList commPortParitys()
virtual void writeSettings(QSettings *set, QString section)
virtual void readSettings(QSettings *set, QString section)
QStringList commPortStopBits()
bool successfullyConnected()
bool openNetworkSocket()
int waitForReadyRead(int tmout=0)
QStringList commPortBaudRates()
FlowType flowControl(int idx)
ParityType parity(int idx)
QStringList commPortFlowControls()