Modbus SEI Dev again...

This commit is contained in:
jfmartel 2019-09-20 14:59:46 -04:00
parent 9d02b34ad0
commit 6f596a9f1e
6 changed files with 107 additions and 35 deletions

View File

@ -554,17 +554,22 @@ int CModbusBackend::SendWriteHoldingRegistersRequest(quint16 StartAddress, quint
NewRequest->mPDU.mData.clear();
NewRequest->mPDU.mFunctionCode = MODBUS_FCT_WRITE_MULTIPLE_REGISTERS;
//Start address
quint8 HighByte, LowByte;
LowByte = StartAddress & 0x00FF;
HighByte = (StartAddress >> 8) & 0x00FF;
NewRequest->mPDU.mData.append(HighByte);
NewRequest->mPDU.mData.append(LowByte);
//Nb Registers
LowByte = RegisterCount & 0x00FF;
HighByte = (RegisterCount >> 8) & 0x00FF;
NewRequest->mPDU.mData.append(HighByte);
NewRequest->mPDU.mData.append(LowByte);
//Byte Count
NewRequest->mPDU.mData.append(RegData.size());
NewRequest->mPDU.mData.append(RegData);
NewRequest->mHeader.mMessageLength = NewRequest->mPDU.mData.size() + 2;

View File

@ -1,10 +1,11 @@
#include "ModbusSEIMgr.h"
#include <QTcpSocket>
#include "ModbusCCDefs.h"
#include "ModbusSEIDefs.h"
#include "GlobalDefine.h"
CModbusSEIMgr::CModbusSEIMgr(CModbusRepository *Repo, QHostAddress ServerIP, int ModbusPort, int DevID):
CModbusBackend(Repo)
CModbusSEIMgr::CModbusSEIMgr(CModbusRepository *SEIRepo, CModbusRepository *CCRepo, QHostAddress ServerIP, int ModbusPort, int DevID):
CModbusBackend(SEIRepo)
{
// connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady()));
mModbusMode = MODBUS_MASTER_MODE;
@ -12,16 +13,27 @@ CModbusSEIMgr::CModbusSEIMgr(CModbusRepository *Repo, QHostAddress ServerIP, int
mDeviceID = DevID;
mSEIIPAddress = ServerIP;
mCCRepoHandle = CCRepo;
mZTWatchdog = false;
mConnectionTimer = new QTimer();
mConnectionTimer->setInterval(SEI_CLIENT_RECONNECT_TIMEOUT);
mConnectionTimer->setSingleShot(false);
mConnectionTimer->stop();
mSEIModbusUpdateTimer = new QTimer();
mSEIModbusUpdateTimer->setInterval(1000);
mSEIModbusUpdateTimer->setSingleShot(false);
mSEIModbusUpdateTimer->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()));
connect(mSEIModbusUpdateTimer,SIGNAL(timeout()),this,SLOT(SEIModbusUpdateTimerExpired()));
}
CModbusSEIMgr::~CModbusSEIMgr()
@ -35,8 +47,8 @@ CModbusSEIMgr::~CModbusSEIMgr()
if(mConnectionTimer)
delete mConnectionTimer;
if(mSEIWatchdogTimer)
delete mSEIWatchdogTimer;
if(mSEIModbusUpdateTimer)
delete mSEIModbusUpdateTimer;
}
int CModbusSEIMgr::StartSEICommunication()
@ -76,6 +88,7 @@ void CModbusSEIMgr::SocketConnected()
emit ModbusMasterConnected(mModbusTCPSocketHandle->localAddress().toIPv4Address(),mModbusTCPSocketHandle->peerAddress().toIPv4Address());
qDebug("Master: Connection established with NetTrac");
mConnectionTimer->stop();
mSEIModbusUpdateTimer->start();
}
void CModbusSEIMgr::SocketDisconnected()
@ -83,6 +96,7 @@ void CModbusSEIMgr::SocketDisconnected()
ModbusLinkDisconnected();
emit ModbusMasterDisconnected();
mConnectionTimer->start();
mSEIModbusUpdateTimer->stop();
qDebug("Disconnected from NetTrac");
}
@ -98,18 +112,7 @@ void CModbusSEIMgr::ConnectionTimerExpired()
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)
@ -131,6 +134,49 @@ int CModbusSEIMgr::SEISettingsChanged(QHostAddress ServerIP, int ModbusPort, int
}
void CModbusSEIMgr::SEIModbusUpdateTimerExpired()
{
//The SEI Modbus table is updated on event, triggered by the TKTransport object (see ZTTKUpdated()).
//Just update the watchdog and send the current table.
mZTWatchdog = !mZTWatchdog;
if(mZTWatchdog)
{
mModbusRepo->WriteSingleReg(SEI_MODBUS_ZT_WATCHDOG_REG,0x01);
}
else
{
mModbusRepo->WriteSingleReg(SEI_MODBUS_ZT_WATCHDOG_REG,0);
}
SendWriteHoldingRegistersRequest(SEI_MODBUS_ZT_DATA_BASE_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE);
}
//This signal is emitted by TKTransport when a changed happened in the TK after the CC modbus table has been updated.
//We just need to copy the relevant info from the CC repo...
//As per specification, we must also send an update on change to the SEI
void CModbusSEIMgr::ZTTKUpdated()
{
//Copy data from CC modbus table to SEI modbus table.
mModbusRepo->WriteHRData(SEI_MODBUS_ZT_DATA_BASE_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE,mCCRepoHandle->GetHRData(MODBUS_ZT_DATA_BASE_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE));
//Keep our actual watchdog value..
if(mZTWatchdog)
{
mModbusRepo->WriteSingleReg(SEI_MODBUS_ZT_WATCHDOG_REG,0x01);
}
else
{
mModbusRepo->WriteSingleReg(SEI_MODBUS_ZT_WATCHDOG_REG,0);
}
//Send the data now to NetTrac
SendWriteHoldingRegistersRequest(SEI_MODBUS_ZT_WATCHDOG_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE);
}
void CModbusSEIMgr::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length)
{
Q_UNUSED(StartAddress)

View File

@ -15,7 +15,7 @@ class CModbusSEIMgr : public CModbusBackend
{
Q_OBJECT
public:
explicit CModbusSEIMgr(CModbusRepository *Repo, QHostAddress ServerIP, int ModbusPort, int DevID);
explicit CModbusSEIMgr(CModbusRepository *SEIRepo,CModbusRepository *CCRepo, QHostAddress ServerIP, int ModbusPort, int DevID);
~CModbusSEIMgr();
int ConnectToSlave(QHostAddress SlaveIP, int SlavePort);
@ -29,12 +29,14 @@ public:
virtual void ModbusResponseException(quint8 ExceptionCode, quint8 FctCode);
QTimer *mConnectionTimer;
QTimer *mSEIWatchdogTimer;
QElapsedTimer mConnectionTimeout;
// QTimer *mSEIWatchdogTimer;
QTimer *mSEIModbusUpdateTimer;
private:
int mModbusPort;
QHostAddress mSEIIPAddress;
CModbusRepository *mCCRepoHandle;
bool mZTWatchdog;
signals:
void ModbusMasterConnected(qint32 LocalIP, qint32 RemoteIP);
@ -45,6 +47,8 @@ public slots:
void SocketConnected();
void SocketDisconnected();
void ConnectionTimerExpired();
void SEIModbusUpdateTimerExpired();
void ZTTKUpdated();
};
#endif // CMODBUSSEIMGR_H

View File

@ -4,9 +4,10 @@
#include "EngLog.h"
CModbusTKTransport::CModbusTKTransport(CModbusRepository *Repo)
CModbusTKTransport::CModbusTKTransport(CModbusRepository *CCRepo, CModbusRepository *SEIRepo)
{
mMCCodbusRepo = Repo;
mMCCodbusRepo = CCRepo;
mSEIModbusRepo = SEIRepo;
mZT1CDVState = mZT2CDVState = false;
mMaintenanceMode = mForceZT1Clear = mForceZT2Clear = false;
mZT1TKDataList.clear();
@ -68,6 +69,8 @@ int CModbusTKTransport::ClearTK(int ZT)
mMCCodbusRepo->WriteMultipleRegs(MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD,Data);
mMCCodbusRepo->WriteSingleReg(MODBUS_MISC_DATA_BASE_REG_ADD,PannesReg);
emit TKUpdated();
}
else if(ZT == ZT2_TYPE_ID)
@ -94,6 +97,7 @@ int CModbusTKTransport::ClearTK(int ZT)
//Clear repo now...
mMCCodbusRepo->WriteMultipleRegs(MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD,Data);
mMCCodbusRepo->WriteSingleReg(MODBUS_MISC_DATA_BASE_REG_ADD,PannesReg);
emit TKUpdated();
}
else
{
@ -263,6 +267,7 @@ int CModbusTKTransport::SendTKToCC(int ZT)
DataBuf[RegOffset(MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD)] = FlagsReg;
DataBuf[RegOffset(MODBUS_MISC_DATA_BASE_REG_ADD)] = PannesReg;
mMCCodbusRepo->WriteMultipleRegs(MODBUS_ZT_DATA_BASE_REG,DataBuf);
emit TKUpdated();
}
else if(ZT == ZT2_TYPE_ID)
@ -365,6 +370,7 @@ int CModbusTKTransport::SendTKToCC(int ZT)
DataBuf[RegOffset(MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD)] = FlagsReg;
DataBuf[RegOffset(MODBUS_MISC_DATA_BASE_REG_ADD)] = PannesReg;
mMCCodbusRepo->WriteMultipleRegs(MODBUS_ZT_DATA_BASE_REG,DataBuf);
emit TKUpdated();
}
else

