复制代码
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
789Modbus 是一个工业上常用的通讯协议、一种通讯约定。 ModBus 协议是应用层报文传输协议(OSI 模型第7层),它定义了一个与通信层无关的协议数据单元(PDU),即PDU=功能码+数据域。 ModBus 协议能够应用在不同类型的总线或网络。对应不同的总线或网络,Modbus 协议引入一些附加域映射成应用数据单元(ADU),即ADU=附加域+PDU。目前,Modbus 有下列三种通信方式: 1. 以太网,对应的通信模式是Modbus TCP。 2. 异步串行传输(各种介质如有线RS-232-/422/485/;光纤、无线等),对应的通信模式是 Modbus RTU 或 Modbus ASCII。 Modbus 的ASCII、RTU 协议规定了消息、数据的结构、命令和应答的方式,数据通讯采用Maser/Slave方式。 3. 高速令牌传递网络,对应的通信模式是Modbus PLUS。 Modbus 需要对数据进行校验,串行协议中除有奇偶校验外,ASCII 模式采用LRC 校验;RTU 模式采用16位CRC 校验;TCP 模式没有额外规定校验,因为TCP 是一个面向连接的可靠协议。 Modbus 协议的应用中,最常用的是Modbus RTU 传输模式。 RTU 传输模式 当设备使用RTU (Remote Terminal Unit) 模式在 Modbus 串行链路通信, 报文中每个8位字节含有两个4位十六进制字符。这种模式的主要优点是较高的数据密度,在相同的波特率下比ASCII 模式有更高的吞吐率。每个报文必须以连续的字符流传送。 RTU 模式每个字节 ( 11 位 ) 的格式为: 编码系统: 8位二进制。 报文中每个8位的字节含有两个4位十六进制字符(0–9, A–F) Bits per Byte: 1 起始位 8 数据位, 首先发送最低有效位 1 位作为奇偶校验 1 停止位 偶校验是要求的,其它模式 ( 奇校验, 无校验 ) 也可以使用。为了保证与其它产品的最大兼容性,同时支持无校验模式是建议的。默认校验模式模式 必须为偶校验。注:使用无校验要求2 个停止位。 字符的串行传送方式: 每个字符或字节均由此顺序发送(从左到右):最低有效位 (LSB) . . . 最高有效位 (MSB)图1:RTU 模式位序列 设备配置为奇校验、偶校验或无校验都可以接受。如果无奇偶校验,将传送一个附加的停止位以填充字符帧:
图2:RTU 模式位序列 (无校验的特殊情况) 帧检验域:循环冗余校验 (CRC) 在RTU 模式包含一个对全部报文内容执行的,基于循环冗余校验 (CRC - Cyclical Redundancy Checking) 算法的错误检验域。 CRC 域检验整个报文的内容。不管报文有无奇偶校验,均执行此检验。 CRC 包含由两个8位字节组成的一个16位值。 CRC 域作为报文的最后的域附加在报文之后。计算后,首先附加低字节,然后是高字节。CRC 高字节为报文发送的最后一个子节。 附加在报文后面的CRC 的值由发送设备计算。接收设备在接收报文时重新计算 CRC 的值,并将计算结果于实际接收到的CRC 值相比较。如果两个值不相等,则为错误。 CRC 的计算,开始对一个16位寄存器预装全1。 然后将报文中的连续的8位子节对其进行后续的计算。只有字符中的8个数据位参与生成CRC 的运算,起始位,停止位和校验位不参与 CRC 计算。 CRC 的生成过程中, 每个 8–位字符与寄存器中的值异或。然后结果向最低有效位(LSB)方向移动(Shift) 1位,而最高有效位(MSB)位置充零。 然后提取并检查 LSB:如果LSB 为1, 则寄存器中的值与一个固定的预置值异或;如果LSB 为 0, 则不进行异或操作。 这个过程将重复直到执行完8次移位。完成最后一次(第8次)移位及相关操作后,下一个8位字节与寄存器的当前值异或,然后又同上面描述过的一样重复8次。当所有报文中子节都运算之后得到的寄存器中的最终值,就是CRC。 当CRC 附加在报文之后时,首先附加低字节,然后是高字节。 CRC 算法如下: { //Perform a basic CRC check: byte[] CRC = new byte[2]; GetCRC(response, ref CRC); if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1]) return true; else return false; } private void GetCRC(byte[] message, ref byte[] CRC) { //Function expects a modbus message of any length as well as a 2 byte CRC array in which to //return the CRC values: ushort CRCFull = 0xFFFF; byte CRCHigh = 0xFF, CRCLow = 0xFF; char CRCLSB; for (int i = 0; i < (message.Length) - 2; i++) { CRCFull = (ushort)(CRCFull ^ message[i]); for (int j = 0; j < 8; j++) { CRCLSB = (char)(CRCFull & 0x0001); CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF); if (CRCLSB == 1) CRCFull = (ushort)(CRCFull ^ 0xA001); } } CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF); CRC[0] = CRCLow = (byte)(CRCFull & 0xFF); } 帧描述 (如下图所示) :
图3:RTU 报文帧 注意:Modbus RTU 帧最大为256字节。 下面是我为公司设计的一个 Modbus RTU 通信测试小工具,界面截图如下:
图4:Modbus RTU 通信工具 我的通用Modbus RTU 动态库,modbus.cs 如下:
modbus.cs using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.Threading; namespace SerialPort_Lib { public class modbus { private SerialPort sp = new SerialPort(); public string modbusStatus; #region Constructor / Deconstructor public modbus() { } ~modbus() { } #endregion #region Open / Close Procedures public bool Open(string portName, int baudRate, int databits, Parity parity, StopBits stopBits) { //Ensure port isn't already opened: if (!sp.IsOpen) { //Assign desired settings to the serial port: sp.PortName = portName; sp.BaudRate = baudRate; sp.DataBits = databits; sp.Parity = parity; sp.StopBits = stopBits; //These timeouts are default and cannot be editted through the class at this point: sp.ReadTimeout = -1; sp.WriteTimeout = 10000; try { sp.Open(); } catch (Exception err) { modbusStatus = "Error opening " + portName + ": " + err.Message; return false; } modbusStatus = portName + " opened successfully"; return true; } else { modbusStatus = portName + " already opened"; return false; } } public bool Close() { //Ensure port is opened before attempting to close: if (sp.IsOpen) { try { sp.Close(); } catch (Exception err) { modbusStatus = "Error closing " + sp.PortName + ": " + err.Message; return false; } modbusStatus = sp.PortName + " closed successfully"; return true; } else { modbusStatus = sp.PortName + " is not open"; return false; } } #endregion #region CRC Computation private void GetCRC(byte[] message, ref byte[] CRC) { //Function expects a modbus message of any length as well as a 2 byte CRC array in which to //return the CRC values: ushort CRCFull = 0xFFFF; byte CRCHigh = 0xFF, CRCLow = 0xFF; char CRCLSB; for (int i = 0; i < (message.Length) - 2; i++) { CRCFull = (ushort)(CRCFull ^ message[i]); for (int j = 0; j < 8; j++) { CRCLSB = (char)(CRCFull & 0x0001); CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF); if (CRCLSB == 1) CRCFull = (ushort)(CRCFull ^ 0xA001); } } CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF); CRC[0] = CRCLow = (byte)(CRCFull & 0xFF); } #endregion #region Build Message private void BuildMessage(byte address, byte type, ushort start, ushort registers, ref byte[] message) { //Array to receive CRC bytes: byte[] CRC = new byte[2]; message[0] = address; message[1] = type; message[2] = (byte)(start >> 8); message[3] = (byte)start; message[4] = (byte)(registers >> 8); message[5] = (byte)registers; GetCRC(message, ref CRC); message[message.Length - 2] = CRC[0]; message[message.Length - 1] = CRC[1]; } #endregion #region Check Response private bool CheckResponse(byte[] response) { //Perform a basic CRC check: byte[] CRC = new byte[2]; GetCRC(response, ref CRC); if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1]) return true; else return false; } #endregion #region Get Response private void GetResponse(ref byte[] response) { //There is a bug in .Net 2.0 DataReceived Event that prevents people from using this //event as an interrupt to handle data (it doesn't fire all of the time). Therefore //we have to use the ReadByte command for a fixed length as it's been shown to be reliable. for (int i = 0; i < response.Length; i++) { response[i] = (byte)(sp.ReadByte()); } } #endregion #region GetModbusData 获得接收数据 public bool GetModbusData(ref byte[] values) { //Ensure port is open: if (sp.IsOpen) { // 等待线程进入 //Monitor.Enter(sp); //Clear in/out buffers: //sp.DiscardOutBuffer(); //sp.DiscardInBuffer(); //Message is 1 addr + 1 type + N Data + 2 CRC try { //GetResponse(ref readBuffer); //string str = readBuffer.ToString(); int count = sp.BytesToRead; if (count > 0) { byte[] readBuffer = new byte[count]; GetResponse(ref readBuffer); // readData = new byte[29]; // Array.Copy(readBuffer, readData, readData.Length); // CRC 验证 if (CheckResponse(readBuffer)) { //显示输入数据 values = readBuffer; modbusStatus = "Write successful"; sp.DiscardInBuffer(); //values = System.Text.Encoding.ASCII.GetString(readData); return true; } else { modbusStatus = "CRC error"; sp.DiscardInBuffer(); return false; } } else return false; } catch (Exception err) { modbusStatus = "Error in write event: " + err.Message; sp.DiscardInBuffer(); return false; } //finally //{ // 通知其它对象 //Monitor.Pulse(sp); // 释放对象锁 //Monitor.Exit(sp); //} } else { modbusStatus = "Serial port not open"; return false; } } #endregion #region SendModbusData 打包发送数据 public bool SendModbusData(ref byte[] values) { //Ensure port is open: if (sp.IsOpen) { //Clear in/out buffers: sp.DiscardOutBuffer(); sp.DiscardInBuffer(); //Function 3 response buffer: byte[] response = new byte[values.Length + 2]; Array.Copy(values, response, values.Length); //BuildMessage(address, (byte)3, start, registers, ref message); //打包带有 CRC 验证的modbus 数据包: byte[] CRC = new byte[2]; GetCRC(response, ref CRC); response[response.Length - 2] = CRC[0]; response[response.Length - 1] = CRC[1]; values = response; //返回带有 CRC 验证的modbus 数据包 //Send modbus message to Serial Port: try { sp.Write(response, 0, response.Length); //GetResponse(ref response); return true; } catch (Exception err) { modbusStatus = "Error in read event: " + err.Message; return false; } //Evaluate message: //if (CheckResponse(response)) //{ // //Return requested register values: // for (int i = 0; i < (response.Length - 5) / 2; i++) // { // values[i] = response[2 * i + 3]; // values[i] <<= 8; // values[i] += response[2 * i + 4]; // } // modbusStatus = "Read successful"; // return true; //} //else //{ // modbusStatus = "CRC error"; // return false; //} } else { modbusStatus = "Serial port not open"; return false; } } #endregion } } 调用的主要代码如下:
modbus类的winform调用代码 { //业务处理类 B_ModbusData ModbusDataBLL = new B_ModbusData(); modbus mb = new modbus(); //SerialPort sp = new SerialPort(); System.Timers.Timer timer = new System.Timers.Timer(); public FormConfig() { InitializeComponent(); timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); } #region Timer Elapsed 事件处理程序 bool runEnd = true; void timer_Elapsed(object sender, ElapsedEventArgs e) { if (runEnd == true) { runEnd = false; PollFunction(); runEnd = true; } } //定时器调用方法 private void PollFunction() { byte[] values = null; try { mb.GetModbusData(ref values); //while (!mb.SendFc3(Convert.ToByte(txtSlaveID.Text), pollStart, pollLength, ref values)) ; } catch (Exception err) { DoGUIStatus("Error in modbus read: " + err.Message); } if (values != null) { //业务处理 byte[] sendData = ModbusDataProcess(values); } } #endregion #region IModbusData 接口成员处理 public byte[] ModbusDataProcess(byte[] _data) { byte[] sendData = ModbusDataBLL.ModbusDataProcess(_data); // CRC验证,并打包发送数据。 mb.SendModbusData(ref sendData); return sendData; } #endregion } 其实,三步就能成功调用: mb.GetModbusData(ref values); // 从串口设备获得数据。 byte[] sendData = ModbusDataBLL.ModbusDataProcess(values); // 你的业务处理,并产生最终返回数据。 mb.SendModbusData(ref sendData); // CRC验证,并打包发送数据。 主要代码已全部提供,由于工作原因暂不提供完整工具源代码,见谅!
复制代码
1
2private bool CheckResponse(byte[] response)
复制代码
1
2using System;
复制代码
1
2public partial class FormConfig : Form,IModbusData
复制代码
1
2modbus mb = new modbus();
最后
以上就是舒心往事最近收集整理的关于Modbus RTU 工业通讯技术实现的全部内容,更多相关Modbus内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复