-
Notifications
You must be signed in to change notification settings - Fork 0
/
MgsModbus.cpp
320 lines (301 loc) · 10.6 KB
/
MgsModbus.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
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
#include "MgsModbus.h"
// For Arduino 1.0
EthernetServer MbServer(MB_PORT);
EthernetClient MbmClient;
// #define DEBUG
MgsModbus::MgsModbus()
{
}
//****************** Send data for ModBusMaster ****************
void MgsModbus::Req(MB_FC FC, word Ref, word Count, word Pos)
{
MbmFC = FC;
byte ServerIp[] = {192,168,200,163};
MbmByteArray[0] = 0; // ID high byte
MbmByteArray[1] = 1; // ID low byte
MbmByteArray[2] = 0; // protocol high byte
MbmByteArray[3] = 0; // protocol low byte
MbmByteArray[5] = 6; // Lenght low byte;
MbmByteArray[4] = 0; // Lenght high byte
MbmByteArray[6] = 1; // unit ID
MbmByteArray[7] = FC; // function code
MbmByteArray[8] = highByte(Ref);
MbmByteArray[9] = lowByte(Ref);
//****************** Read Coils (1) & Read Input discretes (2) **********************
if(FC == MB_FC_READ_COILS || FC == MB_FC_READ_DISCRETE_INPUT) {
if (Count < 1) {Count = 1;}
if (Count > 125) {Count = 2000;}
MbmByteArray[10] = highByte(Count);
MbmByteArray[11] = lowByte(Count);
}
//****************** Read Registers (3) & Read Input registers (4) ******************
if(FC == MB_FC_READ_REGISTERS || FC == MB_FC_READ_INPUT_REGISTER) {
if (Count < 1) {Count = 1;}
if (Count > 125) {Count = 125;}
MbmByteArray[10] = highByte(Count);
MbmByteArray[11] = lowByte(Count);
}
//****************** Write Coil (5) **********************
if(MbmFC == MB_FC_WRITE_COIL) {
if (GetBit(Pos)) {MbmByteArray[10] = 0xFF;} else {MbmByteArray[10] = 0;} // 0xFF coil on 0x00 coil off
MbmByteArray[11] = 0; // always zero
}
//****************** Write Register (6) ******************
if(MbmFC == MB_FC_WRITE_REGISTER) {
MbmByteArray[10] = highByte(MbData[Pos]);
MbmByteArray[11] = lowByte(MbData[Pos]);
}
//****************** Write Multiple Coils (15) **********************
// not fuly tested
if(MbmFC == MB_FC_WRITE_MULTIPLE_COILS) {
if (Count < 1) {Count = 1;}
if (Count > 800) {Count = 800;}
MbmByteArray[10] = highByte(Count);
MbmByteArray[11] = lowByte(Count);
MbmByteArray[12] = (Count + 7) /8;
MbmByteArray[4] = highByte(MbmByteArray[12] + 7); // Lenght high byte
MbmByteArray[5] = lowByte(MbmByteArray[12] + 7); // Lenght low byte;
for (int i=0; i<Count; i++) {
bitWrite(MbmByteArray[13+(i/8)],i-((i/8)*8),GetBit(Pos+i));
}
}
//****************** Write Multiple Registers (16) ******************
if(MbmFC == MB_FC_WRITE_MULTIPLE_REGISTERS) {
if (Count < 1) {Count = 1;}
if (Count > 100) {Count = 100;}
MbmByteArray[10] = highByte(Count);
MbmByteArray[11] = lowByte(Count);
MbmByteArray[12] = (Count*2);
MbmByteArray[4] = highByte(MbmByteArray[12] + 7); // Lenght high byte
MbmByteArray[5] = lowByte(MbmByteArray[12] + 7); // Lenght low byte;
for (int i=0; i<Count;i++) {
MbmByteArray[(i*2)+13] = highByte (MbData[Pos + i]);
MbmByteArray[(i*2)+14] = lowByte (MbData[Pos + i]);
}
}
//****************** ?? ******************
if (MbmClient.connect(ServerIp,502)) {
#ifdef DEBUG
Serial.println("connected with modbus slave");
Serial.print("Master request: ");
for(int i=0;i<MbmByteArray[5]+6;i++) {
if(MbmByteArray[i] < 16){Serial.print("0");}
Serial.print(MbmByteArray[i],HEX);
if (i != MbmByteArray[5]+5) {Serial.print(".");} else {Serial.println();}
}
#endif
for(int i=0;i<MbmByteArray[5]+6;i++) {
MbmClient.write(MbmByteArray[i]);
}
MbmCounter = 0;
MbmByteArray[7] = 0;
MbmPos = Pos;
MbmBitCount = Count;
} else {
#ifdef DEBUG
Serial.println("connection with modbus master failed");
#endif
MbmClient.stop();
}
}
//****************** Recieve data for ModBusMaster ****************
void MgsModbus::MbmRun()
{
//****************** Read from socket ****************
while (MbmClient.available()) {
MbmByteArray[MbmCounter] = MbmClient.read();
if (MbmCounter > 4) {
if (MbmCounter == MbmByteArray[5] + 5) { // the full answer is recieved
MbmClient.stop();
MbmProcess();
#ifdef DEBUG
Serial.println("recieve klaar");
#endif
}
}
MbmCounter++;
}
}
void MgsModbus::MbmProcess()
{
MbmFC = SetFC(int (MbmByteArray[7]));
#ifdef DEBUG
for (int i=0;i<MbmByteArray[5]+6;i++) {
if(MbmByteArray[i] < 16) {Serial.print("0");}
Serial.print(MbmByteArray[i],HEX);
if (i != MbmByteArray[5]+5) {Serial.print(".");
} else {Serial.println();}
}
#endif
//****************** Read Coils (1) & Read Input discretes (2) **********************
if(MbmFC == MB_FC_READ_COILS || MbmFC == MB_FC_READ_DISCRETE_INPUT) {
word Count = MbmByteArray[8] * 8;
if (MbmBitCount < Count) {
Count = MbmBitCount;
}
for (int i=0;i<Count;i++) {
if (i + MbmPos < MbDataLen * 16) {
SetBit(i + MbmPos,bitRead(MbmByteArray[(i/8)+9],i-((i/8)*8)));
}
}
}
//****************** Read Registers (3) & Read Input registers (4) ******************
if(MbmFC == MB_FC_READ_REGISTERS || MbmFC == MB_FC_READ_INPUT_REGISTER) {
word Pos = MbmPos;
for (int i=0;i<MbmByteArray[8];i=i+2) {
if (Pos < MbDataLen) {
MbData[Pos] = (MbmByteArray[i+9] * 0x100) + MbmByteArray[i+1+9];
Pos++;
}
}
}
//****************** Write Coil (5) **********************
if(MbmFC == MB_FC_WRITE_COIL){
}
//****************** Write Register (6) ******************
if(MbmFC == MB_FC_WRITE_REGISTER){
}
//****************** Write Multiple Coils (15) **********************
if(MbmFC == MB_FC_WRITE_MULTIPLE_COILS){
}
//****************** Write Multiple Registers (16) ******************
if(MbmFC == MB_FC_WRITE_MULTIPLE_REGISTERS){
}
}
//****************** Recieve data for ModBusSlave ****************
void MgsModbus::MbsRun()
{
//****************** Read from socket ****************
EthernetClient client = MbServer.available();
if(client.available())
{
delay(10);
int i = 0;
while(client.available())
{
MbsByteArray[i] = client.read();
i++;
}
MbsFC = SetFC(MbsByteArray[7]); //Byte 7 of request is FC
}
int Start, WordDataLength, ByteDataLength, CoilDataLength, MessageLength;
//****************** Read Coils (1 & 2) **********************
if(MbsFC == MB_FC_READ_COILS || MbsFC == MB_FC_READ_DISCRETE_INPUT) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
CoilDataLength = word(MbsByteArray[10],MbsByteArray[11]);
ByteDataLength = CoilDataLength / 8;
if(ByteDataLength * 8 < CoilDataLength) ByteDataLength++;
CoilDataLength = ByteDataLength * 8;
MbsByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
MbsByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
for(int i = 0; i < ByteDataLength ; i++)
{
MbsByteArray[9 + i] = 0; // To get all remaining not written bits zero
for(int j = 0; j < 8; j++)
{
bitWrite(MbsByteArray[9 + i], j, GetBit(Start + i * 8 + j));
}
}
MessageLength = ByteDataLength + 9;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
//****************** Read Registers (3 & 4) ******************
if(MbsFC == MB_FC_READ_REGISTERS || MbsFC == MB_FC_READ_INPUT_REGISTER) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
WordDataLength = word(MbsByteArray[10],MbsByteArray[11]);
ByteDataLength = WordDataLength * 2;
MbsByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
MbsByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
for(int i = 0; i < WordDataLength; i++)
{
MbsByteArray[ 9 + i * 2] = highByte(MbData[Start + i]);
MbsByteArray[10 + i * 2] = lowByte(MbData[Start + i]);
}
MessageLength = ByteDataLength + 9;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
//****************** Write Coil (5) **********************
if(MbsFC == MB_FC_WRITE_COIL) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
if (word(MbsByteArray[10],MbsByteArray[11]) == 0xFF00){SetBit(Start,true);}
if (word(MbsByteArray[10],MbsByteArray[11]) == 0x0000){SetBit(Start,false);}
MbsByteArray[5] = 6; //Number of bytes after this one.
MessageLength = 12;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
//****************** Write Register (6) ******************
if(MbsFC == MB_FC_WRITE_REGISTER) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
MbData[Start] = word(MbsByteArray[10],MbsByteArray[11]);
MbsByteArray[5] = 6; //Number of bytes after this one.
MessageLength = 12;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
//****************** Write Multiple Coils (15) **********************
if(MbsFC == MB_FC_WRITE_MULTIPLE_COILS) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
CoilDataLength = word(MbsByteArray[10],MbsByteArray[11]);
MbsByteArray[5] = 6;
for(int i = 0; i < CoilDataLength; i++)
{
SetBit(Start + i,bitRead(MbsByteArray[13 + (i/8)],i-((i/8)*8)));
}
MessageLength = 12;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
//****************** Write Multiple Registers (16) ******************
if(MbsFC == MB_FC_WRITE_MULTIPLE_REGISTERS) {
Start = word(MbsByteArray[8],MbsByteArray[9]);
WordDataLength = word(MbsByteArray[10],MbsByteArray[11]);
ByteDataLength = WordDataLength * 2;
MbsByteArray[5] = 6;
for(int i = 0; i < WordDataLength; i++)
{
MbData[Start + i] = word(MbsByteArray[ 13 + i * 2],MbsByteArray[14 + i * 2]);
}
MessageLength = 12;
client.write(MbsByteArray, MessageLength);
MbsFC = MB_FC_NONE;
}
}
//****************** ?? ******************
MB_FC MgsModbus::SetFC(int fc)
{
MB_FC FC;
FC = MB_FC_NONE;
if(fc == 1) FC = MB_FC_READ_COILS;
if(fc == 2) FC = MB_FC_READ_DISCRETE_INPUT;
if(fc == 3) FC = MB_FC_READ_REGISTERS;
if(fc == 4) FC = MB_FC_READ_INPUT_REGISTER;
if(fc == 5) FC = MB_FC_WRITE_COIL;
if(fc == 6) FC = MB_FC_WRITE_REGISTER;
if(fc == 15) FC = MB_FC_WRITE_MULTIPLE_COILS;
if(fc == 16) FC = MB_FC_WRITE_MULTIPLE_REGISTERS;
return FC;
}
word MgsModbus::GetDataLen()
{
return MbDataLen;
}
boolean MgsModbus::GetBit(word Number)
{
int ArrayPos = Number / 16;
int BitPos = Number - ArrayPos * 16;
boolean Tmp = bitRead(MbData[ArrayPos],BitPos);
return Tmp;
}
boolean MgsModbus::SetBit(word Number,boolean Data)
{
int ArrayPos = Number / 16;
int BitPos = Number - ArrayPos * 16;
boolean Overrun = ArrayPos > MbDataLen * 16; // check for data overrun
if (!Overrun){
bitWrite(MbData[ArrayPos],BitPos,Data);
}
return Overrun;
}