SEI Modbus Dev Contd
This commit is contained in:
@@ -1,19 +1,27 @@
|
||||
#include "ModbusSEIMgr.h"
|
||||
#include <QTcpSocket>
|
||||
#include "ModbusCCDefs.h"
|
||||
#include "GlobalDefine.h"
|
||||
|
||||
CModbusSEIMgr::CModbusSEIMgr(CModbusRepository *Repo, int ModbusPort, int DevID):
|
||||
CModbusSEIMgr::CModbusSEIMgr(CModbusRepository *Repo, QHostAddress ServerIP, int ModbusPort, int DevID):
|
||||
CModbusBackend(Repo)
|
||||
{
|
||||
// connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady()));
|
||||
mModbusMode = MODBUS_MASTER_MODE;
|
||||
mModbusPort = ModbusPort;
|
||||
mDeviceID = DevID;
|
||||
mSEIIPAddress = ServerIP;
|
||||
|
||||
mConnectionTimer = new QTimer();
|
||||
mConnectionTimer->setInterval(SEI_CLIENT_RECONNECT_TIMEOUT);
|
||||
mConnectionTimer->setSingleShot(false);
|
||||
mConnectionTimer->stop();
|
||||
|
||||
mModbusTCPSocketHandle = new QTcpSocket();
|
||||
connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady()));
|
||||
connect(mModbusTCPSocketHandle,SIGNAL(disconnected()),this,SLOT(SocketDisconnected()));
|
||||
connect(mModbusTCPSocketHandle,SIGNAL(connected()),this,SLOT(SocketConnected()));
|
||||
connect(mConnectionTimer,SIGNAL(timeout()),this,SLOT(ConnectionTimerExpired()));
|
||||
}
|
||||
|
||||
CModbusSEIMgr::~CModbusSEIMgr()
|
||||
@@ -24,9 +32,20 @@ CModbusSEIMgr::~CModbusSEIMgr()
|
||||
mModbusTCPSocketHandle->waitForDisconnected(1000);
|
||||
}
|
||||
delete mModbusTCPSocketHandle;
|
||||
|
||||
if(mConnectionTimer)
|
||||
delete mConnectionTimer;
|
||||
if(mSEIWatchdogTimer)
|
||||
delete mSEIWatchdogTimer;
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::ConnectToSlave(QString SlaveIP, int SlavePort)
|
||||
int CModbusSEIMgr::StartSEICommunication()
|
||||
{
|
||||
mConnectionTimer->start();
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::ConnectToSlave(QHostAddress SlaveIP, int SlavePort)
|
||||
{
|
||||
if(mModbusTCPSocketHandle->state() != QAbstractSocket::UnconnectedState)
|
||||
{
|
||||
@@ -42,12 +61,12 @@ int CModbusSEIMgr::DisconnectFromSlave()
|
||||
{
|
||||
if(mModbusTCPSocketHandle->state() != QAbstractSocket::ConnectedState)
|
||||
{
|
||||
qDebug("Trying to disconnect a non connected socket");
|
||||
mModbusTCPSocketHandle->disconnectFromHost();
|
||||
mConnectionTimer->start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
qDebug("Requesting Disconnection...");
|
||||
qDebug("Requesting Disconnection from NetTrac...");
|
||||
mModbusTCPSocketHandle->disconnectFromHost();
|
||||
return 1;
|
||||
}
|
||||
@@ -55,13 +74,16 @@ int CModbusSEIMgr::DisconnectFromSlave()
|
||||
void CModbusSEIMgr::SocketConnected()
|
||||
{
|
||||
emit ModbusMasterConnected(this);
|
||||
qDebug("Master: Connection established with slave");
|
||||
qDebug("Master: Connection established with NetTrac");
|
||||
mConnectionTimer->stop();
|
||||
}
|
||||
|
||||
void CModbusSEIMgr::SocketDisconnected()
|
||||
{
|
||||
ModbusLinkDisconnected();
|
||||
emit ModbusMasterDisconnected(this);
|
||||
mConnectionTimer->start();
|
||||
qDebug("Disconnected from NetTrac");
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::ReadModbusRegisters()
|
||||
@@ -70,6 +92,44 @@ int CModbusSEIMgr::ReadModbusRegisters()
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void CModbusSEIMgr::ConnectionTimerExpired()
|
||||
{
|
||||
//Establish connection only if not connected
|
||||
if(mModbusTCPSocketHandle->state() == QAbstractSocket::UnconnectedState)
|
||||
{
|
||||
ConnectToSlave(mSEIIPAddress,mModbusPort);
|
||||
mConnectionTimeout.restart();
|
||||
}
|
||||
else if(mModbusTCPSocketHandle->state() == QAbstractSocket::ConnectingState)
|
||||
{
|
||||
//Default connection timeout is 1s for sockets. We want this to be shorter.
|
||||
//if connection timeout is reached, disconnect and retry.
|
||||
if(mConnectionTimeout.hasExpired(SEI_CLIENT_CONNECTION_TIMEOUT))
|
||||
{
|
||||
mModbusTCPSocketHandle->disconnectFromHost();
|
||||
qDebug("Connection timeout expired... disconnecting");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::SEISettingsChanged(QHostAddress ServerIP, int ModbusPort, int DevID)
|
||||
{
|
||||
if(DevID > 255)
|
||||
{
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
|
||||
mSEIIPAddress = ServerIP;
|
||||
mModbusPort = ModbusPort;
|
||||
mDeviceID = DevID;
|
||||
|
||||
//Make shure we are disconnected. Connection timer will do the reconnection.
|
||||
DisconnectFromSlave();
|
||||
|
||||
return RET_OK;
|
||||
|
||||
}
|
||||
|
||||
void CModbusSEIMgr::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length)
|
||||
{
|
||||
@@ -77,24 +137,9 @@ void CModbusSEIMgr::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Lengt
|
||||
Q_UNUSED(Length)
|
||||
|
||||
emit ModbusMasterRepositoryUpdated();
|
||||
qDebug("Database updated with ZT data...");
|
||||
}
|
||||
|
||||
void CModbusSEIMgr::ModbusResponseException(quint8 ExceptionCode, quint8 FctCode)
|
||||
{
|
||||
qDebug("Modbus MASTER exception: code:%d Fct:%d",ExceptionCode,FctCode);
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::SendAN1ToZT()
|
||||
{
|
||||
//return SendWriteHoldingRegistersRequest(CC_AN1_REGISTER_ADDRESS,1);
|
||||
//return SendWriteSingleRegisterRequest(CC_AN1_REGISTER_ADDRESS);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CModbusSEIMgr::SendAN2ToZT()
|
||||
{
|
||||
//return SendWriteHoldingRegistersRequest(CC_AN2_REGISTER_ADDRESS,1);
|
||||
//return SendWriteSingleRegisterRequest(CC_AN2_REGISTER_ADDRESS);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
#ifndef CMODBUSSEIMGR_H
|
||||
#define CMODBUSSEIMGR_H
|
||||
|
||||
|
||||
#include <QObject>
|
||||
#include "ModbusBackend.h"
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <QHostAddress>
|
||||
|
||||
|
||||
|
||||
@@ -11,20 +15,26 @@ class CModbusSEIMgr : public CModbusBackend
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CModbusSEIMgr(CModbusRepository *Repo, int ModbusPort, int DevID);
|
||||
explicit CModbusSEIMgr(CModbusRepository *Repo, QHostAddress ServerIP, int ModbusPort, int DevID);
|
||||
~CModbusSEIMgr();
|
||||
|
||||
int ConnectToSlave(QString SlaveIP, int SlavePort);
|
||||
int ConnectToSlave(QHostAddress SlaveIP, int SlavePort);
|
||||
int DisconnectFromSlave();
|
||||
int ReadModbusRegisters();
|
||||
int SendAN1ToZT();
|
||||
int SendAN2ToZT();
|
||||
int StartSEICommunication();
|
||||
int SEISettingsChanged(QHostAddress ServerIP, int ModbusPort, int DevID);
|
||||
|
||||
|
||||
virtual void RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length);
|
||||
virtual void ModbusResponseException(quint8 ExceptionCode, quint8 FctCode);
|
||||
|
||||
QTimer *mConnectionTimer;
|
||||
QTimer *mSEIWatchdogTimer;
|
||||
QElapsedTimer mConnectionTimeout;
|
||||
|
||||
private:
|
||||
int mModbusPort;
|
||||
QHostAddress mSEIIPAddress;
|
||||
|
||||
signals:
|
||||
void ModbusMasterConnected(CModbusSEIMgr *);
|
||||
@@ -34,6 +44,7 @@ signals:
|
||||
public slots:
|
||||
void SocketConnected();
|
||||
void SocketDisconnected();
|
||||
void ConnectionTimerExpired();
|
||||
};
|
||||
|
||||
#endif // CMODBUSSEIMGR_H
|
||||
|
||||
Reference in New Issue
Block a user