View File

@ -24,6 +24,7 @@
#include "ModbusRepository.h"
#include <QTimer>
#include "ModbusCCDefs.h"
#include "ModbusSEIDefs.h"
class CModbusTKTransport : public CTKTransportInterface
@ -43,7 +44,7 @@ public:
CModbusTKTransport(CModbusRepository *Repo);
CModbusTKTransport(CModbusRepository *CCRepo, CModbusRepository *SEIRepo);
~CModbusTKTransport();
@ -67,7 +68,7 @@ public:
virtual unsigned int EnterMaintenance();
private:
CModbusRepository *mMCCodbusRepo;
CModbusRepository *mMCCodbusRepo,*mSEIModbusRepo;
QList<CZTDetectionData> mZT1TKDataList,mZT2TKDataList;
bool mZT1CDVState, mZT2CDVState;
bool mMaintenanceMode, mForceZT1Clear, mForceZT2Clear;
@ -78,6 +79,9 @@ private:
int RegOffset(qint16 Reg){return (int)Reg-MODBUS_ZT_DATA_BASE_REG;}
signals:
void TKUpdated();
public slots:
void ExecZT1SM();
void ExecZT2SM();

View File

@ -594,7 +594,23 @@ unsigned int CZoneTest::InitZT()
}
mModbusCCMgr->StartModbusCCServer();
CModbusTKTransport *TransportInterface = new CModbusTKTransport(mCCModbusRepository);
mSEIModbusRepository = new CModbusRepository;
mSEIModbusRepository->AddHRDataMap(SEI_MODBUS_ZT_DATA_BASE_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE); //Add the ZT data map
mSEIModbusRepository->AddHRDataMap(SEI_MODBUS_SEI_DATA_BASE_REG,SEI_MODBUS_SEI_TABLE_DATA_SIZE); //Add the SEI data map
if(mModbusSEIMgr == 0)
{
mModbusSEIMgr = new CModbusSEIMgr(mSEIModbusRepository,mCCModbusRepository,mZTSettings->mSEIModbusHostAddress,ModbusSEIPort,SEIDevID);
}
mModbusSEIMgr->StartSEICommunication();
connect(mModbusSEIMgr,SIGNAL(ModbusMasterConnected(qint32,qint32)),panel.mSEISettingsPage,SLOT(ModbusSEIConnected(qint32,qint32)));
connect(mModbusSEIMgr,SIGNAL(ModbusMasterDisconnected()),panel.mSEISettingsPage,SLOT(ModbusSEIDisconnected()));
panel.mZTMainPage->ModbusSEIDisconnected();
CModbusTKTransport *TransportInterface = new CModbusTKTransport(mCCModbusRepository,mSEIModbusRepository);
mTKTransportInterface = (CTKTransportInterface*)TransportInterface;
connect(mModbusCCMgr,SIGNAL(RepoHasChanged()),TransportInterface,SLOT(ModbusCCUpdated()));
@ -606,24 +622,15 @@ unsigned int CZoneTest::InitZT()
connect(mModbusCCMgr,SIGNAL(ModbusCCDisconnected()),panel.mModbusSettingsPage,SLOT(ModbusCCDisconnected()));
connect(mModbusCCMgr,SIGNAL(ModbusCCLinkLost()),panel.mModbusSettingsPage,SLOT(ModbusCCLinkDown()));
connect(mModbusCCMgr,SIGNAL(ModbusCCLinkRecovered()),panel.mModbusSettingsPage,SLOT(ModbusCCLinkUP()));
connect(TransportInterface,SIGNAL(TKUpdated()),mModbusSEIMgr,SLOT(ZTTKUpdated()));
panel.mZTMainPage->ModbusCCDisconnected();
panel.mZTMainPage->mZT1Stats->Init(true);
panel.mZTMainPage->mZT2Stats->Init(true);
mSEIModbusRepository = new CModbusRepository;
mSEIModbusRepository->AddHRDataMap(SEI_MODBUS_ZT_DATA_BASE_REG,SEI_MODBUS_ZT_TABLE_DATA_SIZE); //Add the ZT data map
mSEIModbusRepository->AddHRDataMap(SEI_MODBUS_SEI_DATA_BASE_REG,SEI_MODBUS_SEI_TABLE_DATA_SIZE); //Add the SEI data map
if(mModbusSEIMgr == 0)
{
mModbusSEIMgr = new CModbusSEIMgr(mSEIModbusRepository,mZTSettings->mSEIModbusHostAddress,ModbusSEIPort,SEIDevID);
}
mModbusSEIMgr->StartSEICommunication();
connect(mModbusSEIMgr,SIGNAL(ModbusMasterConnected(qint32,qint32)),panel.mSEISettingsPage,SLOT(ModbusSEIConnected(qint32,qint32)));
connect(mModbusSEIMgr,SIGNAL(ModbusMasterDisconnected()),panel.mSEISettingsPage,SLOT(ModbusSEIDisconnected()));
panel.mZTMainPage->ModbusSEIDisconnected();