qpidctrl  0.7.1
Public Types | Public Slots | Signals | Public Member Functions | Properties | Private Member Functions | Private Attributes | List of all members
QPidCommPort Class Reference

#include <qpidcommport.h>

Inheritance diagram for QPidCommPort:
Inheritance graph

Public Types

enum  { NoPort, LocalSerialPort, NetworkSocket, EpicsPV }
 

Public Slots

QStringList commPortTypes ()
 
QStringList commPortBaudRates ()
 
QStringList commPortDataBits ()
 
QStringList commPortFlowControls ()
 
QStringList commPortParitys ()
 
QStringList commPortStopBits ()
 
BaudRateType baudRate (int idx)
 
DataBitsType dataBits (int idx)
 
FlowType flowControl (int idx)
 
StopBitsType stopBits (int idx)
 
ParityType parity (int idx)
 
void printMessage (QString msg, QDateTime ts=QDateTime::currentDateTime())
 

Signals

void readyRead ()
 

Public Member Functions

 QPidCommPort (QcepSettingsSaverPtr saver, QPidControllerPtr ctl)
 
virtual ~QPidCommPort ()
 
virtual void writeSettings (QSettings *set, QString section)
 
virtual void readSettings (QSettings *set, QString section)
 
QByteArray readAll ()
 
QByteArray read (int sz)
 
QByteArray readLine (int maxlen=0)
 
bool isWritable ()
 
void write (QByteArray msg)
 
int waitForBytesWritten (int tmout=0)
 
int waitForReadyRead (int tmout=0)
 
int bytesAvailable ()
 
int canReadLine ()
 
bool successfullyConnected ()
 

Properties

int commPortType
 
QString serialPortName
 
int serialPortBaud
 
int serialPortBits
 
int serialPortParity
 
int serialPortStopBits
 
int serialPortFlowControl
 
QString socketHostAddress
 
int socketPortNumber
 
int socketTimeout
 
QString epicsPVName
 
int connected
 
int successful
 

Private Member Functions

bool openIfNeeded ()
 
bool openLocalSerialPort ()
 
bool openNetworkSocket ()
 

Private Attributes

QPidControllerWPtr m_Controller
 
QPointer< QIODevice > m_IODevice
 

Detailed Description

Definition at line 14 of file qpidcommport.h.

Member Enumeration Documentation

anonymous enum
Enumerator
NoPort 
LocalSerialPort 
NetworkSocket 
EpicsPV 

Definition at line 21 of file qpidcommport.h.

Constructor & Destructor Documentation

QPidCommPort::QPidCommPort ( QcepSettingsSaverPtr  saver,
QPidControllerPtr  ctl 
)
explicit

Definition at line 8 of file qpidcommport.cpp.

8  :
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 }
QPidControllerWPtr m_Controller
Definition: qpidcommport.h:108
QPidCommPort::~QPidCommPort ( )
virtual

Definition at line 30 of file qpidcommport.cpp.

31 {
32  if (qcepDebug(DEBUG_CONSTRUCTORS)) {
33  printf("QPidCommPort::~QPidCommPort(%p)\n", this);
34  }
35 }

Member Function Documentation

BaudRateType QPidCommPort::baudRate ( int  idx)
slot

Definition at line 62 of file qpidcommport.cpp.

Referenced by openLocalSerialPort().

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 }
int QPidCommPort::bytesAvailable ( )

Definition at line 340 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
int QPidCommPort::canReadLine ( )

Definition at line 355 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QStringList QPidCommPort::commPortBaudRates ( )
slot

Definition at line 49 of file qpidcommport.cpp.

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 }
QStringList QPidCommPort::commPortDataBits ( )
slot

Definition at line 85 of file qpidcommport.cpp.

86 {
87  QStringList res;
88 
89  res << "7 Bits"
90  << "8 Bits";
91 
92  return res;
93 }
QStringList QPidCommPort::commPortFlowControls ( )
slot

Definition at line 109 of file qpidcommport.cpp.

110 {
111  QStringList res;
112 
113  res << "None"
114  << "Hardware"
115  << "XON/XOFF";
116 
117  return res;
118 }
QStringList QPidCommPort::commPortParitys ( )
slot

Definition at line 137 of file qpidcommport.cpp.

138 {
139  QStringList res;
140 
141  res << "None"
142  << "Odd"
143  << "Even"
144  << "Space";
145 
146  return res;
147 }
QStringList QPidCommPort::commPortStopBits ( )
slot

Definition at line 169 of file qpidcommport.cpp.

170 {
171  QStringList res;
172 
173  res << "1 Bits"
174  << "2 Bits";
175 
176  return res;
177 }
QStringList QPidCommPort::commPortTypes ( )
slot

Definition at line 37 of file qpidcommport.cpp.

38 {
39  QStringList res;
40 
41  res << "No Port"
42  << "Local Serial Port"
43  << "Network Socket"
44  << "Epics PV";
45 
46  return res;
47 }
DataBitsType QPidCommPort::dataBits ( int  idx)
slot

Definition at line 95 of file qpidcommport.cpp.

Referenced by openLocalSerialPort().

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 }
FlowType QPidCommPort::flowControl ( int  idx)
slot

Definition at line 120 of file qpidcommport.cpp.

Referenced by openLocalSerialPort().

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 }
bool QPidCommPort::isWritable ( )

Definition at line 274 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
bool QPidCommPort::openIfNeeded ( )
private

