-
Notifications
You must be signed in to change notification settings - Fork 21
/
modbus.c
1292 lines (1192 loc) · 40.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
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
#include "modbus.h"
#include <stdarg.h>
/** 配置ModBus实例 **/
/*** 参数 ***
** address: 设备地址
** frameType: 协议模式
** sendHandler: 发送数据的外部接口, 比如绑定到串口发送函数, 传入参数(byte* buff, size_t buffLen), 参数包括数据指针和数据长度
***/
void ModBus_setup( ModBus_parameter* ModBus_para, ModBus_Setting_T setting)
{
ModBus_para->m_address = setting.address;
ModBus_para->m_modeType = setting.frameType;
ModBus_para->m_receiveFrameBufferLen = 0;
ModBus_para->m_sendFramesN = 0;
ModBus_para->m_nextFrameIndex = 1; // 数据包序号从1开始
//ModBus_para->m_receiveBufferTmpLen = 0;
ModBus_para->m_pBeginReceiveBufferTmp = ModBus_para->m_receiveBufferTmp;
ModBus_para->m_pEndReceiveBufferTmp = ModBus_para->m_receiveBufferTmp;
ModBus_para->m_hasDetectedBufferStart = 0;
ModBus_para->m_registerCount = 0;
if (setting.register_access_limit > 0 && setting.register_access_limit <= MODBUS_REGISTER_LIMIT)
{
ModBus_para->m_registerAcessLimit = setting.register_access_limit;
}
else
{
ModBus_para->m_registerAcessLimit = MODBUS_REGISTER_LIMIT;
}
if (setting.baudRate == 0)
{
setting.baudRate = MODBUS_DEFAULT_BAUD;
}
ModBus_para->m_receiveTimeout = 4000u * 8u / setting.baudRate + 2u;
ModBus_para->m_sendTimeout = ((ModBus_para->m_registerAcessLimit * 4u + 20u) * 2000u + 7000u) * 8u / setting.baudRate + 5u;
ModBus_para->m_lastReceivedTime = ModBus_para->m_lastSentTime = millis();
ModBus_para->m_faston = 0; // 默认关闭快速模式, 保证初始化的时候指令能按顺序被执行
ModBus_para->m_SendHandler = setting.sendHandler;
#ifdef MODBUS_MASTER // 主机
#endif
#ifdef MODBUS_SLAVE // 从机
ModBus_para->m_GetRegisterHandler = NULL;
ModBus_para->m_SetRegisterHandler = NULL;
#endif
}
/** 设置数据收发速率 **/
/*** 参数 ***
** baud: 数据收发速率
** 注: 可以不设置, 使用默认超时时间( 符合波特率9600, 波特率大于9600可以不设置, 小于必须设置 )
***/
void ModBus_setBitRate(ModBus_parameter* ModBus_para, u32 baud)
{
if (baud > 0)
{
ModBus_para->m_receiveTimeout = 4000u * 8u / baud + 2u;
ModBus_para->m_sendTimeout = ((ModBus_para->m_registerAcessLimit * 4u + 20u) * 2000u + 7000u) * 8u / baud + 15u;
}
}
/** 设置接收超时时间 **/
/*** 参数 ***
** receiveTimeout: 接收时等待下一字节超时时间, 根据串口速率确定
** sendTimeout: 发送后等待返回帧超时时间, 根据串口速率确定
** 注: 可以不设置, 使用默认超时时间
***/
void ModBus_setTimeout(ModBus_parameter* ModBus_para, u32 receiveTimeout, u32 sendTimeout)
{
if (receiveTimeout > 0)
ModBus_para->m_receiveTimeout = receiveTimeout;
if (sendTimeout > 0)
ModBus_para->m_sendTimeout = sendTimeout;
}
// RTU模式时, 产生CRC校验码并添加到数据尾部
// Calculate CRC for outcoming buffer
// and place it to end.
// return total length
static size_t GenCRC16(byte* buff, size_t len)
{
uint16_t crc = 0xFFFF;
uint16_t pos = 0;
uint8_t i = 0;
uint8_t lo = 0;
uint8_t hi = 0;
for (pos = 0; pos < len; pos++)
{
crc ^= buff[pos];
for (i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
lo = crc & 0xFF;
hi = (crc >> 8) & 0xFF;
buff[len++] = lo;
buff[len++] = hi;
return len;
}
// RTU模式时, 计算CRC校验码, 给定初值
// Calculate CRC fro incoming buffer
// Return 1 - if CRC is correct, overwise return 0
static byte CheckCRC16(byte* buff, size_t len)
{
uint16_t crc = 0xFFFF;
uint16_t pos = 0;
uint8_t i = 0;
uint8_t lo = 0;
uint8_t hi = 0;
for (pos = 0; pos < len - 2; pos++)
{
crc ^= buff[pos];
for (i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
lo = crc & 0xFF;
hi = (crc >> 8) & 0xFF;
if ((buff[len - 2] == lo) &&
(buff[len - 1] == hi))
{
return 1;
}
#ifdef _UNIT_TEST
printf("CRC Check ERROR\n");
#endif // _UNIT_TEST
return 0;
}
// ASCII模式时, 产生LRC校验码并添加到数据尾部
static size_t GenLRC(byte* buff, size_t len)
{
size_t i = len;
uint8_t uchLRC = 0; /* LRC 字节初始化 */
while (i--)
uchLRC += *buff++; /* 累加*/
uchLRC = ((uint8_t)(-((char)uchLRC)));
*buff = uchLRC;
return len + 1;
}
// ASCII模式时, 校验LRC校验码,
static uint8_t CheckLRC(byte* buff, size_t len)
{
uint8_t uchLRC = 0; /* LRC 字节初始化 */
uint8_t LRC1 = buff[--len];
while (len--)
uchLRC += *buff++; /* 累加*/
uchLRC = ((uint8_t)(-((char)uchLRC)));
if (LRC1 == uchLRC)
return 1;
#ifdef _UNIT_TEST
printf("LRC Check ERROR\n");
#endif // _UNIT_TEST
return 0;
}
// ASCII模式时, 接收到的字符串转换为字节二进制
static size_t char2bin(byte* buff, size_t len)
{
size_t binInd = 0;
for (size_t i = 0; i < len; i++)
{
byte bin = 0, chr = buff[i];
if ((chr >= '0') && (chr <= '9'))
{
bin = (byte)(chr - '0');
}
else if ((chr >= 'A') && (chr <= 'F'))
{
bin = (byte)(chr - 'A' + 0x0A);
}
if (i % 2 == 0)
{
buff[binInd] = bin;
}
else
{
buff[binInd] = (buff[binInd] << 4) + bin;
binInd++;
}
}
return binInd;
}
// ASCII模式时, 字节二进制转换为字符串, 用于发送
static size_t bin2char_s(byte* buff, size_t len, size_t maxLen)
{
size_t binLen = len * 2;
size_t ret = binLen;
if (ret > maxLen)
{
return 0;
}
while (len--)
{
for (int i = 0; i < 2; i++)
{
byte bin = (buff[len] >> 4 * i) & 0x0F;
char chr = 0;
if ((bin >= 0) && (bin <= 9))
{
chr = (char)(bin + '0');
}
else if ((bin >= 0x0A) && (bin <= 0x0F))
{
chr = (char)(bin - 0x0A + 'A');
}
buff[--binLen] = chr;
}
}
return ret;
}
static MODBUS_FRAME_T* addFrame(ModBus_parameter* ModBus_para)
{
MODBUS_FRAME_T* pFrame;
if (ModBus_para->m_sendFramesN >= MODBUS_WAITFRAME_N)
{
memcpy(ModBus_para->m_sendFrames, ModBus_para->m_sendFrames + 1, (ModBus_para->m_sendFramesN - 1) * sizeof(MODBUS_FRAME_T));
pFrame = ModBus_para->m_sendFrames + (ModBus_para->m_sendFramesN - 1);
}
else
{
pFrame = ModBus_para->m_sendFrames + (ModBus_para->m_sendFramesN++);
}
pFrame->index = ModBus_para->m_nextFrameIndex++;
if (ModBus_para->m_nextFrameIndex == 0) // 指令序号不为0
{
ModBus_para->m_nextFrameIndex = 1;
}
pFrame->size = 0;
pFrame->responseHandler = NULL;
pFrame->time = millis();
MODBUS_DELAY_DEBUG(("Frame Len %d\n", ModBus_para->m_sendFramesN));
return pFrame;
}
// 接收字节数据到ModBus协议, 一般在中断函数中调用(如串口接收中断)
void ModBus_readByteFromOuter(ModBus_parameter* ModBus_para, byte receivedByte)
{
#ifdef _UNIT_TEST
printf("address %02x read byte: %02x\n", ModBus_para->m_address, receivedByte);
#endif // _UNIT_TEST
/*** 此函数 内部 不可更改 ModBus_para->m_pBeginReceiveBufferTmp 值!!!!!!!!!!!!!
**** 此函数 外部 不可更改 ModBus_para->m_pEndReceiveBufferTmp 值!!!!!!!!!!!!!!!
**** 避免内存写冲突, 保证数据完整性***/
*ModBus_para->m_pEndReceiveBufferTmp = receivedByte;
if (ModBus_para->m_pEndReceiveBufferTmp >= ModBus_para->m_receiveBufferTmp + MODBUS_BUFFER_SIZE - 1)
{
ModBus_para->m_pEndReceiveBufferTmp = ModBus_para->m_receiveBufferTmp;
}
else
{
ModBus_para->m_pEndReceiveBufferTmp++;
}
if (ModBus_para->m_pEndReceiveBufferTmp == ModBus_para->m_pBeginReceiveBufferTmp)
{
ModBus_para->m_pEndReceiveBufferTmp--;
if (ModBus_para->m_pEndReceiveBufferTmp < ModBus_para->m_receiveBufferTmp)
{
ModBus_para->m_pEndReceiveBufferTmp = ModBus_para->m_receiveBufferTmp + (MODBUS_BUFFER_SIZE - 1);
}
}
ModBus_para->m_lastReceivedTime = millis();
}
void ModBus_fastMode(ModBus_parameter* ModBus_para, byte faston)
{
ModBus_para->m_faston = faston;
}
// 检查接收数据包, 存在有效数据返回1, 否则返回0
static byte ModBus_detectFrame(ModBus_parameter* ModBus_para, size_t* restSize)
{
size_t i = 0, j = 0;
byte* pEnd, *pBegin;
size_t lenBufferTmp;
u8 frameSize = 0;
#ifdef MODBUS_MASTER
if (ModBus_para->m_sendFramesN > 0)
{
frameSize = ModBus_para->m_sendFrames[0].responseSize;
}
#endif
pEnd = ModBus_para->m_pEndReceiveBufferTmp;
pBegin = ModBus_para->m_pBeginReceiveBufferTmp; // volatile变量必须赋值给非volatile变量再操作, 否则有一定几率出现数据不完整
lenBufferTmp = pEnd - pBegin;
if (pEnd < pBegin)
{
lenBufferTmp = (size_t)MODBUS_BUFFER_SIZE - (pBegin - pEnd);
}
*restSize = 0;
switch (ModBus_para->m_modeType)
{
case ASCII:
{
if (lenBufferTmp == 0)
{
return 0;
}
if (!ModBus_para->m_hasDetectedBufferStart)
{// 检测起始字符
for (i = 0; i < lenBufferTmp; i++, pBegin++)
{
if (pBegin >= ModBus_para->m_receiveBufferTmp + MODBUS_BUFFER_SIZE)
{
pBegin = ModBus_para->m_receiveBufferTmp;
}
if (*pBegin == ':') // 检测到起始字符
{
ModBus_para->m_hasDetectedBufferStart = 1;
i++;
pBegin++;
if (pBegin >= ModBus_para->m_receiveBufferTmp + MODBUS_BUFFER_SIZE)
{
pBegin = ModBus_para->m_receiveBufferTmp;
}
break;
}
}
}
if (ModBus_para->m_hasDetectedBufferStart)
{// 检测结束字符
for (j = i; j < lenBufferTmp; j++, pBegin++)
{
if (pBegin >= ModBus_para->m_receiveBufferTmp + MODBUS_BUFFER_SIZE)
{
pBegin = ModBus_para->m_receiveBufferTmp;
}
if (*pBegin == '\r') // 检测到结束字符
{
break;
}
ModBus_para->m_receiveFrameBuffer[ModBus_para->m_receiveFrameBufferLen++] = *pBegin;
}
}
else // 没有检测到起始字符, 则接收数据异常
{
ModBus_para->m_pBeginReceiveBufferTmp = pEnd; // 抛弃pBegin到pEnd之间的数据, 保留pEnd之后新加的数据, 因为此上面处理过程中, 可能有新的数据被接收
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
if (j == lenBufferTmp) // 如果没有检测到结束字符, 返回继续接收
{
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
return 0;
}
else if (j + 1 == lenBufferTmp) // 如果回车结束字符是最后一个字符, 则保留回车字符返回继续接收
{
ModBus_para->m_pBeginReceiveBufferTmp = pBegin;;
return 0;
}
pBegin++;
if (pBegin >= ModBus_para->m_receiveBufferTmp + MODBUS_BUFFER_SIZE)
{
pBegin = ModBus_para->m_receiveBufferTmp;
}
if (*pBegin != '\n') // 如果下一个字符不是换行符, 则接收数据异常
{
ModBus_para->m_hasDetectedBufferStart = 0;
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
ModBus_para->m_receiveFrameBufferLen = char2bin(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBufferLen);
if (ModBus_para->m_receiveFrameBuffer[0] != ModBus_para->m_address)
{
ModBus_para->m_hasDetectedBufferStart = 0;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
if (!CheckLRC(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBufferLen)) // 如果校验不通过
{
ModBus_para->m_hasDetectedBufferStart = 0;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
//MODBUS_DEBUG(("ModBus rec: %*s\n", ModBus_para->m_receiveFrameBufferLen, ModBus_para->m_receiveFrameBuffer));
ModBus_para->m_receiveFrameBufferLen--; // 去除校验码
ModBus_para->m_hasDetectedBufferStart = 0;
break;
}
case RTU:
{
byte isTimeout = 0;
if (lenBufferTmp == 0) // 由于接收超时, 没有接收到数据
{
isTimeout = 1;
}
if (!ModBus_para->m_hasDetectedBufferStart)
{// 检测起始字节
for (i = 0; i < lenBufferTmp; i++, pBegin++)
{
if (*pBegin == ModBus_para->m_address) // 检测到地址
{
ModBus_para->m_hasDetectedBufferStart = 1;
ModBus_para->m_receiveFrameBuffer[ModBus_para->m_receiveFrameBufferLen++] = *pBegin;
i++;
pBegin++;
break;
}
}
}
if (ModBus_para->m_hasDetectedBufferStart)
{
// 拷贝所有临时缓冲区的数据到接收数据缓冲区
size_t newSize = lenBufferTmp - i;
if (ModBus_para->m_receiveFrameBufferLen + newSize > MODBUS_BUFFER_SIZE)
{
newSize = MODBUS_BUFFER_SIZE - ModBus_para->m_receiveFrameBufferLen;
}
if (pBegin <= pEnd)
{
memcpy(ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, pBegin, newSize);
}
else
{
memcpy(ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, pBegin, (size_t)MODBUS_BUFFER_SIZE - (pBegin - ModBus_para->m_receiveBufferTmp));
memcpy(ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, (void*)ModBus_para->m_receiveBufferTmp, pEnd - ModBus_para->m_receiveBufferTmp);
}
ModBus_para->m_receiveFrameBufferLen += newSize;
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
}
else // 没有检测到起始字符, 则接收数据异常
{
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
if (!(isTimeout // 接收超时
|| frameSize > 0 && ModBus_para->m_receiveFrameBufferLen >= frameSize // 数据包足够
|| ModBus_para->m_receiveFrameBufferLen >= MODBUS_BUFFER_SIZE)) // 缓冲区满
{
// 接收未结束, 返回继续接收数据
return 0;
}
if (ModBus_para->m_receiveFrameBufferLen < 2) // 接收超时且数据不足为异常
{
ModBus_para->m_pBeginReceiveBufferTmp = pEnd;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
if (!CheckCRC16(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBufferLen)) // 如果校验不通过
{
if (frameSize > 0 && frameSize < ModBus_para->m_receiveFrameBufferLen) // 如果数据长度比m_responseFrameLen长, 则尝试以m_responseFrameLen长度接收
{
if (!CheckCRC16(ModBus_para->m_receiveFrameBuffer, frameSize)) // 如果校验不通过, 不为超时或缓冲区满则返回继续接收
{
if (isTimeout || ModBus_para->m_receiveFrameBufferLen >= MODBUS_BUFFER_SIZE)
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
*restSize = ModBus_para->m_receiveFrameBufferLen - frameSize;// 待保留的数据长度
ModBus_para->m_receiveFrameBufferLen = frameSize;
}
else
{
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
}
ModBus_para->m_receiveFrameBufferLen--; // 去除校验码
ModBus_para->m_hasDetectedBufferStart = 0;
break;
}
default:
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
break;
}
return 1;
}
#ifdef MODBUS_MASTER
/** 读取寄存器 **/
/*** 参数 ***
** address: 寄存器首地址
** count: 读取寄存器个数
** GetReponseHandler: 读取结果回调函数, 传入参数(uint16_t* buff, uint16_t buffLen)
** 返回指令序号(大于0), 以便在回调函数中判断完成的是哪一指令, 不能发送返回0
***/
byte ModBus_getRegister(ModBus_parameter* ModBus_para, uint16_t address, uint16_t count, void(*GetReponseHandler)(uint16_t*, uint16_t))
{
MODBUS_FRAME_T* pFrame = addFrame(ModBus_para);
pFrame->type = READ_REGISTER;
pFrame->responseSize = 0;
pFrame->responseHandler = GetReponseHandler;
pFrame->address = address;
pFrame->count = count;
if (ModBus_para->m_modeType == ASCII)
{
pFrame->data[pFrame->size++] = ':';
}
pFrame->data[pFrame->size++] = ModBus_para->m_address; // 设备地址
pFrame->data[pFrame->size++] = READ_REGISTER; // 功能码, 读寄存器
pFrame->data[pFrame->size++] = (address >> 8) & 0x0FF; // 寄存器首地址高位
pFrame->data[pFrame->size++] = address & 0x0FF; // 寄存器首地址低位
pFrame->data[pFrame->size++] = (count >> 8) & 0x0FF; // 读寄存器个数高位
pFrame->data[pFrame->size++] = count & 0x0FF; // 读寄存器个数低位
switch (ModBus_para->m_modeType)
{
case ASCII:
pFrame->size = GenLRC(pFrame->data + 1, pFrame->size - 1) + 1; // 不包括起始字符
pFrame->size = bin2char_s(pFrame->data + 1, pFrame->size - 1, MODBUS_BUFFER_SIZE) + 1;
pFrame->data[pFrame->size++] = '\r'; // 结束字符
pFrame->data[pFrame->size++] = '\n'; // 结束字符
pFrame->responseSize = 11 + 4 * count; // 返回帧需要的字节数
break;
case RTU:
pFrame->size = GenCRC16(pFrame->data, pFrame->size);
pFrame->responseSize = 5 + 2 * count; // 返回帧需要的字节数
break;
default:
break;
}
return pFrame->index;
}
/** 写单个寄存器 **/
/*** 参数 ***
** address: 寄存器首地址
** data: 待写入数据
** SetReponseHandler: 写入结果回调函数, 传入参数(uint16_t address, uint16_t count), 参数包括首地址和寄存器个数
** 返回指令序号, 以便在回调函数中判断完成的是哪一指令
***/
byte ModBus_setRegister(ModBus_parameter* ModBus_para, uint16_t address, uint16_t data, void(*SetReponseHandler)(uint16_t, uint16_t))
{
MODBUS_FRAME_T* pFrame = addFrame(ModBus_para);
pFrame->type = WRITE_SINGLE_REGISTER;
pFrame->responseSize = 0;
pFrame->responseHandler = SetReponseHandler;
pFrame->address = address;
pFrame->count = 1;
if (ModBus_para->m_modeType == ASCII)
{
pFrame->data[pFrame->size++] = ':';
}
pFrame->data[pFrame->size++] = ModBus_para->m_address; // 设备地址
pFrame->data[pFrame->size++] = WRITE_SINGLE_REGISTER; // 功能码, 写入单寄存器
pFrame->data[pFrame->size++] = (address >> 8) & 0x0FF; // 寄存器首地址高位
pFrame->data[pFrame->size++] = address & 0x0FF; // 寄存器首地址低位
pFrame->data[pFrame->size++] = (data >> 8) & 0x0FF; // 数据高位
pFrame->data[pFrame->size++] = data & 0x0FF; // 数据低位
switch (ModBus_para->m_modeType)
{
case ASCII:
pFrame->size = GenLRC(pFrame->data + 1, pFrame->size - 1) + 1; // 不包括起始字符
pFrame->size = bin2char_s(pFrame->data + 1, pFrame->size - 1, MODBUS_BUFFER_SIZE) + 1;
pFrame->data[pFrame->size++] = '\r'; // 结束字符
pFrame->data[pFrame->size++] = '\n'; // 结束字符
pFrame->responseSize = 17; // 返回帧需要的字节数
break;
case RTU:
pFrame->size = GenCRC16(pFrame->data, pFrame->size);
pFrame->responseSize = 8; // 返回帧需要的字节数
break;
default:
break;
}
return pFrame->index;
}
/** 写多个寄存器 **/
/*** 参数 ***
** address: 寄存器首地址
** data: 待写入数据
** count: 待写入寄存器个数
** SetReponseHandler: 写入结果回调函数, 传入参数(uint16_t address, uint16_t count), 参数包括首地址和寄存器个数
** 返回0表示发送成功, 返回1表示忙未发送
***/
byte ModBus_setRegisters(ModBus_parameter* ModBus_para, uint16_t address, uint16_t* data, uint16_t count, void(*SetReponseHandler)(uint16_t, uint16_t))
{
MODBUS_FRAME_T* pFrame = addFrame(ModBus_para);
pFrame->type = WRITE_MULTI_REGISTER;
pFrame->responseSize = 0;
pFrame->responseHandler = SetReponseHandler;
pFrame->address = address;
pFrame->count = count;
if (ModBus_para->m_modeType == ASCII)
{
pFrame->data[pFrame->size++] = ':';
}
pFrame->data[pFrame->size++] = ModBus_para->m_address; // 设备地址
pFrame->data[pFrame->size++] = WRITE_MULTI_REGISTER; // 功能码, 写入多寄存器
pFrame->data[pFrame->size++] = (address >> 8) & 0x0FF; // 寄存器首地址高位
pFrame->data[pFrame->size++] = address & 0x0FF; // 寄存器首地址低位
pFrame->data[pFrame->size++] = (count >> 8) & 0x0FF; // 寄存器个数高位
pFrame->data[pFrame->size++] = count & 0x0FF; // 寄存器个数低位
switch (ModBus_para->m_modeType)
{
case ASCII:
pFrame->data[pFrame->size++] = count * 4; // 数据字节数
break;
case RTU:
pFrame->data[pFrame->size++] = count * 2; // 数据字节数
break;
default:
break;
}
if (count > ModBus_para->m_registerAcessLimit || pFrame->size + 2 * count + 2 > MODBUS_BUFFER_SIZE) // 如果超出最大数据量, 不发送, 立即调用回调函数
{
if (SetReponseHandler)
{
ModBus_para->m_sendFramesN--;
(*(SetReponseHandler))(address, 0);
}
return 0;
}
for (uint16_t i = 0; i < count; i++)
{
pFrame->data[pFrame->size++] = (data[i] >> 8) & 0x0FF; // 数据高位
pFrame->data[pFrame->size++] = data[i] & 0x0FF; // 数据低位
}
switch (ModBus_para->m_modeType)
{
case ASCII:
pFrame->size = GenLRC(pFrame->data + 1, pFrame->size - 1) + 1; // 不包括起始字符
pFrame->size = bin2char_s(pFrame->data + 1, pFrame->size - 1, MODBUS_BUFFER_SIZE) + 1;
pFrame->data[pFrame->size++] = '\r'; // 结束字符
pFrame->data[pFrame->size++] = '\n'; // 结束字符
pFrame->responseSize = 17; // 返回帧需要的字节数
break;
case RTU:
pFrame->size = GenCRC16(pFrame->data, pFrame->size);
pFrame->responseSize = 8; // 返回帧需要的字节数
break;
default:
break;
}
return pFrame->index;
}
// 接收数据结束, 处理数据, 存在有效数据返回1, 否则返回0
static byte ModBus_parseReveivedBuff(ModBus_parameter* ModBus_para)
{
size_t restSize;
MODBUS_FRAME_T* pFrame = NULL;
if (ModBus_para->m_sendFramesN > 0)
{
pFrame = ModBus_para->m_sendFrames;
}
else // 如果没有等待返回帧, 则不处理数据
{
ModBus_para->m_pBeginReceiveBufferTmp = ModBus_para->m_pEndReceiveBufferTmp;
ModBus_para->m_receiveFrameBufferLen = 0;
return 0;
}
if (!ModBus_detectFrame(ModBus_para, &restSize))
{
return 0;
}
MODBUS_DELAY_DEBUG(("Frame Delay %d\n", millis() - pFrame->time));
// 判断功能码
switch (ModBus_para->m_receiveFrameBuffer[1])
{
case READ_REGISTER:
{
u8 count = ModBus_para->m_receiveFrameBuffer[2];
MODBUS_DEBUG(("ModBus read reg response\n"));
if (count % 2 != 0 || pFrame->type != READ_REGISTER || count != pFrame->count * 2) // 数据异常
{
// 保留的未处理的数据
memcpy(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, restSize);
ModBus_para->m_receiveFrameBufferLen = restSize;
return 0;
}
count >>= 1; // 除2
if (count > ModBus_para->m_registerAcessLimit)
{
count = ModBus_para->m_registerAcessLimit;
}
for (size_t i = 0; i < count; i++)
{
ModBus_para->m_registerData[i] = (((uint16_t)ModBus_para->m_receiveFrameBuffer[3 + (i << 1)]) << 8) + ModBus_para->m_receiveFrameBuffer[4 + (i << 1)];
}
ModBus_para->m_registerCount = count;
// 回调函数
if (pFrame->responseHandler)
{
(*(GetReponseHandler_T)(pFrame->responseHandler))(ModBus_para->m_registerData, count);
}
break;
}
case WRITE_SINGLE_REGISTER:
{
uint16_t address = (ModBus_para->m_receiveFrameBuffer[2] << 8) + ModBus_para->m_receiveFrameBuffer[3];
uint16_t data = (ModBus_para->m_receiveFrameBuffer[4] << 8) + ModBus_para->m_receiveFrameBuffer[5];
uint16_t dataSent;
MODBUS_DEBUG(("ModBus write 0x%04x %d response\n", address, data));
if (ModBus_para->m_modeType == ASCII)
{
byte data[4];
memcpy(data, pFrame->data + 9, 4);
char2bin(data, 4);
dataSent = ((uint16_t)data[0] << 8) + data[1];
}
else if (ModBus_para->m_modeType == RTU)
{
dataSent = (pFrame->data[4] << 8) + pFrame->data[5];
}
if (pFrame->type != WRITE_SINGLE_REGISTER || address != pFrame->address || dataSent != data) // 数据异常
{
// 保留的未处理的数据
memcpy(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, restSize);
ModBus_para->m_receiveFrameBufferLen = restSize;
return 0;
}
// 回调函数
if (pFrame->responseHandler)
{
(*(SetReponseHandler_T)(pFrame->responseHandler))(address, 1);
}
break;
}
case WRITE_MULTI_REGISTER:
{
uint16_t address = (ModBus_para->m_receiveFrameBuffer[2] << 8) + ModBus_para->m_receiveFrameBuffer[3];
uint16_t count = (ModBus_para->m_receiveFrameBuffer[4] << 8) + ModBus_para->m_receiveFrameBuffer[5];
MODBUS_DEBUG(("ModBus write 0x%04x %d regs response\n", address, count));
if (pFrame->type != WRITE_MULTI_REGISTER || address != pFrame->address || count != pFrame->count) // 数据异常
{
// 保留的未处理的数据
memcpy(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, restSize);
ModBus_para->m_receiveFrameBufferLen = restSize;
return 0;
}
// 回调函数
if (pFrame->responseHandler)
{
(*(SetReponseHandler_T)(pFrame->responseHandler))(address, count);
}
break;
}
default:
memcpy(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, restSize);
ModBus_para->m_receiveFrameBufferLen = restSize;
return 0;
break;
}
memcpy(ModBus_para->m_receiveFrameBuffer, ModBus_para->m_receiveFrameBuffer + ModBus_para->m_receiveFrameBufferLen, restSize);
ModBus_para->m_receiveFrameBufferLen = restSize;
// 移除已返回指令
memcpy(ModBus_para->m_sendFrames, ModBus_para->m_sendFrames + 1, (--ModBus_para->m_sendFramesN) * sizeof(MODBUS_FRAME_T));
ModBus_para->m_waitingResponse = 0;
return 1;
}
static void sendFrame_loop(ModBus_parameter* ModBus_para)
{
u32 now = millis();
if (ModBus_para->m_waitingResponse && now - ModBus_para->m_lastSentTime < ModBus_para->m_sendTimeout || ModBus_para->m_sendFramesN == 0) // 等待返回帧未超时, 或没有待发送数据
{
return;
}
if (ModBus_para->m_waitingResponse && now - ModBus_para->m_lastSentTime >= ModBus_para->m_sendTimeout) // 等待返回帧超时
{
MODBUS_FRAME_T* pFrame = ModBus_para->m_sendFrames;
MODBUS_DELAY_DEBUG(("Frame Timeout %d\n", millis() - pFrame->time));
if (pFrame->responseHandler) // 调用回调, 传入参数(0,0)
{
switch (pFrame->type)
{
case READ_REGISTER:
(*(GetReponseHandler_T)(pFrame->responseHandler))(0, 0);
break;
case WRITE_SINGLE_REGISTER:
(*(SetReponseHandler_T)(pFrame->responseHandler))(0, 0);
break;
case WRITE_MULTI_REGISTER:
(*(SetReponseHandler_T)(pFrame->responseHandler))(0, 0);
break;
default:
break;
}
}
memcpy(ModBus_para->m_sendFrames, ModBus_para->m_sendFrames + 1, (--ModBus_para->m_sendFramesN) * sizeof(MODBUS_FRAME_T)); // 移除已发送数据包
ModBus_para->m_waitingResponse = 0;
}
if (!ModBus_para->m_waitingResponse && ModBus_para->m_sendFramesN > 0) // 不在等待返回帧且有待发送数据包, 则发送
{
MODBUS_FRAME_T* pFrame = ModBus_para->m_sendFrames;
if (ModBus_para->m_faston) // 如果是快速模式, 则只执行最新的指令
{
ModBus_para->m_sendFrames[0] = ModBus_para->m_sendFrames[ModBus_para->m_sendFramesN - 1];
ModBus_para->m_sendFramesN = 1;
}
if (ModBus_para->m_SendHandler != NULL)
{
(*ModBus_para->m_SendHandler)(pFrame->data, pFrame->size);
ModBus_para->m_waitingResponse = 1;
ModBus_para->m_lastSentTime = millis();
}
}
}
void ModBus_Master_loop(ModBus_parameter* ModBus_para)
{
u32 now = millis();
if (ModBus_para->m_pBeginReceiveBufferTmp != ModBus_para->m_pEndReceiveBufferTmp)
{
ModBus_parseReveivedBuff(ModBus_para); // 处理接收到的数据
}
if (now - ModBus_para->m_lastReceivedTime > ModBus_para->m_receiveTimeout) // 接收超时, 处理数据并重置
{
ModBus_parseReveivedBuff(ModBus_para); // 处理接收到的数据
ModBus_para->m_receiveFrameBufferLen = 0;
ModBus_para->m_lastReceivedTime = millis();
}
sendFrame_loop(ModBus_para);
}
#endif
#ifdef MODBUS_SLAVE
void ModBus_attachRegisterHandler(ModBus_parameter* ModBus_para, size_t(*GetRegisterHandler)(uint16_t, uint16_t, uint16_t*), size_t(*SetRegisterHandler)(uint16_t, uint16_t, uint16_t*))
{
ModBus_para->m_GetRegisterHandler = GetRegisterHandler;
ModBus_para->m_SetRegisterHandler = SetRegisterHandler;
}
/** 读取寄存器返回帧 **/
/*** 参数 ***
** address: 寄存器首地址
** count: 读取寄存器个数
** GetReponseHandler: 读取结果回调函数, 传入参数(uint16_t* buff, uint16_t buffLen)
***/
static void ModBus_getRegister_Slave(ModBus_parameter* ModBus_para, uint16_t address, uint8_t count)
{
ModBus_para->m_sendFrameBufferLen = 0;
if (ModBus_para->m_modeType == ASCII)
{
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ':';
}
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ModBus_para->m_address; // 设备地址
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = READ_REGISTER; // 功能码, 读寄存器
if (count > ModBus_para->m_registerAcessLimit || ModBus_para->m_sendFrameBufferLen + 2 * count + 3 > MODBUS_BUFFER_SIZE) // 如果超出最大数据量
{
count = 0;
}
count = (uint8_t)(*(ModBus_para->m_GetRegisterHandler))(address, count, ModBus_para->m_registerData);
ModBus_para->m_registerCount = count;
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = count * 2; // 字节数 = 读寄存器个数 * 2
for (uint16_t i = 0; i < count; i++)
{
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = (ModBus_para->m_registerData[i] >> 8) & 0x0FF; // 数据高位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ModBus_para->m_registerData[i] & 0x0FF; // 数据低位
}
switch (ModBus_para->m_modeType)
{
case ASCII:
ModBus_para->m_sendFrameBufferLen = GenLRC(ModBus_para->m_sendFrameBuffer + 1, ModBus_para->m_sendFrameBufferLen - 1) + 1; // 不包括起始字符
ModBus_para->m_sendFrameBufferLen = bin2char_s(ModBus_para->m_sendFrameBuffer + 1, ModBus_para->m_sendFrameBufferLen - 1, MODBUS_BUFFER_SIZE) + 1;
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = '\r'; // 结束字符
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = '\n'; // 结束字符
break;
case RTU:
ModBus_para->m_sendFrameBufferLen = GenCRC16(ModBus_para->m_sendFrameBuffer, ModBus_para->m_sendFrameBufferLen);
break;
default:
break;
}
if (ModBus_para->m_SendHandler != NULL && ModBus_para->m_sendFrameBufferLen > 0)
{
(*ModBus_para->m_SendHandler)(ModBus_para->m_sendFrameBuffer, ModBus_para->m_sendFrameBufferLen);
ModBus_para->m_lastSentTime = millis();
}
}
/** 写单个寄存器返回帧 **/
/*** 参数 ***
** address: 寄存器首地址
** data: 待写入数据
** SetReponseHandler: 写入结果回调函数, 传入参数(uint16_t address, uint16_t count), 参数包括首地址和寄存器个数
***/
static void ModBus_setRegister_Slave(ModBus_parameter* ModBus_para, uint16_t address, uint16_t data)
{
ModBus_para->m_sendFrameBufferLen = 0;
if (ModBus_para->m_modeType == ASCII)
{
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ':';
}
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ModBus_para->m_address; // 设备地址
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = WRITE_SINGLE_REGISTER; // 功能码, 读寄存器
if ((*(ModBus_para->m_SetRegisterHandler))(address, 1, &data) == 0) // 如果写入错误, 数据取反后返回, 以便主机判断
{
data = ~data;
}
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = (address >> 8) & 0x0FF; // 寄存器首地址高位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = address & 0x0FF; // 寄存器首地址低位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = (data >> 8) & 0x0FF; // 数据高位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = data & 0x0FF; // 数据低位
switch (ModBus_para->m_modeType)
{
case ASCII:
ModBus_para->m_sendFrameBufferLen = GenLRC(ModBus_para->m_sendFrameBuffer + 1, ModBus_para->m_sendFrameBufferLen - 1) + 1; // 不包括起始字符
ModBus_para->m_sendFrameBufferLen = bin2char_s(ModBus_para->m_sendFrameBuffer + 1, ModBus_para->m_sendFrameBufferLen - 1, MODBUS_BUFFER_SIZE) + 1;
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = '\r'; // 结束字符
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = '\n'; // 结束字符
break;
case RTU:
ModBus_para->m_sendFrameBufferLen = GenCRC16(ModBus_para->m_sendFrameBuffer, ModBus_para->m_sendFrameBufferLen);
break;
default:
break;
}
if (ModBus_para->m_SendHandler != NULL && ModBus_para->m_sendFrameBufferLen > 0)
{
(*ModBus_para->m_SendHandler)(ModBus_para->m_sendFrameBuffer, ModBus_para->m_sendFrameBufferLen);
ModBus_para->m_lastSentTime = millis();
}
}
/** 写多个寄存器返回帧 **/
/*** 参数 ***
** address: 寄存器首地址
** data: 待写入数据
** count: 待写入寄存器个数
** SetReponseHandler: 写入结果回调函数, 传入参数(uint16_t address, uint16_t count), 参数包括首地址和寄存器个数
***/
static void ModBus_setRegisters_Slave(ModBus_parameter* ModBus_para, uint16_t address, uint16_t* data, uint16_t count)
{
ModBus_para->m_sendFrameBufferLen = 0;
if (ModBus_para->m_modeType == ASCII)
{
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ':';
}
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = ModBus_para->m_address; // 设备地址
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = WRITE_MULTI_REGISTER; // 功能码, 读寄存器
count = (uint16_t)(*(ModBus_para->m_SetRegisterHandler))(address, count, data);
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = (address >> 8) & 0x0FF; // 首地址高位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = address & 0x0FF; // 首地址低位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = (count >> 8) & 0x0FF; // 个数高位
ModBus_para->m_sendFrameBuffer[ModBus_para->m_sendFrameBufferLen++] = count & 0x0FF; // 个数低位