C#實(shí)現(xiàn)Modbus通信功能的示例詳解
1、前言
Modbus作為一種開(kāi)放且廣泛采用的通信協(xié)議,在實(shí)現(xiàn)設(shè)備間數(shù)據(jù)交換方面發(fā)揮著至關(guān)重要的作用。它不僅支持多種物理層接口(如RS-232, RS-485, 以及以太網(wǎng)),還因其簡(jiǎn)單易用的特點(diǎn)而被大家所青睞。
本文通過(guò)實(shí)際示例介紹如何在C#項(xiàng)目中輕松實(shí)現(xiàn)Modbus通信功能。
2、開(kāi)源通信庫(kù)
通信庫(kù)是對(duì)通信協(xié)議的封裝,一般是以dll動(dòng)態(tài)鏈接庫(kù)的形式存在,對(duì)于編程者來(lái)說(shuō),只需要調(diào)用庫(kù)的各種方法即可實(shí)現(xiàn)數(shù)據(jù)讀寫(xiě)。
通信庫(kù)有兩種,一種是開(kāi)源的,即使開(kāi)源,也要注意看下開(kāi)源許可證,開(kāi)源并不一定免費(fèi),另外一種就是自己開(kāi)發(fā)封裝的,這個(gè)需要具備一定的開(kāi)發(fā)能力。
Modbus通信有很多開(kāi)源通信庫(kù),這其中使用較為廣泛的是NModbus4,NModbus4是一個(gè)開(kāi)源且免費(fèi)的Modbus通信庫(kù),它的開(kāi)源許可證是MIT,是一個(gè)相對(duì)寬松的軟件授權(quán)條款,可以商用。
3、ModbusRTU通信
1、在項(xiàng)目解決方案資源管理器中,選擇【引用】右擊,在彈出的界面中,點(diǎn)擊【管理NuGet程序包】選項(xiàng),如下圖所示:
2、在打開(kāi)的選項(xiàng)卡中,選擇【瀏覽】,然后輸入NModbus4進(jìn)行搜索,搜索到之后,選擇最新穩(wěn)定版2.1.0,點(diǎn)擊【安裝】即可:
3、在NModbus4基礎(chǔ)上封裝一個(gè)打開(kāi)串口和關(guān)閉串口的方法:
private SerialPort serialPort; private ModbusSerialMaster master; public void Open(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) { if (this.serialPort != null && this.serialPort.IsOpen) { this.serialPort.Close(); } this.serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); this.serialPort.Open(); this.master = ModbusSerialMaster.CreateRtu(this.serialPort); this.master.Transport.WriteTimeout = 2000; this.master.Transport.ReadTimeout = 2000; this.master.Transport.WaitToRetryMilliseconds = 500; this.master.Transport.Retries = 3; } public void Close() { if (this.serialPort != null && this.serialPort.IsOpen) { this.serialPort.Close(); } this.master = null; }
4、在NModbus4基礎(chǔ)上封裝各種讀寫(xiě)的方法,這里以讀取保持型寄存器為例,其他方法都是類(lèi)似的:
public byte[] ReadHoldingRegisters(byte slaveId, ushort start, ushort length) { try { ushort[] data = this.master.ReadHoldingRegisters(slaveId, start, length); List<byte> result = new List<byte>(); foreach (var item in data) { result.AddRange(BitConverter.GetBytes(item).Reverse()); } return result.ToArray(); } catch (Exception ex) { throw new Exception("【讀取保持寄存器】失?。? + ex.Message); } }
基于NModbus4實(shí)現(xiàn)ModbusRTU通信,不需要關(guān)注協(xié)議及報(bào)文,只需要對(duì)NModbus4庫(kù)二次封裝即可。
4、ModbusTCP通信
NModbus4不僅支持ModbusRTU通信,也同樣支持ModbusTCP通信,ModbusTCP與ModbusRTU的封裝過(guò)程非常類(lèi)似,主要是存在以下兩個(gè)不同點(diǎn):
ModbusRTU是基于串口通信,因此主要使用的是SerialPort類(lèi),而ModbusTCP是基于以太網(wǎng)通信,主要使用的是TcpClient類(lèi)。
ModbusRTU的讀取和寫(xiě)入方法中都必須包含從站地址,而ModbusTCP可以把SlaveAddress作為一個(gè)可選項(xiàng)。
ModbusTCP通信庫(kù)封裝過(guò)程如下:
1、在NModbus4基礎(chǔ)上封裝一個(gè)TCP連接和斷開(kāi)的方法:
private TcpClient tcpClient; private ModbusIpMaster master public void Connect(string ip, int port) { tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse(ip), port); this.master = ModbusIpMaster.CreateIp(this.tcpClient); this.master.Transport.WriteTimeout = 2000; this.master.Transport.ReadTimeout = 2000; this.master.Transport.WaitToRetryMilliseconds = 500; this.master.Transport.Retries = 3; } public void DisConnect() { if (this.tcpClient != null && this.tcpClient.Connected) { this.tcpClient.Close(); } this.master = null; }
2、封裝一個(gè)讀取輸出線圈的方法,其他讀寫(xiě)方法都是類(lèi)似的:
public bool[] ReadOutputCoils(ushort start, ushort length, byte slaveAddress = 1) { try { return this.master.ReadCoils(slaveAddress, start, length); } catch (Exception ex) { throw new Exception("【讀取輸出線圈】失?。? + ex.Message); } }
總結(jié)
通過(guò)對(duì)本篇文章的學(xué)習(xí),我們探索了如何使用C#來(lái)實(shí)現(xiàn)Modbus協(xié)議下的數(shù)據(jù)通信。
最后
到此這篇關(guān)于C#實(shí)現(xiàn)Modbus通信功能的示例詳解的文章就介紹到這了,更多相關(guān)C#實(shí)現(xiàn)Modbus通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# GridControl的模糊查詢實(shí)現(xiàn)代碼
這篇文章主要介紹了c# GridControl的模糊查詢實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02對(duì)WPF中的TreeView實(shí)現(xiàn)右鍵選定
這篇文章介紹了WPF實(shí)現(xiàn)右鍵選定TreeView的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06ListView用法中與滾動(dòng)相關(guān)的需求實(shí)現(xiàn)
這篇文章主要介紹了ListView用法中與滾動(dòng)相關(guān)的需求實(shí)現(xiàn),獲取并設(shè)置ListView的滾動(dòng)位置,以及獲取滾動(dòng)位置處的項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06C# PDF轉(zhuǎn)圖片(JPG,Png)的項(xiàng)目實(shí)踐
本文主要介紹了C# PDF轉(zhuǎn)圖片(JPG,Png)的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08