@@ -132,28 +132,68 @@ ModbusRequest::ModbusRequest(uint8_t length) :
132
132
_address(0 ),
133
133
_byteCount(0 ) {}
134
134
135
- ModbusRequest04::ModbusRequest04 (uint8_t slaveAddress, uint16_t address, uint16_t byteCount) :
135
+ ModbusRequest02::ModbusRequest02 (uint8_t slaveAddress, uint16_t address, uint16_t numberCoils) :
136
+ ModbusRequest(8 ) {
137
+ _slaveAddress = slaveAddress;
138
+ _functionCode = READ_DISCR_INPUT;
139
+ _address = address;
140
+ _byteCount = numberCoils / 8 + 1 ;
141
+ add (_slaveAddress);
142
+ add (_functionCode);
143
+ add (high (_address));
144
+ add (low (_address));
145
+ add (high (numberCoils));
146
+ add (low (numberCoils));
147
+ uint16_t CRC = CRC16 (_buffer, 6 );
148
+ add (low (CRC));
149
+ add (high (CRC));
150
+ }
151
+
152
+ size_t ModbusRequest02::responseLength () {
153
+ return 5 + _byteCount;
154
+ }
155
+
156
+ ModbusRequest03::ModbusRequest03 (uint8_t slaveAddress, uint16_t address, uint16_t numberRegisters) :
157
+ ModbusRequest(12 ) {
158
+ _slaveAddress = slaveAddress;
159
+ _functionCode = READ_HOLD_REGISTER;
160
+ _address = address;
161
+ _byteCount = numberRegisters * 2 ; // register is 2 bytes wide
162
+ add (_slaveAddress);
163
+ add (_functionCode);
164
+ add (high (_address));
165
+ add (low (_address));
166
+ add (high (numberRegisters));
167
+ add (low (numberRegisters));
168
+ uint16_t CRC = CRC16 (_buffer, 6 );
169
+ add (low (CRC));
170
+ add (high (CRC));
171
+ }
172
+
173
+ size_t ModbusRequest03::responseLength () {
174
+ return 5 + _byteCount;
175
+ }
176
+
177
+ ModbusRequest04::ModbusRequest04 (uint8_t slaveAddress, uint16_t address, uint16_t numberRegisters) :
136
178
ModbusRequest(8 ) {
137
179
_slaveAddress = slaveAddress;
138
180
_functionCode = READ_INPUT_REGISTER;
139
181
_address = address;
140
- _byteCount = byteCount;
182
+ _byteCount = numberRegisters * 2 ; // register is 2 bytes wide
141
183
add (_slaveAddress);
142
184
add (_functionCode);
143
185
add (high (_address));
144
186
add (low (_address));
145
- add (high (_byteCount ));
146
- add (low (_byteCount ));
187
+ add (high (numberRegisters ));
188
+ add (low (numberRegisters ));
147
189
uint16_t CRC = CRC16 (_buffer, 6 );
148
190
add (low (CRC));
149
191
add (high (CRC));
150
192
}
151
193
152
- ModbusResponse* ModbusRequest04::makeResponse () {
194
+ size_t ModbusRequest04::responseLength () {
153
195
// slaveAddress (1) + functionCode (1) + byteCount (1) + length x 2 + CRC (2)
154
- uint8_t responseLength = 3 + (_byteCount * 2 ) + 2 ;
155
- ModbusResponse* response = new ModbusResponse (responseLength, this );
156
- return response;
196
+ return 5 + _byteCount;
157
197
}
158
198
159
199
ModbusResponse::ModbusResponse (uint8_t length, ModbusRequest* request) :
@@ -165,9 +205,7 @@ bool ModbusResponse::isComplete() {
165
205
if (_buffer[1 ] > 0x80 && _index == 4 ) { // 4: slaveAddress(1), errorCode(1), CRC(2)
166
206
return true ;
167
207
}
168
- if (_index > _buffer[2 ] + 4 ) {
169
- return true ;
170
- }
208
+ if (_index == _request->responseLength ()) return true ;
171
209
return false ;
172
210
}
173
211
@@ -191,7 +229,7 @@ bool ModbusResponse::isSucces() {
191
229
192
230
bool ModbusResponse::checkCRC () {
193
231
uint16_t CRC = CRC16 (_buffer, _length - 2 );
194
- if (low (CRC) == _buffer[_length - 2 ] && high (CRC) == _buffer[_length -1 ]) {
232
+ if (low (CRC) == _buffer[_length - 2 ] && high (CRC) == _buffer[_length -1 ]) {
195
233
return true ;
196
234
} else {
197
235
return false ;
0 commit comments