-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modbus.h
378 lines (331 loc) · 13.2 KB
/
Modbus.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//---------------------------------------------------------------------------
#ifndef ModbusH
#define ModbusH
#include <System.SysUtils.hpp>
#include <cstdint>
#include <string>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
//---------------------------------------------------------------------------
namespace Modbus {
//---------------------------------------------------------------------------
extern void DebugBytesToHex( String Prologue, TBytes Data );
enum class FunctionCode {
ReadCoilStatus = 1,
ReadInputStatus = 2,
ReadHoldingRegisters = 3,
ReadInputRegisters = 4,
ForceSingleCoil = 5,
PresetSingleRegister = 6,
ReadExceptionStatus = 7,
Diagnostics = 8,
Program484 = 9,
Poll484 = 10,
FetchCommEventCtr = 11,
FetchCommEventLog = 12,
ProgramController = 13,
PollController = 14,
ForceMultipleCoils = 15,
PresetMultipleRegisters = 16,
ReportSlave = 17,
Program884_M84 = 18,
ResetCommLink = 19,
ReadGeneralReference = 20,
WriteGeneralReference = 21,
MaskWrite4XRegister = 22,
ReadWrite4XRegisters = 23,
ReadFIFOQueue = 24
};
enum class ExceptionCode {
IllegalFunction = 1, // The function code received in the query
// is not an allowable action for the slave.
// If a Poll Program Complete command
// was issued, this code indicates that no
// program function preceded it.
IllegalDataAddress = 2, // The data address received in the query
// is not an allowable address for the
// slave.
IllegalDataValue = 3, // A value contained in the query data
// field is not an allowable value for the
// slave.
SlaveDeviceFailure = 4, // An unrecoverable error occurred while
// the slave was attempting to perform the
// requested action.
Acknowledge = 5, // The slave has accepted the request
// and is processing it, but a long duration
// of time will be required to do so. This
// response is returned to prevent a
// timeout error from occurring in the
// master. The master can next issue a
// Poll Program Complete message to
// determine if processing is completed.
SlaveDeviceBusy = 6, // The slave is engaged in processing a
// long–duration program command. The
// master should retransmit the message
// later when the slave is free.
NegativeAcknowledge = 7, // The slave cannot perform the program
// function received in the query. This
// code is returned for an unsuccessful
// programming request using function
// code 13 or 14 decimal. The master
// should request diagnostic or error
// information from the slave.
MemoryParityError = 8, // The slave attempted to read extended
// memory, but detected a parity error in
// the memory. The master can retry the
// request, but service may be required on
// the slave device.
};
//---------------------------------------------------------------------------
class Context {
public:
using SlaveAddrType = uint8_t;
using TransactionIdType = uint16_t;
explicit Context( SlaveAddrType SlaveAddr ) : slaveAddr_( SlaveAddr ) {}
virtual ~Context() = default;
SlaveAddrType GetSlaveAddr() const { return DoGetSlaveAddr(); }
TransactionIdType GetTransactionIdentifier() const {
return DoGetTransactionIdentifier();
}
protected:
virtual SlaveAddrType DoGetSlaveAddr() const { return slaveAddr_; }
virtual TransactionIdType DoGetTransactionIdentifier() const {
return TransactionIdType();
}
private:
SlaveAddrType slaveAddr_;
};
//---------------------------------------------------------------------------
class EBaseException : public Exception {
public:
explicit EBaseException( String Message )
: Exception(
_T( "Modbus exception %s" )
, ARRAYOFCONST( ( Message ) )
) {}
EBaseException( String Msg, TVarRec *Args, int Args_High )
: Exception(
_T( "Modbus exception %s" )
, ARRAYOFCONST( ( Format( Msg, Args, Args_High ) ) )
) {}
};
//---------------------------------------------------------------------------
class EContextException : public EBaseException {
public:
explicit EContextException( Context const & Context, String Message )
: EBaseException( Message )
, context_( Context ) {}
EContextException( Context const & Context, String Msg, TVarRec *Args,
int Args_High )
: EBaseException( Msg, Args, Args_High )
, context_( Context ) {}
Context const & GetContext() const { return context_; }
public:
Context context_;
};
//---------------------------------------------------------------------------
class EProtocolException : public EContextException
{
public:
EProtocolException( Context const & Context, ExceptionCode Code, String Message )
: EContextException( Context, Message )
, code_( Code ) {}
EProtocolException( Context const & Context, ExceptionCode Code, String Msg,
TVarRec *Args, int Args_High )
: EContextException( Context, Msg, Args, Args_High )
, code_( Code ) {}
ExceptionCode GetCode() const { return code_; }
private:
ExceptionCode code_;
};
//---------------------------------------------------------------------------
extern String GetExceptionCodeText( ExceptionCode Code );
extern String GetExceptionCodeDescription( ExceptionCode Code );
#if !defined(__BORLANDC__) || defined(__clang__) // BCC32C, BCC64
[[ noreturn ]] extern void RaiseFunctionCodeNotImplementedException( FunctionCode Code );
[[ noreturn ]] extern void RaiseStandardException( Context const & Context,
ExceptionCode Code,
String Prefix = String() );
#else // BCC32
extern void RaiseFunctionCodeNotImplementedException( FunctionCode Code ) [[noreturn]];
extern void RaiseStandardException( Context const & Context,
ExceptionCode Code,
String Prefix = String() ) [[noreturn]];
#endif
//#endif
//---------------------------------------------------------------------------
template<ExceptionCode Code>
class EProtocolStdException : public EProtocolException
{
public:
EProtocolStdException( Context const & Context, String Prefix = String() )
: EProtocolException(
Context,
Code,
Prefix.IsEmpty() ?
GetExceptionCodeText( Code )
:
Format( _T( "%s: %s" ), ARRAYOFCONST( ( Prefix, GetExceptionCodeText( Code ) ) ) )
)
{}
};
using
EIllegalFunction =
EProtocolStdException<ExceptionCode::IllegalFunction>;
using
EIllegalDataAddress =
EProtocolStdException<ExceptionCode::IllegalDataAddress>;
using
EIllegalDataValue =
EProtocolStdException<ExceptionCode::IllegalDataValue>;
using
ESlaveDeviceFailure =
EProtocolStdException<ExceptionCode::SlaveDeviceFailure>;
using
EAcknowledge =
EProtocolStdException<ExceptionCode::Acknowledge>;
using
ESlaveDeviceBusy =
EProtocolStdException<ExceptionCode::SlaveDeviceBusy>;
using
ENegativeAcknowledge =
EProtocolStdException<ExceptionCode::NegativeAcknowledge>;
using
EMemoryParityError =
EProtocolStdException<ExceptionCode::MemoryParityError>;
using CoilAddrType = uint16_t;
using CoilCountType = uint16_t;
using CoilDataType = uint8_t;
using RegAddrType = uint16_t;
using RegCountType = uint16_t;
using RegDataType = uint16_t;
//---------------------------------------------------------------------------
namespace Master {
//---------------------------------------------------------------------------
class Protocol {
public:
Protocol();
virtual ~Protocol();
void Open();
void Close();
bool IsConnected() const { return DoIsConnected(); }
String GetProtocolName() const { return DoGetProtocolName(); }
String GetProtocolParamsStr() const { return DoGetProtocolParamsStr(); }
// ReadCoilStatus
// ReadInputStatus
void ReadHoldingRegisters( Context const & Context,
RegAddrType StartAddr, RegCountType PointCount,
RegDataType* Data )
{
DoReadHoldingRegisters( Context, StartAddr, PointCount, Data );
}
void ReadInputRegisters( Context const & Context,
RegAddrType StartAddr, RegCountType PointCount,
RegDataType* Data )
{
DoReadInputRegisters( Context, StartAddr, PointCount, Data );
}
// ForceSingleCoil
// PresetSingleRegister
void PresetSingleRegister( Context const & Context,
RegAddrType Addr, RegDataType Data )
{
DoPresetSingleRegister( Context, Addr, Data );
}
// ReadExceptionStatus
// Diagnostics
// Program484
// Poll484
// FetchCommEventCtr
// FetchCommEventLog
// ProgramController
// PollController
// ForceMultipleCoils
void PresetMultipleRegisters( Context const & Context,
RegAddrType StartAddr, RegCountType PointCount,
const RegDataType* Data )
{
DoPresetMultipleRegisters( Context, StartAddr, PointCount, Data );
}
// ReportSlave
// Program884_M84
// ResetCommLink
// ReadGeneralReference
// WriteGeneralReference
// MaskWrite4XRegister
void MaskWrite4XRegister( Context const & Context,
RegAddrType Addr, RegDataType AndMask,
RegDataType OrMask )
{
DoMaskWrite4XRegister( Context, Addr, AndMask, OrMask );
}
// ReadWrite4XRegisters
// ReadFIFOQueue
protected:
virtual String DoGetProtocolName() const = 0;
virtual String DoGetProtocolParamsStr() const = 0;
virtual void DoOpen() = 0;
virtual void DoClose() = 0;
virtual bool DoIsConnected() const = 0;
// DoReadInputStatus
virtual void DoReadHoldingRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
RegDataType* Data ) = 0;
virtual void DoReadInputRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
RegDataType* Data ) = 0;
// DoForceSingleCoil
virtual void DoPresetSingleRegister( Context const & Context,
RegAddrType Addr,
RegDataType Data ) = 0;
// DoReadExceptionStatus
// DoDiagnostics
// DoProgram484
// DoPoll484
// DoFetchCommEventCtr
// DoFetchCommEventLog
// DoProgramController
// DoPollController
// DoForceMultipleCoils
virtual void DoPresetMultipleRegisters( Context const & Context,
RegAddrType StartAddr,
RegCountType PointCount,
const RegDataType* Data ) = 0;
// DoReportSlave
// DoProgram884_M84
// DoResetCommLink
// DoReadGeneralReference
// DoWriteGeneralReference
virtual void DoMaskWrite4XRegister( Context const & Context,
RegAddrType Addr,
RegDataType AndMask,
RegDataType OrMask ) = 0;
// DoReadWrite4XRegisters
// DoReadFIFOQueue
void RaiseExceptionIfIsConnected( String SubMsg ) const;
void RaiseExceptionIfIsNotConnected( String SubMsg ) const;
private:
};
//---------------------------------------------------------------------------
class SessionManager {
public:
explicit SessionManager( Protocol& Protocol )
: protocol_( Protocol ) { protocol_.Open(); }
~SessionManager() { protocol_.Close(); }
SessionManager( SessionManager const & Rhs ) = delete;
SessionManager& operator=( SessionManager const & Rhs ) = delete;
SessionManager( SessionManager&& Rhs ) = delete;
SessionManager& operator=( SessionManager&& Rhs ) = delete;
private:
Protocol& protocol_;
};
//---------------------------------------------------------------------------
}; // End of namespace Master
//---------------------------------------------------------------------------
}; // End of namespace Modbus
//---------------------------------------------------------------------------
#endif