-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModbusTCP_IP.h
164 lines (142 loc) · 6.43 KB
/
ModbusTCP_IP.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//---------------------------------------------------------------------------
#ifndef ModbusTCP_IPH
#define ModbusTCP_IPH
#include <vector>
#include <cstdint>
//#include "ExceptUtils.h"
#include "Modbus.h"
#define DEFAULT_MODBUS_TCPIP_HOST "localhost"
#define DEFAULT_MODBUS_TCPIP_PORT 502
//---------------------------------------------------------------------------
namespace Modbus {
//---------------------------------------------------------------------------
class TCPIPContext : public Context {
public:
TCPIPContext( SlaveAddrType SlaveAddr, TransactionIdType TransactionId = 0 )
: Context( SlaveAddr ), transactionId_( TransactionId ) {}
protected:
virtual TransactionIdType DoGetTransactionIdentifier() const override {
return transactionId_;
}
private:
TransactionIdType transactionId_;
};
//---------------------------------------------------------------------------
namespace Master {
//---------------------------------------------------------------------------
class TCPIPProtocol : public Protocol {
public:
String GetHost() const;
void SetHost( String Val );
uint16_t GetPort() const;
void SetPort( uint16_t Val );
protected:
using BMAPTransactionIdType = uint16_t;
using BMAPProtocolType = uint16_t;
using BMAPDataLengthType = uint16_t;
using BMAPUnitIdType = uint8_t;
virtual String DoGetProtocolParamsStr() const override;
virtual String DoGetHost() const = 0;
virtual void DoSetHost( String Val ) = 0;
virtual uint16_t DoGetPort() const = 0;
virtual void DoSetPort( uint16_t Val ) = 0;
virtual void DoInputBufferClear() {}
virtual void DoWrite( TBytes const OutBuffer ) = 0;
virtual void DoRead( TBytes& InBuffer, size_t Length ) = 0;
// DoReadCoilStatus
// DoReadInputStatus
virtual void DoReadHoldingRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
RegDataType* Data ) override;
virtual void DoReadInputRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
RegDataType* Data ) override;
// DoForceSingleCoil
virtual void DoPresetSingleRegister( Context const & Context,
RegAddrType Addr,
RegDataType Data ) override;
// DoReadExceptionStatus
// DoDiagnostics
// DoProgram484
// DoPoll484
// DoFetchCommEventCtr
// DoFetchCommEventLog
// DoProgramController
// DoPollController
// DoForceMultipleCoils
void DoPresetMultipleRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
RegDataType const * Data ) override;
// DoReportSlave
// DoProgram884_M84
// DoResetCommLink
// DoReadGeneralReference
// DoWriteGeneralReference
// DoMaskWrite4XRegister
virtual void DoMaskWrite4XRegister( Context const & Context,
RegAddrType Addr,
RegDataType AndMask,
RegDataType OrMask ) override;
// DoReadWrite4XRegisters
// DoReadFIFOQueue
private:
static void RaiseExceptionIfBMAPIsNotValid( Context const & Context,
TBytes const Buffer );
static void RaiseExceptionIfBMAPIsNotEQ( Context const & Context,
TBytes const LBuffer,
TBytes const RBuffer );
static void RaiseExceptionIfReplyIsNotValid( Context const & Context,
TBytes const Buffer,
FunctionCode ExpectedFunctionCode );
static FunctionCode GetFunctionCode( TBytes const Buffer );
static ExceptionCode GetExceptCode( TBytes const Buffer );
static BMAPDataLengthType GetDataLength( TBytes const Buffer );
static BMAPTransactionIdType GetBMAPTransactionIdentifier( TBytes const Buffer );
static BMAPProtocolType GetBMAPProtocol( TBytes const Buffer );
static BMAPDataLengthType GetBMAPDataLength( TBytes const Buffer );
static BMAPUnitIdType GetBMAPUnitIdentifier( TBytes const Buffer );
static BMAPDataLengthType GetBMAPHeaderLength() { return 7; }
static int WriteBMAPHeader( TBytes & OutBuffer, int StartIdx,
Context const & Context );
static int GetAddressPointCountPairLength() { return 4; }
static int WriteAddressPointCountPair( TBytes & OutBuffer, int StartIdx,
RegAddrType StartAddr,
RegCountType PointCount );
static int WriteData( TBytes & OutBuffer, int StartIdx, RegAddrType Data );
static void CopyDataWord( Context const & Context, TBytes const Buffer,
int BufferOffset, uint16_t* Data );
static int GetPayloadLength( Context const & Context,
TBytes const Buffer,
int BufferOffset );
void ReadRegisters( FunctionCode FnCode, Context const & Context,
RegAddrType StartAddr, RegCountType PointCount,
RegDataType* Data );
protected:
template<typename T>
static uint16_t GetLength( T const & Data ) {
//return static_cast<BMAPDataLengthType>( Data.size() );
return static_cast<BMAPDataLengthType>( Data.Length );
}
template<typename T>
static void SetLength( T& OutBuffer, uint16_t Length ) {
//OutBuffer.resize( static_cast<typename T::size_type>( Length ) );
OutBuffer.Length = Length;
}
template<typename T>
static uint8_t const * GetData( T const & OutBuffer ) {
return &OutBuffer[0];
}
template<typename T>
static uint8_t* GetData( T& OutBuffer ) {
return &OutBuffer[0];
}
};
//---------------------------------------------------------------------------
}; // End of namespace Master
//---------------------------------------------------------------------------
}; // End of namespace Modbus
//---------------------------------------------------------------------------
#endif