From 7efa2c68b48076f0714a0af7cb425c701500a8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 28 Nov 2024 13:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B0v11=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Modbus/Drivers/ModbusDriver.cs | 3 +- NewLife.Modbus/ModbusSession.cs | 12 +++--- NewLife.Modbus/NewLife.Modbus.csproj | 2 +- NewLife.Modbus/Protocols/Modbus.cs | 18 ++++----- .../Protocols/ModbusAsciiMessage.cs | 12 +++++- NewLife.Modbus/Protocols/ModbusIp.cs | 6 +-- NewLife.Modbus/Protocols/ModbusIpMessage.cs | 12 +++++- NewLife.Modbus/Protocols/ModbusMessage.cs | 37 +++++++++++++------ NewLife.Modbus/Protocols/ModbusRtuMessage.cs | 9 ++++- NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs | 6 +-- NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs | 6 +-- NewLife.Modbus/Protocols/ModbusTcp.cs | 2 +- NewLife.Modbus/Protocols/ModbusUdp.cs | 2 +- .../Controllers/DefaultSerialPort.cs | 18 +++++---- NewLife.ModbusRTU/Protocols/ModbusAscii.cs | 2 +- NewLife.ModbusRTU/Protocols/ModbusRtu.cs | 2 +- Test/Test.csproj | 2 +- WinModbus/FrmI6O6N.cs | 3 +- WinModbus/FrmSwitch.cs | 1 + WinModbus/WinModbus.csproj | 2 +- XUnitTest/ModbusAsciiMessageTests.cs | 1 + XUnitTest/ModbusDriverTests.cs | 17 +++++---- XUnitTest/ModbusIpMessageTests.cs | 1 + XUnitTest/ModbusMessageTests.cs | 1 + XUnitTest/ModbusRtuMessageTests.cs | 1 + XUnitTest/ModbusTests.cs | 16 ++++---- XUnitTest/XUnitTest.csproj | 12 +++--- 27 files changed, 129 insertions(+), 77 deletions(-) diff --git a/NewLife.Modbus/Drivers/ModbusDriver.cs b/NewLife.Modbus/Drivers/ModbusDriver.cs index 4639bfb..64f5751 100644 --- a/NewLife.Modbus/Drivers/ModbusDriver.cs +++ b/NewLife.Modbus/Drivers/ModbusDriver.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using NewLife.Data; using NewLife.IoT.Protocols; using NewLife.IoT.ThingModels; using NewLife.IoT.ThingSpecification; @@ -144,7 +145,7 @@ public override IDictionary Read(INode node, IPoint[] points) // 其中一项读取报错时,直接跳过,不要影响其它批次 try { - seg.Data = Modbus.Read(seg.ReadCode, n.Host, (UInt16)seg.Address, (UInt16)seg.Count)?.ReadBytes(); + seg.Data = Modbus.Read(seg.ReadCode, n.Host, (UInt16)seg.Address, (UInt16)seg.Count)?.ReadBytes(-1); //var x = seg.Data.Join(" ", e => e.ToHex()); } diff --git a/NewLife.Modbus/ModbusSession.cs b/NewLife.Modbus/ModbusSession.cs index ba10d02..cd587a7 100644 --- a/NewLife.Modbus/ModbusSession.cs +++ b/NewLife.Modbus/ModbusSession.cs @@ -52,7 +52,7 @@ protected override void OnReceive(ReceivedEventArgs e) base.OnReceive(e); } - Packet OnReadCoil(ModbusMessage msg) + IPacket OnReadCoil(ModbusMessage msg) { var coils = Host.Coils; if (coils == null) return null; @@ -82,13 +82,13 @@ Packet OnReadCoil(ModbusMessage msg) rs[1 + i] = (Byte)b; } - return rs; + return (ArrayPacket)rs; } return null; } - Packet OnReadRegister(ModbusMessage msg) + IPacket OnReadRegister(ModbusMessage msg) { var regs = Host.Registers; if (regs == null) return null; @@ -99,7 +99,7 @@ Packet OnReadRegister(ModbusMessage msg) if (addr >= 0 && addr + regCount <= regs.Count) { var buf = regs.Skip(addr).Take(regCount).SelectMany(e => e.GetData()).ToArray(); - var pk = new Packet(new Byte[] { (Byte)buf.Length }); + var pk = new ArrayPacket([(Byte)buf.Length]); pk.Append(buf); return pk; } @@ -107,7 +107,7 @@ Packet OnReadRegister(ModbusMessage msg) return null; } - Packet OnWriteRegister(ModbusMessage msg) + IPacket OnWriteRegister(ModbusMessage msg) { var regs = Host.Registers; if (regs == null) return null; @@ -130,7 +130,7 @@ Packet OnWriteRegister(ModbusMessage msg) { var addr = reqAddr - regs[0].Address; - return regs.Skip(addr).Take(regCount).SelectMany(e => e.GetData()).ToArray(); + return (ArrayPacket)regs.Skip(addr).Take(regCount).SelectMany(e => e.GetData()).ToArray(); } } } diff --git a/NewLife.Modbus/NewLife.Modbus.csproj b/NewLife.Modbus/NewLife.Modbus.csproj index 9f5ab7a..a3c7457 100644 --- a/NewLife.Modbus/NewLife.Modbus.csproj +++ b/NewLife.Modbus/NewLife.Modbus.csproj @@ -45,7 +45,7 @@ - + diff --git a/NewLife.Modbus/Protocols/Modbus.cs b/NewLife.Modbus/Protocols/Modbus.cs index 9048083..afdd71a 100644 --- a/NewLife.Modbus/Protocols/Modbus.cs +++ b/NewLife.Modbus/Protocols/Modbus.cs @@ -51,7 +51,7 @@ public virtual void Open() { } /// 地址。例如0x0002 /// 数据值 /// 返回响应消息的负载部分 - public virtual Packet SendCommand(FunctionCodes code, Byte host, UInt16 address, UInt16 value) + public virtual IPacket SendCommand(FunctionCodes code, Byte host, UInt16 address, UInt16 value) { var msg = CreateMessage(); msg.Host = host; @@ -69,7 +69,7 @@ public virtual Packet SendCommand(FunctionCodes code, Byte host, UInt16 address, /// 主机。一般是1 /// 数据 /// 返回响应消息的负载部分 - public virtual Packet SendCommand(FunctionCodes code, Byte host, Packet data) + public virtual IPacket SendCommand(FunctionCodes code, Byte host, IPacket data) { var msg = CreateMessage(); msg.Host = host; @@ -95,7 +95,7 @@ public virtual Packet SendCommand(FunctionCodes code, Byte host, Packet data) /// 个数。寄存器个数或线圈个数 /// /// - public virtual Packet Read(FunctionCodes code, Byte host, UInt16 address, UInt16 count) + public virtual IPacket Read(FunctionCodes code, Byte host, UInt16 address, UInt16 count) { switch (code) { @@ -115,7 +115,7 @@ public virtual Packet Read(FunctionCodes code, Byte host, UInt16 address, UInt16 /// 地址。例如0x0002 /// 线圈数量。一般要求8的倍数 /// 线圈状态字节数组 - public Packet ReadCoil(Byte host, UInt16 address, UInt16 count) + public IPacket ReadCoil(Byte host, UInt16 address, UInt16 count) { using var span = Tracer?.NewSpan("modbus:ReadCoil", $"host={host} address={address}/0x{address:X4} count={count}"); try @@ -144,7 +144,7 @@ public Packet ReadCoil(Byte host, UInt16 address, UInt16 count) /// 地址。例如0x0002 /// 输入数量。一般要求8的倍数 /// 输入状态字节数组 - public Packet ReadDiscrete(Byte host, UInt16 address, UInt16 count) + public IPacket ReadDiscrete(Byte host, UInt16 address, UInt16 count) { using var span = Tracer?.NewSpan("modbus:ReadDiscrete", $"host={host} address={address}/0x{address:X4} count={count}"); try @@ -173,7 +173,7 @@ public Packet ReadDiscrete(Byte host, UInt16 address, UInt16 count) /// 地址。例如0x0002 /// 寄存器数量。每个寄存器2个字节 /// 寄存器值数组 - public Packet ReadRegister(Byte host, UInt16 address, UInt16 count) + public IPacket ReadRegister(Byte host, UInt16 address, UInt16 count) { using var span = Tracer?.NewSpan("modbus:ReadRegister", $"host={host} address={address}/0x{address:X4} count={count}"); try @@ -202,7 +202,7 @@ public Packet ReadRegister(Byte host, UInt16 address, UInt16 count) /// 地址。例如0x0002 /// 输入寄存器数量。每个寄存器2个字节 /// 输入寄存器值数组 - public Packet ReadInput(Byte host, UInt16 address, UInt16 count) + public IPacket ReadInput(Byte host, UInt16 address, UInt16 count) { using var span = Tracer?.NewSpan("modbus:ReadInput", $"host={host} address={address}/0x{address:X4} count={count}"); try @@ -332,7 +332,7 @@ public Int32 WriteCoils(Byte host, UInt16 address, UInt16[] values) // 直接使用内存流缓冲区,避免拷贝 binary.Stream.Position = 0; - var pk = new Packet(binary.Stream); + var pk = new ArrayPacket(binary.Stream); var rs = SendCommand(FunctionCodes.WriteCoils, host, pk); if (rs == null || rs.Total < 4) return -1; @@ -370,7 +370,7 @@ public Int32 WriteRegisters(Byte host, UInt16 address, UInt16[] values) // 直接使用内存流缓冲区,避免拷贝 binary.Stream.Position = 0; - var pk = new Packet(binary.Stream); + var pk = new ArrayPacket(binary.Stream); var rs = SendCommand(FunctionCodes.WriteRegisters, host, pk); if (rs == null || rs.Total < 4) return -1; diff --git a/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs b/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs index 2dbf58f..af05373 100644 --- a/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs +++ b/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs @@ -45,12 +45,22 @@ public override Boolean Read(Stream stream, Object context) /// 数据包 /// 是否响应 /// - public static new ModbusAsciiMessage Read(Packet data, Boolean reply = false) + public static new ModbusAsciiMessage Read(IPacket data, Boolean reply = false) { var msg = new ModbusAsciiMessage { Reply = reply }; return msg.Read(data.GetStream(), null) ? msg : null; } + /// 解析消息 + /// + /// + /// + public static new ModbusAsciiMessage Read(Byte[] data, Boolean reply = false) + { + var msg = new ModbusAsciiMessage { Reply = reply }; + return msg.Read(new MemoryStream(data), null) ? msg : null; + } + /// 写入消息到数据流 /// 数据流 /// 上下文 diff --git a/NewLife.Modbus/Protocols/ModbusIp.cs b/NewLife.Modbus/Protocols/ModbusIp.cs index 6dc65bd..e32b932 100644 --- a/NewLife.Modbus/Protocols/ModbusIp.cs +++ b/NewLife.Modbus/Protocols/ModbusIp.cs @@ -81,15 +81,15 @@ public override void Open() /// 目标数据包 /// 是否匹配请求 /// 响应消息 - protected abstract ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match); + protected abstract ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match); /// 接收响应 /// - protected virtual Packet ReceiveCommand() + protected virtual IPacket ReceiveCommand() { // 设置协议最短长度,避免读取指令不完整。由于请求响应机制,不存在粘包返回。 var dataLength = 8; // 2+2+2+1+1 - Packet pk = null; + IPacket pk = null; for (var i = 0; i < 8; i++) { // 阻塞读取 diff --git a/NewLife.Modbus/Protocols/ModbusIpMessage.cs b/NewLife.Modbus/Protocols/ModbusIpMessage.cs index 9f6a1c2..e8cfc06 100644 --- a/NewLife.Modbus/Protocols/ModbusIpMessage.cs +++ b/NewLife.Modbus/Protocols/ModbusIpMessage.cs @@ -36,12 +36,22 @@ public override Boolean Read(Stream stream, Object context) /// 数据包 /// 是否响应 /// - public new static ModbusIpMessage Read(Packet data, Boolean reply = false) + public new static ModbusIpMessage Read(IPacket data, Boolean reply = false) { var msg = new ModbusIpMessage { Reply = reply }; return msg.Read(data.GetStream(), null) ? msg : null; } + /// 解析消息 + /// + /// + /// + public new static ModbusIpMessage Read(Byte[] data, Boolean reply = false) + { + var msg = new ModbusIpMessage { Reply = reply }; + return msg.Read(new MemoryStream(data), null) ? msg : null; + } + /// 写入消息到数据流 /// 数据流 /// 上下文 diff --git a/NewLife.Modbus/Protocols/ModbusMessage.cs b/NewLife.Modbus/Protocols/ModbusMessage.cs index d5cd222..7d09d26 100644 --- a/NewLife.Modbus/Protocols/ModbusMessage.cs +++ b/NewLife.Modbus/Protocols/ModbusMessage.cs @@ -26,7 +26,7 @@ public class ModbusMessage : IAccessor /// 负载数据 [IgnoreDataMember] - public Packet Payload { get; set; } + public IPacket Payload { get; set; } #endregion #region 构造 @@ -64,7 +64,7 @@ public virtual Boolean Read(Stream stream, Object context) return true; } - Payload = stream.ReadBytes(-1); + Payload = (ArrayPacket)stream.ReadBytes(-1); return true; } @@ -73,7 +73,7 @@ public virtual Boolean Read(Stream stream, Object context) /// 数据包 /// 是否响应 /// - public static ModbusMessage Read(Packet data, Boolean reply = false) + public static ModbusMessage Read(IPacket data, Boolean reply = false) { var msg = new ModbusMessage { Reply = reply }; if (msg.Read(data.GetStream(), null)) return msg; @@ -81,6 +81,16 @@ public static ModbusMessage Read(Packet data, Boolean reply = false) return null; } + /// 解析消息 + /// + /// + /// + public static ModbusMessage Read(Byte[] data, Boolean reply = false) + { + var msg = new ModbusMessage { Reply = reply }; + return msg.Read(new MemoryStream(data), null) ? msg : null; + } + /// 写入消息到数据流 /// 数据流 /// 上下文 @@ -102,22 +112,22 @@ public virtual Boolean Write(Stream stream, Object context) return true; } - var pk = Payload; - if (pk != null) binary.Write(pk.Data, pk.Offset, pk.Count); - //Payload?.CopyTo(binary.Stream); + //var pk = Payload; + //if (pk != null) binary.Write(pk.Data, pk.Offset, pk.Count); + Payload?.CopyTo(binary.Stream); return true; } /// 消息转数据包 /// - public Packet ToPacket() + public IPacket ToPacket() { var ms = new MemoryStream(); Write(ms, null); ms.Position = 0; - return new Packet(ms); + return new ArrayPacket(ms); } /// 创建响应 @@ -161,16 +171,19 @@ public void SetRequest(UInt16 address, UInt16 count) buf.Write(address, 0, false); buf.Write(count, 2, false); - Payload = buf; + Payload = (ArrayPacket)buf; } /// 设置请求地址和数据,填充负载数据 /// /// - public void SetRequest(UInt16 address, Packet data) + public void SetRequest(UInt16 address, IPacket data) { - Payload = new Packet(address.GetBytes(false)); - Payload.Append(data); + //Payload = new ArrayPacket(address.GetBytes(false)); + //Payload.Append(data); + var pk = new ArrayPacket(address.GetBytes(false)); + pk.Next = data; + Payload = pk; } #endregion } \ No newline at end of file diff --git a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs index 98c9d6a..810bdd4 100644 --- a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs +++ b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs @@ -45,12 +45,19 @@ public override Boolean Read(Stream stream, Object context) /// 数据包 /// 是否响应 /// - public static new ModbusRtuMessage Read(Packet data, Boolean reply = false) + public static new ModbusRtuMessage Read(IPacket data, Boolean reply = false) { var msg = new ModbusRtuMessage { Reply = reply }; return msg.Read(data.GetStream(), null) ? msg : null; } + /// 解析消息 + public static new ModbusRtuMessage Read(Byte[] data, Boolean reply = false) + { + var msg = new ModbusRtuMessage { Reply = reply }; + return msg.Read(new MemoryStream(data), null) ? msg : null; + } + /// 写入消息到数据流 /// 数据流 /// 上下文 diff --git a/NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs b/NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs index 1abbfb2..4c81afb 100644 --- a/NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs +++ b/NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs @@ -21,11 +21,11 @@ public class ModbusRtuOverTcp : ModbusIp /// 接收响应 /// - protected override Packet ReceiveCommand() + protected override IPacket ReceiveCommand() { // 设置协议最短长度,避免读取指令不完整。由于请求响应机制,不存在粘包返回。 var dataLength = 4; // 1+1+2 - Packet pk = null; + IPacket pk = null; for (var i = 0; i < 3; i++) { // 阻塞读取 @@ -48,7 +48,7 @@ protected override Packet ReceiveCommand() /// 目标数据包 /// 是否匹配请求 /// 响应消息 - protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match) + protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match) { match = true; diff --git a/NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs b/NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs index f7dbdf1..13094c7 100644 --- a/NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs +++ b/NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs @@ -21,11 +21,11 @@ public class ModbusRtuOverUdp : ModbusIp /// 接收响应 /// - protected override Packet ReceiveCommand() + protected override IPacket ReceiveCommand() { // 设置协议最短长度,避免读取指令不完整。由于请求响应机制,不存在粘包返回。 var dataLength = 4; // 1+1+2 - Packet pk = null; + IPacket pk = null; for (var i = 0; i < 3; i++) { // 阻塞读取 @@ -48,7 +48,7 @@ protected override Packet ReceiveCommand() /// 目标数据包 /// 是否匹配请求 /// 响应消息 - protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match) + protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match) { match = true; diff --git a/NewLife.Modbus/Protocols/ModbusTcp.cs b/NewLife.Modbus/Protocols/ModbusTcp.cs index a5fd5b9..c49dbe1 100644 --- a/NewLife.Modbus/Protocols/ModbusTcp.cs +++ b/NewLife.Modbus/Protocols/ModbusTcp.cs @@ -41,7 +41,7 @@ public override void Init(IDictionary parameters) /// 目标数据包 /// 是否匹配请求 /// 响应消息 - protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match) + protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match) { match = true; diff --git a/NewLife.Modbus/Protocols/ModbusUdp.cs b/NewLife.Modbus/Protocols/ModbusUdp.cs index 46e4977..8789125 100644 --- a/NewLife.Modbus/Protocols/ModbusUdp.cs +++ b/NewLife.Modbus/Protocols/ModbusUdp.cs @@ -41,7 +41,7 @@ public override void Init(IDictionary parameters) /// 目标数据包 /// 是否匹配请求 /// 响应消息 - protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match) + protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match) { match = true; diff --git a/NewLife.ModbusRTU/Controllers/DefaultSerialPort.cs b/NewLife.ModbusRTU/Controllers/DefaultSerialPort.cs index 4f76485..8ee2108 100644 --- a/NewLife.ModbusRTU/Controllers/DefaultSerialPort.cs +++ b/NewLife.ModbusRTU/Controllers/DefaultSerialPort.cs @@ -72,6 +72,9 @@ void OnReceiveSerial(Object sender, SerialDataReceivedEventArgs e) if (rs != null) { Received?.Invoke(this, new ReceivedEventArgs { Packet = rs }); + + // 回收内存池 + rs.TryDispose(); } } @@ -102,7 +105,7 @@ public virtual Int32 Read(Byte[] buffer, Int32 offset, Int32 count) /// 待发送数据 /// 等待响应数据的最小长度,默认1 /// - public virtual Packet Invoke(Packet? request, Int32 minLength) + public virtual IPacket Invoke(IPacket? request, Int32 minLength) { Open(); @@ -111,10 +114,10 @@ public virtual Packet Invoke(Packet? request, Int32 minLength) // 清空缓冲区 _port.DiscardInBuffer(); - if (request.Next == null) - _port.Write(request.Data, request.Offset, request.Count); + if (request.Next == null && request is ArrayPacket ap) + _port.Write(ap.Buffer, ap.Offset, ap.Length); else - _port.Write(request.ToArray(), 0, request.Total); + _port.Write(request.ReadBytes(), 0, request.Total); if (ByteTimeout > 10) Thread.Sleep(ByteTimeout); } @@ -122,10 +125,11 @@ public virtual Packet Invoke(Packet? request, Int32 minLength) // 串口速度较慢,等待收完数据 WaitMore(_port, minLength); - var buf = new Byte[BufferSize]; - var rs = _port.Read(buf, 0, buf.Length); + var p = new OwnerPacket(BufferSize); + var rs = _port.Read(p.Buffer, p.Offset, p.Length); + p.Resize(rs); - return new Packet(buf, 0, rs); + return p; } private void WaitMore(SerialPort sp, Int32 minLength) diff --git a/NewLife.ModbusRTU/Protocols/ModbusAscii.cs b/NewLife.ModbusRTU/Protocols/ModbusAscii.cs index bb01e68..3547dff 100644 --- a/NewLife.ModbusRTU/Protocols/ModbusAscii.cs +++ b/NewLife.ModbusRTU/Protocols/ModbusAscii.cs @@ -101,7 +101,7 @@ protected override ModbusMessage SendCommand(ModbusMessage message) try { var count = _port.Read(buf, 0, buf.Length); - var pk = new Packet(buf, 0, count); + var pk = new ArrayPacket(buf, 0, count); Log?.Debug("{0}<= {1}", PortName, pk.ToHex(32, "-")); if (span != null) span.Tag += Environment.NewLine + pk.ToHex(64, "-"); diff --git a/NewLife.ModbusRTU/Protocols/ModbusRtu.cs b/NewLife.ModbusRTU/Protocols/ModbusRtu.cs index 4e2b309..e7f99b0 100644 --- a/NewLife.ModbusRTU/Protocols/ModbusRtu.cs +++ b/NewLife.ModbusRTU/Protocols/ModbusRtu.cs @@ -119,7 +119,7 @@ protected override ModbusMessage SendCommand(ModbusMessage message) try { var count = _port.Read(buf, 0, buf.Length); - var pk = new Packet(buf, 0, count); + var pk = new ArrayPacket(buf, 0, count); Log?.Debug("{0}<= {1}", PortName, pk.ToHex(32, "-")); if (span != null) span.Tag += Environment.NewLine + pk.ToHex(64, "-"); diff --git a/Test/Test.csproj b/Test/Test.csproj index 6f19c1a..fb30e7e 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/WinModbus/FrmI6O6N.cs b/WinModbus/FrmI6O6N.cs index f92ccd9..a5ea81c 100644 --- a/WinModbus/FrmI6O6N.cs +++ b/WinModbus/FrmI6O6N.cs @@ -1,4 +1,5 @@ using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using NewLife.Log; using NewLife.Serialization; @@ -26,7 +27,7 @@ private void Form1_Load(Object sender, EventArgs e) private void btnReadInfo_Click(Object sender, EventArgs e) { var pk = _modbus.ReadRegister(_host, 0x1000, 14); - if (pk == null || pk.Count == 0) return; + if (pk == null || pk.Total == 0) return; var ms = pk.GetStream(); var reader = new Binary { Stream = ms, IsLittleEndian = false }; diff --git a/WinModbus/FrmSwitch.cs b/WinModbus/FrmSwitch.cs index 4a5e441..39f2121 100644 --- a/WinModbus/FrmSwitch.cs +++ b/WinModbus/FrmSwitch.cs @@ -1,4 +1,5 @@ using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using NewLife.Log; diff --git a/WinModbus/WinModbus.csproj b/WinModbus/WinModbus.csproj index efe30c9..45101f5 100644 --- a/WinModbus/WinModbus.csproj +++ b/WinModbus/WinModbus.csproj @@ -18,7 +18,7 @@ - + diff --git a/XUnitTest/ModbusAsciiMessageTests.cs b/XUnitTest/ModbusAsciiMessageTests.cs index bc41f9b..f7ce692 100644 --- a/XUnitTest/ModbusAsciiMessageTests.cs +++ b/XUnitTest/ModbusAsciiMessageTests.cs @@ -1,5 +1,6 @@ using System.IO; using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using Xunit; diff --git a/XUnitTest/ModbusDriverTests.cs b/XUnitTest/ModbusDriverTests.cs index ee3f83d..3587638 100644 --- a/XUnitTest/ModbusDriverTests.cs +++ b/XUnitTest/ModbusDriverTests.cs @@ -3,6 +3,7 @@ using System.Linq; using Moq; using NewLife; +using NewLife.Data; using NewLife.IoT; using NewLife.IoT.Drivers; using NewLife.IoT.Protocols; @@ -87,7 +88,7 @@ public void ReadTest() // 模拟Modbus var mb = new Mock(); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 0, 10)) - .Returns("12-34-56-78-90-12-34-56-78-90-12-34-56-78-90-12-34-56-78-90".ToHex()); + .Returns((ArrayPacket)"12-34-56-78-90-12-34-56-78-90-12-34-56-78-90-12-34-56-78-90".ToHex()); driver.Modbus = mb.Object; var points = new List(); @@ -201,9 +202,9 @@ public void ReadWithBatch() // 模拟Modbus var mb = new Mock(); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 0, 8)) - .Returns("12-34-56-78-90-12-34-56-78-90-12-34-56-78-90-12".ToHex()); + .Returns((ArrayPacket)"12-34-56-78-90-12-34-56-78-90-12-34-56-78-90-12".ToHex()); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 8, 2)) - .Returns("34-56-78-90".ToHex()); + .Returns((ArrayPacket)"34-56-78-90".ToHex()); driver.Modbus = mb.Object; var points = new List(); @@ -247,11 +248,11 @@ public void ReadWithBatch2() // 模拟Modbus var mb = new Mock(); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 0, 4)) - .Returns("12-34-56-78-90-12-34-56".ToHex()); + .Returns((ArrayPacket)"12-34-56-78-90-12-34-56".ToHex()); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 4, 4)) - .Returns("78-90-12-34-56-78-90-12".ToHex()); + .Returns((ArrayPacket)"78-90-12-34-56-78-90-12".ToHex()); mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 8, 2)) - .Returns("34-56-78-90".ToHex()); + .Returns((ArrayPacket)"34-56-78-90".ToHex()); driver.Modbus = mb.Object; var points = new List(); @@ -296,9 +297,9 @@ public void ReadRegister() //mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 100, 1)) // .Returns("01-02-00".ToHex()); mb.Setup(e => e.SendCommand(FunctionCodes.ReadRegister, 1, 100, 1)) - .Returns("02-02-00".ToHex()); + .Returns((ArrayPacket)"02-02-00".ToHex()); mb.Setup(e => e.SendCommand(FunctionCodes.ReadRegister, 1, 102, 1)) - .Returns("02-05-00".ToHex()); + .Returns((ArrayPacket)"02-05-00".ToHex()); driver.Modbus = mb.Object; var points = new List diff --git a/XUnitTest/ModbusIpMessageTests.cs b/XUnitTest/ModbusIpMessageTests.cs index 0f1fdd6..38ae350 100644 --- a/XUnitTest/ModbusIpMessageTests.cs +++ b/XUnitTest/ModbusIpMessageTests.cs @@ -1,5 +1,6 @@ using System.IO; using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using Xunit; diff --git a/XUnitTest/ModbusMessageTests.cs b/XUnitTest/ModbusMessageTests.cs index a2613fe..c3212af 100644 --- a/XUnitTest/ModbusMessageTests.cs +++ b/XUnitTest/ModbusMessageTests.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using Xunit; diff --git a/XUnitTest/ModbusRtuMessageTests.cs b/XUnitTest/ModbusRtuMessageTests.cs index 1f60928..a06b1f4 100644 --- a/XUnitTest/ModbusRtuMessageTests.cs +++ b/XUnitTest/ModbusRtuMessageTests.cs @@ -1,5 +1,6 @@ using System.IO; using NewLife; +using NewLife.Data; using NewLife.IoT.Protocols; using Xunit; diff --git a/XUnitTest/ModbusTests.cs b/XUnitTest/ModbusTests.cs index 3b575a1..af0d8cd 100644 --- a/XUnitTest/ModbusTests.cs +++ b/XUnitTest/ModbusTests.cs @@ -17,9 +17,9 @@ public void Read() //mb.Setup(e => e.Read(FunctionCodes.ReadRegister, 1, 100, 1)) // .Returns("01-02-00".ToHex()); mb.Setup(e => e.SendCommand(FunctionCodes.ReadRegister, 1, It.IsAny(), 1)) - .Returns("02-02-00".ToHex()); + .Returns((ArrayPacket)"02-02-00".ToHex()); mb.Setup(e => e.SendCommand(FunctionCodes.ReadRegister, 1, It.IsAny(), 2)) - .Returns("04-01-02-03-04".ToHex()); + .Returns((ArrayPacket)"04-01-02-03-04".ToHex()); var modbus = mb.Object; @@ -28,11 +28,11 @@ public void Read() Assert.Throws(() => modbus.Read(FunctionCodes.ReadWriteMultipleRegisters, 1, 1, 1)); // 读取 - var rs = modbus.Read(FunctionCodes.ReadRegister, 1, 100, 1) as Packet; + var rs = modbus.Read(FunctionCodes.ReadRegister, 1, 100, 1) as IPacket; Assert.NotNull(rs); Assert.Equal(0x0200, rs.ReadBytes().ToUInt16(0, false)); - rs = modbus.Read(FunctionCodes.ReadRegister, 1, 102, 2) as Packet; + rs = modbus.Read(FunctionCodes.ReadRegister, 1, 102, 2) as IPacket; Assert.NotNull(rs); Assert.Equal(0x01020304u, rs.ReadBytes().ToUInt32(0, false)); } @@ -43,7 +43,7 @@ public void ReadCoil() // 模拟Modbus var mb = new Mock() { CallBase = true }; mb.Setup(e => e.SendCommand(FunctionCodes.ReadCoil, 1, 100, 2)) - .Returns("02-12-34-56-78".ToHex()); + .Returns((ArrayPacket)"02-12-34-56-78".ToHex()); var modbus = mb.Object; @@ -62,7 +62,7 @@ public void ReadDiscrete() // 模拟Modbus var mb = new Mock() { CallBase = true }; mb.Setup(e => e.SendCommand(FunctionCodes.ReadDiscrete, 1, 100, 2)) - .Returns("02-12-34-56-78".ToHex()); + .Returns((ArrayPacket)"02-12-34-56-78".ToHex()); var modbus = mb.Object; @@ -81,7 +81,7 @@ public void ReadRegister() // 模拟Modbus var mb = new Mock() { CallBase = true }; mb.Setup(e => e.SendCommand(FunctionCodes.ReadRegister, 1, 100, 2)) - .Returns("04-12-34-56-78".ToHex()); + .Returns((ArrayPacket)"04-12-34-56-78".ToHex()); var modbus = mb.Object; @@ -101,7 +101,7 @@ public void ReadInput() // 模拟Modbus var mb = new Mock() { CallBase = true }; mb.Setup(e => e.SendCommand(FunctionCodes.ReadInput, 1, 100, 2)) - .Returns("04-12-34-56-78".ToHex()); + .Returns((ArrayPacket)"04-12-34-56-78".ToHex()); var modbus = mb.Object; diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index e1c1870..9bca347 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -11,12 +11,12 @@ - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive