-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus.c
508 lines (439 loc) · 11.6 KB
/
modbus.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#include <xc.h> /* XC8 General Include File */
#include "modbus.h"
#include <plib/usart.h>
#include "system.h"
/******************************************************************************/
/* Global variables */
/******************************************************************************/
extern volatile unsigned int holdingReg[50];
extern volatile unsigned char coils[50];
extern volatile unsigned char response[125]; //Enough to return all holding-r's
extern volatile unsigned char received[125]; //Enough to write all holding-r's
extern volatile char modbusMessage,messageLength;
/******************************************************************************/
/* User Functions */
/******************************************************************************/
void modbusDelay(void)
{
/* Writes to Timer0 for 1.04ms delay*/
TMR1H = Timer1High;
TMR1L = Timer1Low;
}
void clearResponse(void)
{
unsigned char i;
for(i=0;i<125;i++){ //response is 125 long
response[i] = 0;
}
}
void decodeIt(void)
{
if(received[0] == SlaveAddress){
if(checkCRC()){
if(received[1] == 0x01){
readCoil();
}
else if(received[1] == 0x02){
readInputCoil();
}
else if(received[1] == 0x03){
readReg();
}
else if(received[1] == 0x04){
readInputReg();
}
else if(received[1] == 0x05){
writeCoil();
}
else if(received[1] == 0x06){
writeReg();
}
else{
response[0] = 0; //error this does nothing though..
}
}
}
modbusMessage = 0;
}
void readReg(void)
{
unsigned int rr_Address = 0;
unsigned int rr_numRegs = 0;
unsigned char j = 3;
unsigned int crc = 0;
unsigned int i = 0;
//Combine address bytes
rr_Address = received[2];
rr_Address <<= 8;
rr_Address |= received[3];
//Combine number of regs bytes
rr_numRegs = received[4];
rr_numRegs <<= 8;
rr_numRegs |= received[5];
response[0] = SlaveAddress;
response[1] = 0x03;
response[2] = rr_numRegs*2; //2 bytes per reg
for(i=rr_Address;i<(rr_Address + rr_numRegs);i++){
if(holdingReg[i] > 255){
//Need to split it up into 2 bytes
response[j] = holdingReg[i] >> 8;
j++;
response[j] = holdingReg[i];
j++;
}
else{
response[j] = 0x00;
j++;
response[j] = holdingReg[i];
j++;
}
}
crc = generateCRC(j+2);
response[j] = crc >> 8;
j++;
response[j] = crc;
j+=2;
writeEnable = 1;
for(i=0;i!=j;i++){
while(busyUsart); //Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
j=0;
clearResponse();
}
void readInputReg(void)
{
unsigned int rr_Address = 0;
unsigned int rr_numRegs = 0;
unsigned char j = 3;
unsigned int crc = 0;
unsigned int i = 0;
//Combine address bytes
rr_Address = received[2];
rr_Address <<= 8;
rr_Address |= received[3];
//Combine number of regs bytes
rr_numRegs = received[4];
rr_numRegs <<= 8;
rr_numRegs |= received[5];
response[0] = SlaveAddress;
response[1] = 0x04;
response[2] = rr_numRegs*2; //2 bytes per reg
for(i=rr_Address;i<(rr_Address + rr_numRegs);i++){
if(holdingReg[i] > 255){
//Need to split it up into 2 bytes
response[j] = holdingReg[i] >> 8;
j++;
response[j] = holdingReg[i];
j++;
}
else{
response[j] = 0x00;
j++;
response[j] = holdingReg[i];
j++;
}
}
crc = generateCRC(j+2);
response[j] = crc >> 8;
j++;
response[j] = crc;
j+=2;
writeEnable = 1;
for(i=0;i!=j;i++){
while(busyUsart); //Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
j=0;
clearResponse();
}
void writeReg(void)
{
/******************************************************************************/
/* Works out which reg to write and then responds */
/******************************************************************************/
unsigned int wr_AddressLow = 0;
unsigned int wr_AddressHigh = 0;
unsigned int wr_Address = 0;
unsigned int wr_valToWrite = 0;
unsigned int wr_valToWriteLow = 0;
unsigned int wr_valToWriteHigh = 0;
unsigned int crc = 0;
unsigned int i = 0;
//Combine address bytes
wr_Address = received[2];
wr_Address <<= 8;
wr_Address |= received[3];
wr_AddressLow = received[3]; //useful to store
wr_AddressHigh = received[2];
//Combine value to write regs
wr_valToWrite = received[4];
wr_valToWrite <<= 8;
wr_valToWrite |= received[5];
wr_valToWriteLow = received[5];
wr_valToWriteHigh = received[4];
holdingReg[wr_Address] = wr_valToWrite;
response[0] = SlaveAddress;
response[1] = 0x06;
response[3] = wr_AddressLow; //2 bytes per reg
response[2] = wr_AddressHigh;
//TO DO CHECK VALUE IS ACTUALLY WRITTEN//
response[4] = wr_valToWriteHigh;
response[5] = wr_valToWriteLow;
crc = generateCRC(8);
response[6] = crc >> 8;
response[7] = crc;
writeEnable = 1;
for(i=0;i!=9;i++){
while(busyUsart);//Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
clearResponse();
}
void readCoil(void)
{
/******************************************************************************/
/* Reads a coil and then responds */
/******************************************************************************/
unsigned int rc_Address = 0;
unsigned int rc_numCoils = 0;
unsigned int crc = 0;
unsigned char howManyBytes = 0;
unsigned char remainder = 0;
unsigned char lsb = 0;
unsigned char i,j,k,l = 0;
//Combine address bytes
rc_Address = received[2];
rc_Address <<=8;
rc_Address |= received[3];
//Combine number of coils bytes
rc_numCoils = received[4];
rc_numCoils <<= 8;
rc_numCoils |= received[5];
response[0] = SlaveAddress;
response[1] = 0x01;
howManyBytes = rc_numCoils/8;
remainder = rc_numCoils % 8;
if(remainder){
howManyBytes += 1;
}
response[2] = howManyBytes;
l = rc_Address;
k = 3; //start at response 3
for(i=howManyBytes; i!=0; i--){
if(i>1){
for(j=0;j!=8;j++){
if(coils[l]){
lsb = 1;
}
else{
lsb = 0;
}
response[k] ^= (lsb << j);
l++;
}
k++;
}
else{
for(j=0;j!=remainder;j++){
if(coils[l]){
lsb = 1;
}
else{
lsb = 0;
}
response[k] ^= (lsb << j);
l++;
}
k++;
}
}
crc = generateCRC(k+2);
response[k] = crc >> 8;
response[k+1] = crc;
writeEnable = 1;
for(i=0;i!=(k+3);i++){
while(busyUsart);//Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
clearResponse();
}
void readInputCoil(void)
{
/******************************************************************************/
/* Reads a coil and then responds */
/******************************************************************************/
unsigned int rc_Address = 0;
unsigned int rc_numCoils = 0;
unsigned int crc = 0;
unsigned char howManyBytes = 0;
unsigned char remainder = 0;
unsigned char lsb = 0;
unsigned char i,j,k,l = 0;
//Combine address bytes
rc_Address = received[2];
rc_Address <<=8;
rc_Address |= received[3];
//Combine number of coils bytes
rc_numCoils = received[4];
rc_numCoils <<= 8;
rc_numCoils |= received[5];
response[0] = SlaveAddress;
response[1] = 0x02;
howManyBytes = rc_numCoils/8;
remainder = rc_numCoils % 8;
if(remainder){
howManyBytes += 1;
}
response[2] = howManyBytes;
l = rc_Address;
k = 3; //start at response 3
for(i=howManyBytes; i!=0; i--){
if(i>1){
for(j=0;j!=8;j++){
if(coils[l]){
lsb = 1;
}
else{
lsb = 0;
}
response[k] ^= (lsb << j);
l++;
}
k++;
}
else{
for(j=0;j!=remainder;j++){
if(coils[l]){
lsb = 1;
}
else{
lsb = 0;
}
response[k] ^= (lsb << j);
l++;
}
k++;
}
}
crc = generateCRC(k+2);
response[k] = crc >> 8;
response[k+1] = crc;
writeEnable = 1;
for(i=0;i!=(k+3);i++){
while(busyUsart);//Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
clearResponse();
}
void writeCoil(void)
{
/******************************************************************************/
/* Writes to a coil and then responds */
/******************************************************************************/
unsigned int wc_AddressLow = 0;
unsigned int wc_AddressHigh = 0;
unsigned int wc_Address = 0;
unsigned int wc_valToWrite = 0;
unsigned int wc_valToWriteLow = 0;
unsigned int wc_valToWriteHigh = 0;
int i = 0;
unsigned int crc = 0;
//Combine address bytes
wc_Address = received[2];
wc_Address <<= 8;
wc_Address |= received[3];
wc_AddressLow = received[3]; //useful to store
wc_AddressHigh = received[2];
//Combine value to write regs
wc_valToWrite = received[4];
wc_valToWrite <<= 8;
wc_valToWrite |= received[5];
wc_valToWriteLow = received[5];
wc_valToWriteHigh = received[4];
if(wc_valToWrite){
coils[wc_Address] = 0xFF;
}
else{
coils[wc_Address] = 0x00;
}
response[0] = SlaveAddress;
response[1] = 0x02;
response[3] = wc_AddressLow; //2 bytes per reg
response[2] = wc_AddressHigh;
//TO DO CHECK VALUE IS ACTUALLY WRITTEN//
response[4] = wc_valToWriteHigh;
response[5] = wc_valToWriteLow;
crc = generateCRC(8);
response[6] = crc >> 8;
response[7] = crc;
writeEnable = 1;
for(i=0;i!=9;i++){
while(busyUsart);//Change this to Busy1USART for double USART PIC's
TransmitBuffer = response[i];
}
writeEnable = 0;
clearResponse();
}
unsigned int generateCRC(unsigned char messageLength)
{
unsigned int crc = 0xFFFF;
unsigned int crcHigh = 0;
unsigned int crcLow = 0;
int i,j = 0;
for(i=0;i<messageLength-2;i++){
crc ^= response[i];
for(j=8; j!=0; j--){
if((crc & 0x0001) != 0){
crc >>= 1;
crc ^= 0xA001;
}
else{
crc >>= 1;
}
}
}
//bytes are wrong way round so doing a swap here..
crcHigh = (crc & 0x00FF) <<8;
crcLow = (crc & 0xFF00) >>8;
crcHigh |= crcLow;
crc = crcHigh;
return crc;
}
unsigned char checkCRC(void)
{
unsigned int crc = 0xFFFF;
unsigned int crcHigh = 0;
unsigned int crcLow = 0;
int i,j = 0;
for(i=0;i<messageLength-2;i++){
crc ^= received[i];
for(j=8; j!=0; j--){
if((crc & 0x0001) != 0){
crc >>= 1;
crc ^= 0xA001;
}
else{
crc >>= 1;
}
}
}
//bytes are wrong way round so doing a swap here..
crcHigh = (crc & 0x00FF);
crcLow = (crc & 0xFF00) >>8;
if((crcHigh == received[i])&&(crcLow == received[i+1]))
{
return 1;
}
else{
return 0;
}
}