Q_OBJECT
Q_DISABLE_COPY(MasterDevice)
public:
enum CHANNEL_TYPE {
DataChannel,
ControlChannel
};
private:
quint16 dataPort;
quint16 controlPort;
public:
explicit Detector
(QObject *parent,
QHostAddress hostAddress, quint16 dataPort, quint16 controlPort
) , hostAddress(hostAddress)
, dataPort(dataPort)
, controlPort(controlPort)
{
connect(controlDevice, SIGNAL(readyRead()),this, SLOT(controlChannelReadReady()));
connect(dataDevice, SIGNAL(readyRead()),this, SLOT(dataChannelReadReady()));
}
virtual ~Detector() {}
bool open(OpenMode mode) override {
controlDevice
->connectToHost
(hostAddress, controlPort,
QTcpSocket::ReadWrite);
dataDevice
->connectToHost
(hostAddress, dataPort,
QTcpSocket::ReadOnly);
}
qint64 readData(char *data, qint64 maxSize) override {
switch ( currentReadChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->read(data, maxSize);
}
qint64 writeData(const char * data, qint64 maxSize) override {
switch ( currentWriteChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->write(data, maxSize);
}
private slots:
void controlChannelReadReady() {
emit channelReadyRead(ControlChannel);
}
void dataChannelReadReady() {
emit channelReadyRead(DataChannel);
}
};
class Detector : public QIODevice {
Q_OBJECT
Q_DISABLE_COPY(MasterDevice)
public:
enum CHANNEL_TYPE {
DataChannel,
ControlChannel
};
private:
QTcpSocket *controlDevice;
QTcpSocket *dataDevice;
QHostAddress hostAddress;
quint16 dataPort;
quint16 controlPort;
public:
explicit Detector(QObject *parent, QHostAddress hostAddress, quint16 dataPort, quint16 controlPort)
: QIODevice(parent)
, hostAddress(hostAddress)
, dataPort(dataPort)
, controlPort(controlPort)
{
controlDevice = new QTcpSocket(this);
connect(controlDevice, SIGNAL(readyRead()),this, SLOT(controlChannelReadReady()));
dataDevice = new QTcpSocket(this);
connect(dataDevice, SIGNAL(readyRead()),this, SLOT(dataChannelReadReady()));
}
virtual ~Detector() {}
bool open(OpenMode mode) override {
QIODevice::open(mode);
controlDevice->connectToHost(hostAddress, controlPort, QTcpSocket::ReadWrite);
dataDevice->connectToHost(hostAddress, dataPort, QTcpSocket::ReadOnly);
}
qint64 readData(char *data, qint64 maxSize) override {
QTcpSocket *socket;
switch ( currentReadChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->read(data, maxSize);
}
qint64 writeData(const char * data, qint64 maxSize) override {
QTcpSocket *socket;
switch ( currentWriteChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->write(data, maxSize);
}
private slots:
void controlChannelReadReady() {
emit channelReadyRead(ControlChannel);
}
void dataChannelReadReady() {
emit channelReadyRead(DataChannel);
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks