-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbm_core.c
1720 lines (1458 loc) · 69.5 KB
/
mbm_core.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*********************************************************************************************************
* uC/Modbus
* The Embedded Modbus Stack
*
* Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* uC/MODBUS MASTER COMMAND PROCESSOR
*
* Filename : mbm_core.c
* Version : V2.14.00
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MBM_MODULE
#include "mb.h"
#if (MODBUS_CFG_MASTER_EN == DEF_ENABLED)
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL MACRO'S
*********************************************************************************************************
*/
#define MBM_TX_FRAME (&pch->TxFrame)
#define MBM_TX_FRAME_NBYTES (pch->TxFrameNDataBytes)
#define MBM_TX_FRAME_SLAVE_ADDR (pch->TxFrameData[0])
#define MBM_TX_FRAME_FC (pch->TxFrameData[1])
#define MBM_TX_FRAME_FC01_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC01_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC01_NBR_POINTS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC01_NBR_POINTS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC02_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC02_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC02_NBR_POINTS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC02_NBR_POINTS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC03_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC03_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC03_NBR_POINTS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC03_NBR_POINTS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC04_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC04_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC04_NBR_POINTS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC04_NBR_POINTS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC05_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC05_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC05_FORCE_DATA_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC05_FORCE_DATA_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC06_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC06_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC06_DATA_ADDR (&pch->TxFrameData[4])
#define MBM_TX_FRAME_FC06_DATA_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC06_DATA_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC08_FNCT_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC08_FNCT_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC08_FNCT_DATA_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC08_FNCT_DATA_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC15_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC15_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC15_NBR_POINTS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC15_NBR_POINTS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC15_BYTE_CNT (pch->TxFrameData[6])
#define MBM_TX_FRAME_FC15_DATA (&pch->TxFrameData[7])
#define MBM_TX_FRAME_FC16_ADDR_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_FC16_ADDR_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_FC16_NBR_REGS_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_FC16_NBR_REGS_LO (pch->TxFrameData[5])
#define MBM_TX_FRAME_FC16_BYTE_CNT (pch->TxFrameData[6])
#define MBM_TX_FRAME_FC16_DATA (&pch->TxFrameData[7])
#define MBM_TX_FRAME_DIAG_FNCT_HI (pch->TxFrameData[2])
#define MBM_TX_FRAME_DIAG_FNCT_LO (pch->TxFrameData[3])
#define MBM_TX_FRAME_DIAG_FNCT_DATA_HI (pch->TxFrameData[4])
#define MBM_TX_FRAME_DIAG_FNCT_DATA_LO (pch->TxFrameData[5])
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC01_EN == DEF_ENABLED) || \
(MODBUS_CFG_FC02_EN == DEF_ENABLED)
static CPU_INT16U MBM_Coil_DI_Rd_Resp(MODBUS_CH *pch,
CPU_INT08U *ptbl);
#endif
#if (MODBUS_CFG_FC03_EN == DEF_ENABLED) || \
(MODBUS_CFG_FC04_EN == DEF_ENABLED)
static CPU_INT16U MBM_RegRd_Resp (MODBUS_CH *pch,
CPU_INT16U *ptbl);
#endif
#if (MODBUS_CFG_FC03_EN == DEF_ENABLED) && \
(MODBUS_CFG_FP_EN == DEF_ENABLED)
static CPU_INT16U MBM_RegRdFP_Resp (MODBUS_CH *pch,
CPU_FP32 *ptbl);
#endif
#if (MODBUS_CFG_FC05_EN == DEF_ENABLED)
static CPU_INT16U MBM_CoilWr_Resp (MODBUS_CH *pch);
#endif
#if (MODBUS_CFG_FC15_EN == DEF_ENABLED)
static CPU_INT16U MBM_CoilWrN_Resp (MODBUS_CH *pch);
#endif
#if (MODBUS_CFG_FC06_EN == DEF_ENABLED)
static CPU_INT16U MBM_RegWr_Resp (MODBUS_CH *pch);
#endif
#if (MODBUS_CFG_FC16_EN == DEF_ENABLED)
static CPU_INT16U MBM_RegWrN_Resp (MODBUS_CH *pch);
#endif
#if (MODBUS_CFG_FC08_EN == DEF_ENABLED)
static CPU_INT16U MBM_Diag_Resp (MODBUS_CH *pch,
CPU_INT16U *pval);
#endif
static CPU_BOOLEAN MBM_RxReply (MODBUS_CH *pch);
static void MBM_TxCmd (MODBUS_CH *pch);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* MBM_FC01_CoilRd()
*
* Description : Sends a MODBUS message to read the status of coils from a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus coil start address
*
* p_coil_tbl Is a pointer to an array of bytes containing the value of the coils read. The
* format is:
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* p_coil_tbl[0] #8 #7 #1
* p_coil_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array that will be receiving the coil values must be greater
* than or equal to: (nbr_coils - 1) / 8 + 1
*
* nbr_coils Is the desired number of coils to read
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC01_EN == DEF_ENABLED)
CPU_INT16U MBM_FC01_CoilRd (MODBUS_CH *pch,
CPU_INT08U slave_addr,
CPU_INT16U start_addr,
CPU_INT08U *p_coil_tbl,
CPU_INT16U nbr_coils)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_addr; /* Slave Address */
MBM_TX_FRAME_FC = 1; /* Function Code */
MBM_TX_FRAME_FC01_ADDR_HI = (CPU_INT08U)((start_addr >> 8) & 0x00FF); /* Staring Address */
MBM_TX_FRAME_FC01_ADDR_LO = (CPU_INT08U) (start_addr & 0x00FF);
MBM_TX_FRAME_FC01_NBR_POINTS_HI = (CPU_INT08U)((nbr_coils >> 8) & 0x00FF); /* Number of points */
MBM_TX_FRAME_FC01_NBR_POINTS_LO = (CPU_INT08U) (nbr_coils & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_Coil_DI_Rd_Resp(pch, /* Parse the response from the slave */
p_coil_tbl);
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC02_DIRd()
*
* Description : Sends a MODBUS message to read the status of discrete inputs from a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus discrete input start address
*
* p_di_tbl Is a pointer to an array that will receive the state of the desired discrete inputs.
* The format of the array is as follows:
*
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* p_di_tbl[0] #8 #7 #1
* p_di_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array that will be receiving the discrete input values must be greater
* than or equal to: (nbr_di - 1) / 8 + 1
*
* nbr_di Is the desired number of discrete inputs to read
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC02_EN == DEF_ENABLED)
CPU_INT16U MBM_FC02_DIRd (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_INT08U *p_di_tbl,
CPU_INT16U nbr_di)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 2;
MBM_TX_FRAME_FC02_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC02_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC02_NBR_POINTS_HI = (CPU_INT08U)((nbr_di >> 8) & 0x00FF);
MBM_TX_FRAME_FC02_NBR_POINTS_LO = (CPU_INT08U) (nbr_di & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_Coil_DI_Rd_Resp(pch, /* Parse the response from the slave */
p_di_tbl);
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC03_HoldingRegRd()
*
* Description : Sends a MODBUS message to read the value of holding registers from a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus holding register start address
*
* p_reg_tbl Is a pointer to an array of integers that will receive the current value of
* the desired holding registers from the slave. The array pointed to by
* 'p_reg_tbl' needs to be able to hold at least 'nbr_regs' entries.
*
* nbr_regs Is the desired number of holding registers to read
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC03_EN == DEF_ENABLED)
CPU_INT16U MBM_FC03_HoldingRegRd (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_INT16U *p_reg_tbl,
CPU_INT16U nbr_regs)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 3;
MBM_TX_FRAME_FC03_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC03_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC03_NBR_POINTS_HI = (CPU_INT08U)((nbr_regs >> 8) & 0x00FF);
MBM_TX_FRAME_FC03_NBR_POINTS_LO = (CPU_INT08U) (nbr_regs & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_RegRd_Resp(pch, /* Parse the response from the slave */
p_reg_tbl);
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC03_HoldingRegRdFP()
*
* Description : Sends a MODBUS message to read the value of floating-point holding registers from a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus floating-point holding register start address
*
* p_reg_tbl Is a pointer to an array of floating-points that will receive the current
* value of the desired holding registers from the slave. The array pointed to
* by 'p_reg_tbl' needs to be able to hold at least 'nbr_regs' entries.
*
* nbr_regs Is the desired number of holding registers to read
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC03_EN == DEF_ENABLED) && \
(MODBUS_CFG_FP_EN == DEF_ENABLED)
CPU_INT16U MBM_FC03_HoldingRegRdFP (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_FP32 *p_reg_tbl,
CPU_INT16U nbr_regs)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 3;
MBM_TX_FRAME_FC03_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC03_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC03_NBR_POINTS_HI = (CPU_INT08U)((nbr_regs >> 8) & 0x00FF);
MBM_TX_FRAME_FC03_NBR_POINTS_LO = (CPU_INT08U) (nbr_regs & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_RegRdFP_Resp(pch, /* Parse the response from the slave */
p_reg_tbl);
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC04_InRegRd()
*
* Description : Sends a MODBUS message to read the value of input registers from a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus input register start address
*
* p_reg_tbl Is a pointer to an array of integers that will receive the current value of
* the desired holding registers from the slave. The array pointed to by
* 'p_reg_tbl' needs to be able to hold at least 'nbr_regs' entries.
*
* nbr_regs Is the desired number of input registers to read
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC04_EN == DEF_ENABLED)
CPU_INT16U MBM_FC04_InRegRd (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_INT16U *p_reg_tbl,
CPU_INT16U nbr_regs)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 4;
MBM_TX_FRAME_FC04_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC04_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC04_NBR_POINTS_HI = (CPU_INT08U)((nbr_regs >> 8) & 0x00FF);
MBM_TX_FRAME_FC04_NBR_POINTS_LO = (CPU_INT08U) (nbr_regs & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_RegRd_Resp(pch, /* Parse the response from the slave */
p_reg_tbl);
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC05_CoilWr()
*
* Description : Sends a MODBUS message to write the value of single coil to a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus coil to change
*
* coil_val Is the desired value of the coil:
* MODBUS_COIL_ON to turn the coil on.
* MODBUS_COIL_OFF to turn the coil off.
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
* MODBUS_ERR_COIL_ADDR If you specified an invalid coil address
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC05_EN == DEF_ENABLED)
CPU_INT16U MBM_FC05_CoilWr (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_BOOLEAN coil_val)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 5;
MBM_TX_FRAME_FC05_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC05_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
if (coil_val == MODBUS_COIL_OFF) {
MBM_TX_FRAME_FC05_FORCE_DATA_HI = (CPU_INT08U)0x00;
MBM_TX_FRAME_FC05_FORCE_DATA_LO = (CPU_INT08U)0x00;
} else {
MBM_TX_FRAME_FC05_FORCE_DATA_HI = (CPU_INT08U)0xFF;
MBM_TX_FRAME_FC05_FORCE_DATA_LO = (CPU_INT08U)0x00;
}
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_CoilWr_Resp(pch); /* Parse the response from the slave */
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC06_HoldingRegWr()
*
* Description : Sends a MODBUS message to write the value of single holding register to a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus holding register address
*
* reg_val Is the desired 'integer' value of the holding register:
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_REG_ADDR If you specified an invalid register address
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC06_EN == DEF_ENABLED)
CPU_INT16U MBM_FC06_HoldingRegWr (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_INT16U reg_val)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 6;
MBM_TX_FRAME_FC06_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC06_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC06_DATA_HI = (CPU_INT08U)((reg_val >> 8) & 0x00FF);
MBM_TX_FRAME_FC06_DATA_LO = (CPU_INT08U) (reg_val & 0x00FF);
MBM_TxCmd(pch); /* Send command to the slave */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_RegWr_Resp(pch); /* Parse the response from the slave */
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC06_HoldingRegWrFP()
*
* Description : Sends a MODBUS message to write the value of single holding register to a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* slave_addr Is the Modbus holding register address
*
* reg_val_fp Is the desired value of the floating-point holding register:
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_REG_ADDR If you specified an invalid register address
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC06_EN == DEF_ENABLED) && \
(MODBUS_CFG_FP_EN == DEF_ENABLED)
CPU_INT16U MBM_FC06_HoldingRegWrFP (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_FP32 reg_val_fp)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
CPU_INT08U i;
CPU_INT08U *p_fp;
CPU_INT08U *p_data;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 6;
MBM_TX_FRAME_FC06_ADDR_HI = (CPU_INT08U)((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC06_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
p_fp = (CPU_INT08U *)®_val_fp; /* Point to the FP value */
p_data = MBM_TX_FRAME_FC06_DATA_ADDR;
#if CPU_CFG_ENDIAN_TYPE == CPU_ENDIAN_TYPE_BIG
for (i = 0; i < sizeof(CPU_FP32); i++) { /* Copy value to transmit buffer */
*p_data++ = *p_fp++;
}
#else
p_fp += sizeof(CPU_FP32) - 1;
for (i = 0; i < sizeof(CPU_FP32); i++) {
*p_data++ = *p_fp--;
}
#endif
MBM_TxCmd(pch); /* Send command */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == DEF_TRUE) {
err = MBM_RegWr_Resp(pch); /* Parse the response from the slave */
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC08_Diag()
*
* Description : Sends a MODBUS message to perform a diagnostic function of a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to obtain the information from.
*
* fnct Is a sub-function code
*
* fnct_data Is the data for a sub-function (if needed)
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SUB_FNCT If you specified an invalid sub-function code
* MODBUS_ERR_DIAG If there was an error in the command
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC08_EN == DEF_ENABLED)
CPU_INT16U MBM_FC08_Diag (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U fnct,
CPU_INT16U fnct_data,
CPU_INT16U *pval)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 8;
MBM_TX_FRAME_FC08_FNCT_HI = (CPU_INT08U)((fnct >> 8) & 0x00FF);
MBM_TX_FRAME_FC08_FNCT_LO = (CPU_INT08U) (fnct & 0x00FF);
MBM_TX_FRAME_FC08_FNCT_DATA_HI = (CPU_INT08U)((fnct_data >> 8) & 0x00FF);
MBM_TX_FRAME_FC08_FNCT_DATA_LO = (CPU_INT08U) (fnct_data & 0x00FF);
MBM_TxCmd(pch); /* Send command */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_Diag_Resp(pch, /* Parse the response from the slave */
pval);
} else {
err = MODBUS_ERR_RX;
}
} else {
*pval = 0;
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC15_CoilWr()
*
* Description : Sends a MODBUS message to write to coils on a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to write to.
*
* slave_addr Is the Modbus coil start address
*
* p_coil_tbl Is a pointer to an array of bytes containing the value of the coils to write.
* The format is:
*
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* p_coil_tbl[0] #8 #7 #1
* p_coil_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array containing the coil values must be greater than or equal
* to: nbr_coils / 8 + 1
*
* nbr_coils Is the desired number of coils to write
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_BYTE_COUNT If the expected number of bytes to receive doesn't correspond to the number of bytes received
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_FC15_EN == DEF_ENABLED)
CPU_INT16U MBM_FC15_CoilWr (MODBUS_CH *pch,
CPU_INT08U slave_node,
CPU_INT16U slave_addr,
CPU_INT08U *p_coil_tbl,
CPU_INT16U nbr_coils)
{
CPU_INT16U err;
CPU_BOOLEAN ok;
CPU_INT08U nbr_bytes;
CPU_INT08U i;
CPU_INT08U *p_data;
MBM_TX_FRAME_NBYTES = 4;
MBM_TX_FRAME_SLAVE_ADDR = slave_node; /* Setup command */
MBM_TX_FRAME_FC = 15;
MBM_TX_FRAME_FC15_ADDR_HI = (CPU_INT08U) ((slave_addr >> 8) & 0x00FF);
MBM_TX_FRAME_FC15_ADDR_LO = (CPU_INT08U) (slave_addr & 0x00FF);
MBM_TX_FRAME_FC15_NBR_POINTS_HI = (CPU_INT08U) ((nbr_coils >> 8) & 0x00FF);
MBM_TX_FRAME_FC15_NBR_POINTS_LO = (CPU_INT08U) (nbr_coils & 0x00FF);
nbr_bytes = (CPU_INT08U)(((nbr_coils - 1) / 8) + 1);
MBM_TX_FRAME_FC15_BYTE_CNT = nbr_bytes;
p_data = MBM_TX_FRAME_FC15_DATA;
for (i = 0; i < nbr_bytes; i++) {
*p_data++ = *p_coil_tbl++;
}
MBM_TxCmd(pch); /* Send command */
MB_OS_RxWait(pch, /* Wait for response from slave */
&err);
if (err == MODBUS_ERR_NONE) {
ok = MBM_RxReply(pch);
if (ok == MODBUS_TRUE) {
err = MBM_CoilWrN_Resp(pch); /* Parse the response from the slave */
} else {
err = MODBUS_ERR_RX;
}
}
pch->RxBufByteCtr = 0;
pch->RxBufPtr = &pch->RxBuf[0];
return (err);
}
#endif
/*
*********************************************************************************************************
* MBM_FC16_HoldingRegWrN()
*
* Description : Sends a MODBUS message to write to integer holding registers to a slave unit.
*
* Argument(s) : pch Is a pointer to the Modbus channel to send the request to.
*
* slave_node Is the Modbus node number of the desired slave to write to.
*
* slave_addr Is the Modbus holding register start address
*
* p_reg_tbl Is a pointer to an array of integers containing the value of the holding
* registers to write.
*
* Note that the array containing the register values must be greater than or equal
* to 'nbr_regs'
*
* nbr_regs Is the desired number of registers to write
*
* Return(s) : MODBUS_ERR_NONE If the function was sucessful.
* MODBUS_ERR_RX If a timeout occurred before receiving a response from the slave.
* MODBUS_ERR_SLAVE_ADDR If the transmitted slave address doesn't correspond to the received slave address
* MODBUS_ERR_FC If the transmitted function code doesn't correspond to the received function code
* MODBUS_ERR_REG_ADDR If you specified an invalid register address
* MODBUS_ERR_NBR_REG If you specified an invalid number of registers
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/