-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodbusTCP.cpp
More file actions
213 lines (155 loc) · 10.2 KB
/
modbusTCP.cpp
File metadata and controls
213 lines (155 loc) · 10.2 KB
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
#include "modbusTCP.h"
modbus::modbus () {}
void modbus::DigitalRead(uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, byte *Data)
{
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;} // Prepare the first 5 bytes of the slave reply
for ( count = 0; count < 2; count++){*Mstrquery = 0; Mstrquery++;}
FC = *Mstrquery;
for ( count = 0; count < 6; count++){Buffer[count] = *Mstrquery; *Mstrquery = 0; Mstrquery++;} // Extrapolate relevant data from the modbus query
Start = word(Buffer[3],Buffer[2]); // Calculate the offset from zero (First byte of data to read)
WordDataLenght = word(Buffer[5],Buffer[4])-1; // Number of inputs or coils to read
ByteDataLenght = (WordDataLenght/8)+1; // Number of bytes needed to store the inputs or coils data, one byte is 8 digital status
*SlvReply = ByteDataLenght + 3; // Number of total bytes transmitted after this one
SlvReply++;
*SlvReply = 1; // Set the slave address
SlvReply++;
*SlvReply = FC; // Set the function code
SlvReply++;
*SlvReply = ByteDataLenght; // Number of total bytes transmitted after this one, containing inputs or coils data
for( count = 0; count < Start; count++) {Data++;} // Shift the data array start position acording to the offset requested by the query
for( count = 0; count < ByteDataLenght; count++){SlvReply++; *SlvReply = *Data; Data++;} // Copyt the inputs or coils data into the slave reply array
*MsgLenght = ByteDataLenght + 9 ; // Lenght of the slave reply ( Total number of bytes )
PrintData (SlvReply,MsgLenght);
}
void modbus::AnalogRead (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, word *Data)
{
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;} // Prepare the first 5 bytes of the slave reply
for ( count = 0; count < 2; count++){*Mstrquery = 0; Mstrquery++;}
FC = *Mstrquery;
for ( count = 0; count < 6; count++){ Buffer[count] = *Mstrquery; *Mstrquery = 0; Mstrquery++;} // Extrapolate relevant data from the modbus query
Start = word(Buffer[3],Buffer[2]); // Calculate the offset from zero (First byte of data to read)
WordDataLenght = word(Buffer[5],Buffer[4]); // Number of holding or input registers to read
ByteDataLenght = WordDataLenght * 2; // Number of bytes needed to store the holding or input registers, one byte is 8 digital status
*SlvReply = ByteDataLenght + 3; // Number of total bytes transmitted after this one, containing inputs or holding registers data
SlvReply++;
*SlvReply = 1; // Set the slave address
SlvReply++;
*SlvReply = FC; // Set the function code
SlvReply++;
*SlvReply = ByteDataLenght; // Number of total bytes transmitted after this one, containing inputs or hoding registers data
for( count = 0; count < Start; count++) {Data++;} // Shift the data array start position acording to the offset requested by the query
for( count = 0; count < WordDataLenght; count++){ // Copyt the inputs or holding registers data into the slave reply array
SlvReply++;
*SlvReply = highByte(*Data); // Add the highbyte of the input or holdning register data in the slave reply array
SlvReply++;
*SlvReply = lowByte(*Data); // Add the lowbyte of the input or holdning register data in the slave reply array
Data++; }
*MsgLenght = ByteDataLenght + 9 ; // Lenght of the slave reply ( Total number of bytes )
PrintData (SlvReply,MsgLenght);
}
void modbus::DigitalWrite (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, byte *Data)
{
for ( count = 0; count < 8; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
for ( count = 0; count < 2; count++){*SlvReply = *Mstrquery;Buffer[count] = *SlvReply;*Mstrquery = 0; Mstrquery++; SlvReply++;}
Start= word(Buffer[0],Buffer[1]);
for ( count = 0; count < Start; count++){Data++;}
*Data = *Mstrquery ;
*SlvReply = *Mstrquery;
SlvReply++;
*SlvReply = 0;
*MsgLenght = 12;
PrintData (SlvReply,MsgLenght);
}
void modbus::AnalogWrite (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, word *Data)
{
for ( count = 0; count < 8; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
for ( count = 0; count < 4; count++){*SlvReply = *Mstrquery; Buffer[count] = *SlvReply;*Mstrquery = 0; Mstrquery++; SlvReply++;}
Start = word(Buffer[0],Buffer[1]);
DataByte = word(Buffer[2],Buffer[3]);
for ( count = 0; count < Start; count++){Data++;}
*Data = DataByte;
*MsgLenght = 12;
SlvReply--;
PrintData (SlvReply,MsgLenght);
}
void modbus::MultiCoilsWrite (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, byte *Data)
{
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
*MsgLenght= *Mstrquery + 6;
for ( count = 0; count < 8; count++){*SlvReply = *Mstrquery; Buffer[count] = *SlvReply; *Mstrquery = 0; Mstrquery++; SlvReply++;}
Start = word(Buffer [5],Buffer [4]);
ByteDataLenght = Buffer [7];
for ( count = 0; count < Start; count++){Data++;}
for ( count = 0; count < ByteDataLenght; count++){*SlvReply = *Mstrquery; *Data = *SlvReply; *Mstrquery = 0; Mstrquery++; SlvReply++; Data++;}
SlvReply--;
PrintData (SlvReply,MsgLenght);
}
void modbus::MultiRegsWrite (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, word *Data)
{
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
*MsgLenght= *Mstrquery + 6;
for ( count = 0; count < 8; count++){*SlvReply = *Mstrquery; Buffer[count] = *SlvReply; *Mstrquery = 0; Mstrquery++; SlvReply++;}
Start = word(Buffer [3],Buffer [4]);
ByteDataLenght = Buffer [6]*2;
for ( count = 0; count < Start; count++){Data++;}
for ( count = 0; count < ByteDataLenght; count++){*SlvReply = *Mstrquery; Buffer[count] = *SlvReply; *Mstrquery = 0; Mstrquery++; SlvReply++;}
for ( count = 0; count < ByteDataLenght; count++){ DataByte = word (Buffer[count],Buffer[count+1]); *Data = DataByte; Data++; count++;}
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
for ( count = 0; count < 6; count++){SlvReply--;}
PrintData (SlvReply,MsgLenght);
}
void modbus::MaskWriteRegister (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, word *Data)
{
for ( count = 0; count < 5; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
*MsgLenght= *Mstrquery + 6;
for ( count = 0; count < 9; count++){*SlvReply = *Mstrquery; Buffer[count] = *SlvReply; *Mstrquery = 0; Mstrquery++; SlvReply++;}
Start = word(Buffer [3],Buffer [4]);
AndMask = word(Buffer [5],Buffer [6]);
OrMask = word(Buffer [7],Buffer [8]);
for ( count = 0; count < Start; count++){Data++;}
*Data = (*Data | OrMask ) & AndMask;
SlvReply--;
PrintData (SlvReply,MsgLenght);
}
void modbus::ReadWriteMultiReg (uint8_t *Mstrquery,uint8_t *SlvReply,uint8_t *MsgLenght, word *Data)
{
for ( count = 0; count < 8; count++){*SlvReply = *Mstrquery; *Mstrquery = 0; Mstrquery++; SlvReply++;}
for ( count = 0; count < 9; count++){Buffer[count] = *Mstrquery; *Mstrquery = 0; Mstrquery++;}
Start = word(Buffer [0],Buffer [1]);
ByteDataLenght = word(Buffer [2],Buffer [3]);
WordDataLenght = ByteDataLenght * 2;
*MsgLenght = WordDataLenght + 9;
for( count = 0; count < Start; count++) {Data++;} // Shift the data array start position acording to the offset requested by the query
*SlvReply = WordDataLenght;
SlvReply++;
for( count = 0; count < WordDataLenght; count++){ // Copyt the inputs or holding registers data into the slave reply array
*SlvReply = highByte(*Data); // Add the highbyte of the input or holdning register data in the slave reply array
SlvReply++;
*SlvReply = lowByte(*Data); // Add the lowbyte of the input or holdning register data in the slave reply array
SlvReply++;
Data++; }
for( count = 0; count < Start; count++) {Data--;}
for( count = 0; count < WordDataLenght; count++) {Data--;}
Start = word(Buffer [4],Buffer [5]);
ByteDataLenght = Buffer [8];
for ( count = 0; count < Start; count++){Data++;}
for ( count = 0; count < ByteDataLenght; count++){ Buffer[count] = *Mstrquery; *Mstrquery = 0; Mstrquery++; }
for ( count = 0; count < ByteDataLenght; count++){ DataByte = word (Buffer[count],Buffer[count+1]); *Data = DataByte; Data++; count++;}
for ( count = 0; count < WordDataLenght + 1; count++){SlvReply--;}
PrintData (SlvReply,MsgLenght);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
void PrintData (uint8_t *SlvReply,uint8_t *MsgLenght)
{
for( int count = 1; count < *MsgLenght; count++) {SlvReply--;} // Inizialize the slave reply pointer
Serial.print("Slave Reply = "); // Print the slave reply data
for( int count = 0; count < *MsgLenght ; count++){
Serial.print ((*SlvReply) + String (" "));
SlvReply++;}
Serial.println ("");
Serial.print ("MessageLenght = ");
Serial.println (*MsgLenght);
Serial.println ("Ready for the net query!");
Serial.println ("");
}