Definition at line 439 of file qpidcommport.cpp.

References DEBUG_SERIAL, DEBUG_SOCKET, LocalSerialPort, NetworkSocket, openLocalSerialPort(), openNetworkSocket(), and printMessage().

Referenced by bytesAvailable(), canReadLine(), isWritable(), read(), readAll(), readLine(), waitForBytesWritten(), waitForReadyRead(), and write().

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 }
bool openLocalSerialPort()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
bool openNetworkSocket()
bool QPidCommPort::openLocalSerialPort ( )
private

Definition at line 370 of file qpidcommport.cpp.

References baudRate(), dataBits(), DEBUG_LEVEL1, DEBUG_SERIAL, flowControl(), m_IODevice, parity(), printMessage(), and stopBits().

Referenced by openIfNeeded().

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 }
DataBitsType dataBits(int idx)
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
BaudRateType baudRate(int idx)
StopBitsType stopBits(int idx)
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
FlowType flowControl(int idx)
ParityType parity(int idx)
bool QPidCommPort::openNetworkSocket ( )
private

Definition at line 423 of file qpidcommport.cpp.

References m_IODevice, and printMessage().

Referenced by openIfNeeded().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
ParityType QPidCommPort::parity ( int  idx)
slot

Definition at line 149 of file qpidcommport.cpp.

Referenced by openLocalSerialPort().

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 }
void QPidCommPort::printMessage ( QString  msg,
QDateTime  ts = QDateTime::currentDateTime() 
)
slot

Definition at line 193 of file qpidcommport.cpp.

References m_Controller.

Referenced by bytesAvailable(), canReadLine(), isWritable(), openIfNeeded(), openLocalSerialPort(), openNetworkSocket(), read(), readAll(), readLine(), waitForBytesWritten(), waitForReadyRead(), and write().

194 {
196 
197  if (ctl) {
198  ctl->printMessage(msg, ts);
199  }
200 }
QSharedPointer< QPidController > QPidControllerPtr
QPidControllerWPtr m_Controller
Definition: qpidcommport.h:108
QByteArray QPidCommPort::read ( int  sz)

Definition at line 232 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QByteArray QPidCommPort::readAll ( )

Definition at line 212 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QByteArray QPidCommPort::readLine ( int  maxlen = 0)

Definition at line 253 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
void QPidCommPort::readSettings ( QSettings *  set,
QString  section 
)
virtual

Definition at line 207 of file qpidcommport.cpp.

208 {
209  QcepProperty::readSettings(this, &staticMetaObject, section, set);
210 }
void QPidCommPort::readyRead ( )
signal
StopBitsType QPidCommPort::stopBits ( int  idx)
slot

Definition at line 179 of file qpidcommport.cpp.

Referenced by openLocalSerialPort().

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 }
bool QPidCommPort::successfullyConnected ( )

Definition at line 288 of file qpidcommport.cpp.

289 {
290  return get_Successful();
291 }
int QPidCommPort::waitForBytesWritten ( int  tmout = 0)

Definition at line 308 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
int QPidCommPort::waitForReadyRead ( int  tmout = 0)

Definition at line 323 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
void QPidCommPort::write ( QByteArray  msg)

Definition at line 293 of file qpidcommport.cpp.

References DEBUG_COMMPORT, DEBUG_LEVEL1, m_IODevice, openIfNeeded(), and printMessage().

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 }
QPointer< QIODevice > m_IODevice
Definition: qpidcommport.h:109
bool openIfNeeded()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
void QPidCommPort::writeSettings ( QSettings *  set,
QString  section 
)
virtual

Definition at line 202 of file qpidcommport.cpp.

203 {
204  QcepProperty::writeSettings(this, &staticMetaObject, section, set);
205 }

Member Data Documentation

QPidControllerWPtr QPidCommPort::m_Controller
private

Definition at line 108 of file qpidcommport.h.

Referenced by printMessage().

QPointer<QIODevice> QPidCommPort::m_IODevice
private

Property Documentation

int QPidCommPort::commPortType
readwrite

Definition at line 68 of file qpidcommport.h.

int QPidCommPort::connected
readwrite

Definition at line 101 of file qpidcommport.h.

QString QPidCommPort::epicsPVName
readwrite

Definition at line 98 of file qpidcommport.h.

int QPidCommPort::serialPortBaud
readwrite

Definition at line 74 of file qpidcommport.h.

int QPidCommPort::serialPortBits
readwrite

Definition at line 77 of file qpidcommport.h.

int QPidCommPort::serialPortFlowControl
readwrite

Definition at line 86 of file qpidcommport.h.

QString QPidCommPort::serialPortName
readwrite

Definition at line 71 of file qpidcommport.h.

int QPidCommPort::serialPortParity
readwrite

Definition at line 80 of file qpidcommport.h.

int QPidCommPort::serialPortStopBits
readwrite

Definition at line 83 of file qpidcommport.h.

QString QPidCommPort::socketHostAddress
readwrite

Definition at line 89 of file qpidcommport.h.

int QPidCommPort::socketPortNumber
readwrite

Definition at line 92 of file qpidcommport.h.

int QPidCommPort::socketTimeout
readwrite

Definition at line 95 of file qpidcommport.h.

int QPidCommPort::successful
readwrite

Definition at line 104 of file qpidcommport.h.


The documentation for this class was generated from the following files: