Modbus TCP 0.1.0

ModbusTCP.h
Go to the documentation of this file.
1
42 #ifndef Modbus_TCPIP_h
43 #define Modbus_TCPIP_h
44
45 #define WIZNET_W5100 0
46 #define ENC28J60 1
50 /* _____STANDARD INCLUDES____________________________________________________ */
51 // include types & constants of Wiring core API
52 #if defined(ARDUINO) && ARDUINO >= 100
53 #include "Arduino.h"
54 #else
55 #include "WProgram.h"
56 #endif
57
58 #include <SPI.h>
59
60 #if WIZNET_W5100
61 #include <Ethernet.h>
62 #endif
63
64 #if ENC28J60
65 #include <UIPEthernet.h>
66 #endif
67
68
69 /* _____PROJECT INCLUDES_____________________________________________________ */
70
71 // functions to manipulate words
72 #include "util/word.h"
73
74
75 /* _____CLASS DEFINITIONS____________________________________________________ */
79 class ModbusTCP
80 {
81 public:
82
83 IPAddress serverIP;
84
85 #if WIZNET_W5100
86 EthernetClient ModbusClient;
87 #elif ENC28J60
88 UIPClient ModbusClient;
89 #endif
90
91 char MBconnectionFlag = 0;
92
93 ModbusTCP();
94 ModbusTCP(uint8_t);
95 void setUnitId(uint8_t);
96 void setTransactionID(uint16_t);
97 void setServerIPAddress(IPAddress);
98 void idle(void (*)());
99
100 // Modbus exception codes
113 static const uint8_t MBIllegalFunction = 0x01;
114
133 static const uint8_t MBIllegalDataAddress = 0x02;
134
148 static const uint8_t MBIllegalDataValue = 0x03;
149 //static const uint8_t MBIllegalResponseLength = 0x04;
150
160 static const uint8_t MBServerConnectionTimeOut = 0x05;
161
162 // Class-defined success/exception codes
174 static const uint8_t MBSuccess = 0x00;
175
183 static const uint8_t MBInvalidTransactionID = 0xE0;
184
192 static const uint8_t MBInvalidFunction = 0xE1;
193
202 static const uint8_t MBResponseTimedOut = 0xE2;
203
211 static const uint8_t MBInvalidUnitID = 0xE3;
212 static const uint8_t MBInvalidProtocol = 0xE4;
213
214 uint8_t getResponseBufferLength();
215 uint16_t getResponseBuffer(uint8_t);
216 void clearResponseBuffer();
217 uint8_t setTransmitBuffer(uint8_t, uint16_t);
218 void clearTransmitBuffer();
219
220
221 uint8_t readCoils(uint16_t, uint16_t);
222 uint8_t readDiscreteInputs(uint16_t, uint16_t);
223 uint8_t readHoldingRegisters(uint16_t, uint16_t);
224 uint8_t readInputRegisters(uint16_t, uint8_t);
225 uint8_t writeSingleCoil(uint16_t, uint8_t);
226 uint8_t writeSingleRegister(uint16_t, uint16_t);
227 uint8_t writeMultipleCoils(uint16_t, uint16_t);
228 uint8_t writeMultipleRegisters(uint16_t, uint16_t);
229 uint8_t maskWriteRegister(uint16_t, uint16_t, uint16_t);
230 uint8_t readWriteMultipleRegisters(uint16_t, uint16_t, uint16_t, uint16_t);
231
232 private:
233
234 uint8_t _u8MBUnitID;
235 uint16_t _u16MBTransactionID = 1;
236 uint16_t _u16MBProtocolID = 0;
237 static const uint8_t MaxBufferSize = 64;
238 uint16_t _u16ReadAddress;
239 uint16_t _u16ReadQty;
240 uint16_t _u16TxRxBuffer[MaxBufferSize];
241 uint16_t _u16WriteAddress;
242 uint16_t _u16WriteQty;
243 uint8_t _u8ResponseBufferLength;
244
245 // Modbus function codes for bit access
246 static const uint8_t MBReadCoils = 0x01;
247 static const uint8_t MBReadDiscreteInputs = 0x02;
248 static const uint8_t MBWriteSingleCoil = 0x05;
249 static const uint8_t MBWriteMultipleCoils = 0x0F;
250
251 // Modbus function codes for 16 bit access
252 static const uint8_t MBReadHoldingRegisters = 0x03;
253 static const uint8_t MBReadInputRegisters = 0x04;
254 static const uint8_t MBWriteSingleRegister = 0x06;
255 static const uint8_t MBWriteMultipleRegisters = 0x10;
256 static const uint8_t MBMaskWriteRegister = 0x16;
257 static const uint8_t MBReadWriteMultipleRegisters = 0x17;
258
259
260 static const uint16_t ku16MBResponseTimeout = 2000;
261
262 // master function that conducts Modbus transactions
263 uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);
264
265 // idle callback function; gets called during idle time between TX and RX
266 void (*_idle)();
267 };
268 #endif
269
ModbusTCP
Definition: ModbusTCP.h:79
ModbusTCP::setUnitId
void setUnitId(uint8_t)
Definition: ModbusTCP.cpp:65
ModbusTCP::readHoldingRegisters
uint8_t readHoldingRegisters(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:270
ModbusTCP::getResponseBuffer
uint16_t getResponseBuffer(uint8_t)
Definition: ModbusTCP.cpp:122
ModbusTCP::writeMultipleRegisters
uint8_t writeMultipleRegisters(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:387
ModbusTCP::writeMultipleCoils
uint8_t writeMultipleCoils(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:364
ModbusTCP::readWriteMultipleRegisters
uint8_t readWriteMultipleRegisters(uint16_t, uint16_t, uint16_t, uint16_t)
Definition: ModbusTCP.cpp:448
ModbusTCP::clearTransmitBuffer
void clearTransmitBuffer()
Definition: ModbusTCP.cpp:180
ModbusTCP::clearResponseBuffer
void clearResponseBuffer()
Definition: ModbusTCP.cpp:141
ModbusTCP::ModbusTCP
ModbusTCP()
Definition: ModbusTCP.cpp:41
ModbusTCP::writeSingleRegister
uint8_t writeSingleRegister(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:338
ModbusTCP::readCoils
uint8_t readCoils(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:214
ModbusTCP::getResponseBufferLength
uint8_t getResponseBufferLength()
Definition: ModbusTCP.cpp:107
ModbusTCP::MBIllegalDataAddress
static const uint8_t MBIllegalDataAddress
Definition: ModbusTCP.h:133
ModbusTCP::readDiscreteInputs
uint8_t readDiscreteInputs(uint16_t, uint16_t)
Definition: ModbusTCP.cpp:245
ModbusTCP::setTransactionID
void setTransactionID(uint16_t)
Definition: ModbusTCP.cpp:77
ModbusTCP::idle
void idle(void(*)())
Definition: ModbusTCP.cpp:93
ModbusTCP::MBInvalidProtocol
static const uint8_t MBInvalidProtocol
Definition: ModbusTCP.h:212
ModbusTCP::setServerIPAddress
void setServerIPAddress(IPAddress)
Definition: ModbusTCP.cpp:71
ModbusTCP::MBconnectionFlag
char MBconnectionFlag
Definition: ModbusTCP.h:91
ModbusTCP::MBServerConnectionTimeOut
static const uint8_t MBServerConnectionTimeOut
Definition: ModbusTCP.h:160
ModbusTCP::MBIllegalFunction
static const uint8_t MBIllegalFunction
Definition: ModbusTCP.h:113
ModbusTCP::MBResponseTimedOut
static const uint8_t MBResponseTimedOut
Definition: ModbusTCP.h:202
ModbusTCP::serverIP
IPAddress serverIP
Definition: ModbusTCP.h:83
ModbusTCP::MBSuccess
static const uint8_t MBSuccess
Definition: ModbusTCP.h:174
ModbusTCP::ModbusClient
UIPClient ModbusClient
Definition: ModbusTCP.h:88
ModbusTCP::readInputRegisters
uint8_t readInputRegisters(uint16_t, uint8_t)
Definition: ModbusTCP.cpp:295
ModbusTCP::maskWriteRegister
uint8_t maskWriteRegister(uint16_t, uint16_t, uint16_t)
Definition: ModbusTCP.cpp:418
ModbusTCP::MBInvalidTransactionID
static const uint8_t MBInvalidTransactionID
Definition: ModbusTCP.h:183
ModbusTCP::writeSingleCoil
uint8_t writeSingleCoil(uint16_t, uint8_t)
Definition: ModbusTCP.cpp:318
ModbusTCP::setTransmitBuffer
uint8_t setTransmitBuffer(uint8_t, uint16_t)
Definition: ModbusTCP.cpp:160
ModbusTCP::MBInvalidUnitID
static const uint8_t MBInvalidUnitID
Definition: ModbusTCP.h:211
ModbusTCP::MBIllegalDataValue
static const uint8_t MBIllegalDataValue
Definition: ModbusTCP.h:148
ModbusTCP::MBInvalidFunction
static const uint8_t MBInvalidFunction
Definition: ModbusTCP.h:192