This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtumodbusserver.cpp
113 lines (100 loc) · 2.53 KB
/
rtumodbusserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// SmartServer
// Copyright (C) 2021 АО "Нефтеавтоматика"
//
// Разработчик
// Ананьев А.А. <Ananev-AA@nefteavtomatika.ru>
//
#include <QDebug>
#include "rtumodbusserver.h"
///
/// \brief RtuModbusServer::RtuModbusServer
///
RtuModbusServer::RtuModbusServer()
:_restartTimeout(0)
,_lastRequestTime(QDateTime::currentDateTime().toSecsSinceEpoch())
{
connect(&_restartTimer, &QTimer::timeout, this, &RtuModbusServer::on_timeout);
}
///
/// \brief RtuModbusServer::~RtuModbusServer
///
RtuModbusServer::~RtuModbusServer()
{
disconnectDevice();
}
///
/// \brief RtuModbusServer::addSmartCardInfo
/// \param smi
///
void RtuModbusServer::addSmartCardInfo(const SmartCardInfo& smi)
{
if(_buffer == nullptr)
{
return;
}
const auto id = smi.id().toUInts();
const auto address = _buffer->nextAddress();
for(int i = 0; i < id.size(); i++)
{
setData(_buffer->registerType(), address + i, id[i]);
}
setData(_buffer->registerType(), _buffer->startAddress(), address + 1);
}
///
/// \brief RtuModbusServer::createRegisters
/// \param type
/// \param start
/// \param count
/// \param alignment
///
void RtuModbusServer::createRegisters(QModbusDataUnit::RegisterType type, quint16 start, quint16 count, quint8 alignment)
{
_buffer = std::make_unique<RtuModbusDataBuffer>(type, start, count);
_buffer->setDataAlignment(alignment);
setMap(_buffer->getModbusDataUnitMap());
}
///
/// \brief RtuModbusServer::restartTimeout
/// \return
///
int RtuModbusServer::restartTimeout() const
{
return _restartTimeout;
}
///
/// \brief RtuModbusServer::setRestartTimeout
/// \param value
///
void RtuModbusServer::setRestartTimeout(int value)
{
_restartTimer.stop();
_restartTimeout = value;
_restartTimer.start(1000);
}
///
/// \brief RtuModbusServer::processRequest
/// \param request
/// \return
///
QModbusResponse RtuModbusServer::processRequest(const QModbusPdu &request)
{
_lastRequestTime = QDateTime::currentDateTime().toSecsSinceEpoch();
return QModbusRtuSerialServer::processRequest(request);
}
///
/// \brief RtuModbusServer::on_timeout
///
void RtuModbusServer::on_timeout()
{
if(_restartTimeout == 0) return;
if(state() != QModbusDevice::ConnectedState) return;
const auto nowTime = QDateTime::currentDateTime().toSecsSinceEpoch();
if(nowTime - _lastRequestTime > _restartTimeout)
{
disconnectDevice();
connectDevice();
_lastRequestTime = nowTime;
emit restarted();
}
}