实例介绍
【实例简介】实例封装了modbus标准协议,通过socket可以与目前工控主流PLC直接通讯
与PLC通讯专用
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Windows.Forms; namespace CSharpModBusExample { public enum FunctionCode : byte { /// <summary> /// Read Multiple Registers /// </summary> Read = 3, /// <summary> /// Write Multiple Registers /// </summary> Write = 16 } internal class ModBusTCPIPWrapper : ModBusWrapper, IDisposable { private static short StartingAddress = short.Parse(ConfigurationManager.AppSettings["StartingAddress"]); public static ModBusTCPIPWrapper Instance = new ModBusTCPIPWrapper(); private SocketWrapper socketWrapper = new SocketWrapper(); bool connected = false; public override void Connect() { if (!connected) { this.socketWrapper.Logger = this.Logger; this.socketWrapper.Connect(); this.connected = true; } } public override byte[] Receive() { this.Connect(); List<byte> sendData = new List<byte>(255); //[1].Send sendData.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier) sendData.AddRange(new Byte[] { 0, 0 });//3~4:Protocol Identifier,0 = MODBUS protocol sendData.AddRange(ValueHelper.Instance.GetBytes((short)6));//5~6:后续的Byte数量(针对读请求,后续为6个byte) sendData.Add(0);//7:Unit Identifier:This field is used for intra-system routing purpose. sendData.Add((byte)FunctionCode.Read);//8.Function Code : 3 (Read Multiple Register) sendData.AddRange(ValueHelper.Instance.GetBytes(StartingAddress));//9~10.起始地址 sendData.AddRange(ValueHelper.Instance.GetBytes((short)30));//11~12.需要读取的寄存器数量 this.socketWrapper.Write(sendData.ToArray()); //发送读请求 //[2].防止连续读写引起前台UI线程阻塞 Application.DoEvents(); //[3].读取Response Header : 完后会返回8个byte的Response Header byte[] receiveData = this.socketWrapper.Read(256);//缓冲区中的数据总量不超过256byte,一次读256byte,防止残余数据影响下次读取 short identifier = (short)((((short)receiveData[0]) << 8) receiveData[1]); //[4].读取返回数据:根据ResponseHeader,读取后续的数据 if (identifier != this.CurrentDataIndex) //请求的数据标识与返回的标识不一致,则丢掉数据包 { return new Byte[0]; } byte length = receiveData[8];//最后一个字节,记录寄存器中数据的Byte数 byte[] result = new byte[length]; Array.Copy(receiveData, 9, result, 0, length); return result; } public override void Send(byte[] data) { //[0]:填充0,清掉剩余的寄存器 if (data.Length < 60) { var input = data; data = new Byte[60]; Array.Copy(input, data, input.Length); } this.Connect(); List<byte> values = new List<byte>(255); //[1].Write Header:MODBUS Application Protocol header values.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier) values.AddRange(new Byte[] { 0, 0 });//3~4:Protocol Identifier,0 = MODBUS protocol values.AddRange(ValueHelper.Instance.GetBytes((byte)(data.Length 7)));//5~6:后续的Byte数量 values.Add(0);//7:Unit Identifier:This field is used for intra-system routing purpose. values.Add((byte)FunctionCode.Write);//8.Function Code : 16 (Write Multiple Register) values.AddRange(ValueHelper.Instance.GetBytes(StartingAddress));//9~10.起始地址 values.AddRange(ValueHelper.Instance.GetBytes((short)(data.Length / 2)));//11~12.寄存器数量 values.Add((byte)data.Length);//13.数据的Byte数量 //[2].增加数据 values.AddRange(data);//14~End:需要发送的数据 //[3].写数据 this.socketWrapper.Write(values.ToArray()); //[4].防止连续读写引起前台UI线程阻塞 Application.DoEvents(); //[5].读取Response: 写完后会返回12个byte的结果 byte[] responseHeader = this.socketWrapper.Read(12); } #region IDisposable 成员 public override void Dispose() { socketWrapper.Dispose(); } #endregion } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论