From 4e5a67d79946b303690c633bfd658c545f5c42e2 Mon Sep 17 00:00:00 2001 From: "parallelbgls@outlook.com" Date: Fri, 10 Oct 2014 15:59:05 +0800 Subject: [PATCH] 2014-10-10 update 1 --- Modbus.Net/ModBus.Net/ModBus.Net.csproj | 1 + .../ModbusProtocalLinkerBytesExtend.cs | 56 +++++++++++++++++++ .../ModBus.Net/ProtocalLinkerBytesExtend.cs | 48 ---------------- 3 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 Modbus.Net/ModBus.Net/ModbusProtocalLinkerBytesExtend.cs diff --git a/Modbus.Net/ModBus.Net/ModBus.Net.csproj b/Modbus.Net/ModBus.Net/ModBus.Net.csproj index da83504..94c37aa 100644 --- a/Modbus.Net/ModBus.Net/ModBus.Net.csproj +++ b/Modbus.Net/ModBus.Net/ModBus.Net.csproj @@ -45,6 +45,7 @@ + diff --git a/Modbus.Net/ModBus.Net/ModbusProtocalLinkerBytesExtend.cs b/Modbus.Net/ModBus.Net/ModbusProtocalLinkerBytesExtend.cs new file mode 100644 index 0000000..da2ebd2 --- /dev/null +++ b/Modbus.Net/ModBus.Net/ModbusProtocalLinkerBytesExtend.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ModBus.Net +{ + /// + /// Tcp协议字节伸缩 + /// + public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + { + public override byte[] BytesExtend(byte[] content) + { + //Modbus/Tcp协议扩张,前面加6个字节,前面4个为0,后面两个为协议整体内容的长度 + byte[] newFormat = new byte[6 + content.Length]; + int tag = 0; + ushort leng = (ushort)content.Length; + Array.Copy(ValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4); + Array.Copy(ValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); + Array.Copy(content, 0, newFormat, 6, content.Length); + return newFormat; + } + + public override byte[] BytesDecact(byte[] content) + { + //Modbus/Tcp协议收缩,抛弃前面6个字节的内容 + byte[] newContent = new byte[content.Length - 6]; + Array.Copy(content, 6, newContent, 0, newContent.Length); + return newContent; + } + } + + public class ModbusRtuProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend + { + public override byte[] BytesExtend(byte[] content) + { + byte[] crc = new byte[2]; + //Modbus/Rtu协议扩张,增加CRC校验 + byte[] newFormat = new byte[content.Length + 2]; + Crc16.GetInstance().GetCRC(content, ref crc); + Array.Copy(content, 0, newFormat, 0, content.Length); + Array.Copy(crc, 0, newFormat, newFormat.Length - 2, crc.Length); + return newFormat; + } + + public override byte[] BytesDecact(byte[] content) + { + //Modbus/Rtu协议收缩,抛弃后面1个字节的内容 + byte[] newContent = new byte[content.Length - 2]; + Array.Copy(content, 0, newContent, 0, newContent.Length); + return newContent; + } + } +} diff --git a/Modbus.Net/ModBus.Net/ProtocalLinkerBytesExtend.cs b/Modbus.Net/ModBus.Net/ProtocalLinkerBytesExtend.cs index fda64f4..511e6e2 100644 --- a/Modbus.Net/ModBus.Net/ProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/ModBus.Net/ProtocalLinkerBytesExtend.cs @@ -25,52 +25,4 @@ namespace ModBus.Net /// 收缩后的协议内容 public abstract byte[] BytesDecact(byte[] content); } - - /// - /// Tcp协议字节伸缩 - /// - public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend - { - public override byte[] BytesExtend(byte[] content) - { - //Modbus/Tcp协议扩张,前面加6个字节,前面4个为0,后面两个为协议整体内容的长度 - byte[] newFormat = new byte[6 + content.Length]; - int tag = 0; - ushort leng = (ushort)content.Length; - Array.Copy(ValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4); - Array.Copy(ValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2); - Array.Copy(content, 0, newFormat, 6, content.Length); - return newFormat; - } - - public override byte[] BytesDecact(byte[] content) - { - //Modbus/Tcp协议收缩,抛弃前面6个字节的内容 - byte[] newContent = new byte[content.Length - 6]; - Array.Copy(content, 6, newContent, 0, newContent.Length); - return newContent; - } - } - - public class ModbusRtuProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend - { - public override byte[] BytesExtend(byte[] content) - { - byte[] crc = new byte[2]; - //Modbus/Rtu协议扩张,增加CRC校验 - byte[] newFormat = new byte[content.Length + 2]; - Crc16.GetInstance().GetCRC(content, ref crc); - Array.Copy(content, 0, newFormat, 0, content.Length); - Array.Copy(crc, 0, newFormat, newFormat.Length - 2, crc.Length); - return newFormat; - } - - public override byte[] BytesDecact(byte[] content) - { - //Modbus/Rtu协议收缩,抛弃后面1个字节的内容 - byte[] newContent = new byte[content.Length - 2]; - Array.Copy(content, 0, newContent, 0, newContent.Length); - return newContent; - } - } }