diff --git a/NewLife.Modbus/NewLife.Modbus.csproj b/NewLife.Modbus/NewLife.Modbus.csproj
index 236aeb8..2515c79 100644
--- a/NewLife.Modbus/NewLife.Modbus.csproj
+++ b/NewLife.Modbus/NewLife.Modbus.csproj
@@ -44,6 +44,7 @@
all
runtime; build; native; contentfiles; analyzers; buildtransitive
+
diff --git a/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs b/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs
index 9508675..2848d16 100644
--- a/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs
+++ b/NewLife.Modbus/Protocols/ModbusAsciiMessage.cs
@@ -19,7 +19,7 @@ public class ModbusAsciiMessage : ModbusMessage
/// 从数据读取消息
/// 读取器
///
- public override Boolean Read(SpanReader reader)
+ public override Boolean Read(ref SpanReader reader)
{
var str = reader.ReadString(-1, Encoding.ASCII);
if (str.Length < 1 + 2 + 2 + 2 || str[0] != ':') return false;
@@ -31,7 +31,7 @@ public override Boolean Read(SpanReader reader)
var buf = str[1..p].ToHex();
var reader2 = new SpanReader(buf) { IsLittleEndian = false };
- if (!base.Read(reader2)) return false;
+ if (!base.Read(ref reader2)) return false;
Lrc = buf[^1];
Lrc2 = ModbusHelper.Lrc(buf, 0, buf.Length - 1);
@@ -47,17 +47,17 @@ public static ModbusAsciiMessage Read(ReadOnlySpan data, Boolean reply = f
{
var msg = new ModbusAsciiMessage { Reply = reply };
var reader = new SpanReader(data) { IsLittleEndian = false };
- return msg.Read(reader) ? msg : null;
+ return msg.Read(ref reader) ? msg : null;
}
/// 写入消息到数据
/// 写入器
///
- public override Boolean Write(SpanWriter writer)
+ public override Boolean Write(ref SpanWriter writer)
{
using var pk = new OwnerPacket(256);
var writer2 = new SpanWriter(pk.GetSpan()) { IsLittleEndian = false };
- if (!base.Write(writer2)) return false;
+ if (!base.Write(ref writer2)) return false;
var buf = pk.GetSpan()[..writer2.Position].ToArray();
diff --git a/NewLife.Modbus/Protocols/ModbusIpMessage.cs b/NewLife.Modbus/Protocols/ModbusIpMessage.cs
index 5b4785a..6627a17 100644
--- a/NewLife.Modbus/Protocols/ModbusIpMessage.cs
+++ b/NewLife.Modbus/Protocols/ModbusIpMessage.cs
@@ -17,7 +17,7 @@ public class ModbusIpMessage : ModbusMessage
/// 从数据读取消息
/// 读取器
///
- public override Boolean Read(SpanReader reader)
+ public override Boolean Read(ref SpanReader reader)
{
TransactionId = reader.ReadUInt16();
ProtocolId = reader.ReadUInt16();
@@ -25,7 +25,7 @@ public override Boolean Read(SpanReader reader)
var len = reader.ReadUInt16();
if (len < 1 + 1 || len > reader.FreeCapacity) return false;
- return base.Read(reader);
+ return base.Read(ref reader);
}
/// 从数据读取消息
@@ -36,13 +36,13 @@ public static ModbusIpMessage Read(ReadOnlySpan data, Boolean reply = fals
{
var msg = new ModbusIpMessage { Reply = reply };
var reader = new SpanReader(data) { IsLittleEndian = false };
- return msg.Read(reader) ? msg : null;
+ return msg.Read(ref reader) ? msg : null;
}
/// 写入消息到数据
/// 写入器
///
- public override Boolean Write(SpanWriter writer)
+ public override Boolean Write(ref SpanWriter writer)
{
writer.Write(TransactionId);
writer.Write(ProtocolId);
@@ -51,7 +51,7 @@ public override Boolean Write(SpanWriter writer)
var len = 2 + (pk?.Total ?? 0);
writer.Write((UInt16)len);
- return base.Write(writer);
+ return base.Write(ref writer);
}
/// 创建响应
diff --git a/NewLife.Modbus/Protocols/ModbusMessage.cs b/NewLife.Modbus/Protocols/ModbusMessage.cs
index 1a20486..f650ac2 100644
--- a/NewLife.Modbus/Protocols/ModbusMessage.cs
+++ b/NewLife.Modbus/Protocols/ModbusMessage.cs
@@ -47,7 +47,7 @@ public override String ToString()
/// 从数据读取消息
/// 读取器
///
- public virtual Boolean Read(SpanReader reader)
+ public virtual Boolean Read(ref SpanReader reader)
{
Host = reader.ReadByte();
@@ -61,7 +61,8 @@ public virtual Boolean Read(SpanReader reader)
return true;
}
- Payload = (ArrayPacket)reader.ReadBytes(-1).ToArray();
+ if (reader.FreeCapacity > 0)
+ Payload = (ArrayPacket)reader.ReadBytes(reader.FreeCapacity).ToArray();
return true;
}
@@ -72,7 +73,7 @@ public virtual Boolean Read(SpanReader reader)
public virtual Int32 Read(ReadOnlySpan data)
{
var reader = new SpanReader(data) { IsLittleEndian = false };
- if (!Read(reader)) return -1;
+ if (!Read(ref reader)) return -1;
return reader.Position;
}
@@ -80,7 +81,7 @@ public virtual Int32 Read(ReadOnlySpan data)
/// 写入消息到数据
/// 写入器
///
- public virtual Boolean Write(SpanWriter writer)
+ public virtual Boolean Write(ref SpanWriter writer)
{
writer.Write(Host);
@@ -107,7 +108,7 @@ public virtual Boolean Write(SpanWriter writer)
public virtual Int32 Writer(Span data)
{
var writer = new SpanWriter(data) { IsLittleEndian = false };
- if (!Write(writer)) return -1;
+ if (!Write(ref writer)) return -1;
return writer.Position;
}
@@ -118,7 +119,7 @@ public virtual IPacket ToPacket(Int32 bufferSize = 256)
{
var pk = new OwnerPacket(bufferSize);
var writer = new SpanWriter(pk.GetSpan()) { IsLittleEndian = false };
- if (!Write(writer)) return null;
+ if (!Write(ref writer)) return null;
pk.Resize(writer.Position);
diff --git a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs
index f176bb4..bf092b2 100644
--- a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs
+++ b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs
@@ -1,5 +1,7 @@
using NewLife.Buffers;
using NewLife.Data;
+using NewLife.Reflection;
+using NewLife.Security;
namespace NewLife.IoT.Protocols;
@@ -18,10 +20,10 @@ public class ModbusRtuMessage : ModbusMessage
/// 从数据读取消息
/// 读取器
///
- public override Boolean Read(SpanReader reader)
+ public override Boolean Read(ref SpanReader reader)
{
var p = reader.Position;
- if (!base.Read(reader)) return false;
+ if (!base.Read(ref reader)) return false;
// 从负载数据里把Crc取出来
var pk = Payload;
@@ -32,8 +34,12 @@ public override Boolean Read(SpanReader reader)
Payload = pk.Slice(0, count - 2);
}
- //reader.Position = p;
- //Crc2 = ModbusHelper.Crc(stream, (Int32)(stream.Length - stream.Position - 2));
+ // 计算CRC
+ var p2 = reader.Position - 2;
+ var sp = reader.Span.Slice(p, p2 - p);
+ var buf = sp.ToArray();
+
+ Crc2 = Crc16.ComputeModbus(buf, 0, buf.Length);
return true;
}
@@ -46,16 +52,16 @@ public static ModbusRtuMessage Read(ReadOnlySpan data, Boolean reply = fal
{
var msg = new ModbusRtuMessage { Reply = reply };
var reader = new SpanReader(data) { IsLittleEndian = false };
- return msg.Read(reader) ? msg : null;
+ return msg.Read(ref reader) ? msg : null;
}
/// 写入消息到数据
/// 写入器
///
- public override Boolean Write(SpanWriter writer)
+ public override Boolean Write(ref SpanWriter writer)
{
var p = writer.Position;
- if (!base.Write(writer)) return false;
+ if (!base.Write(ref writer)) return false;
//writer.Position = p;
//Crc2 = ModbusHelper.Crc(writer);
diff --git a/Test/Test.csproj b/Test/Test.csproj
index f9b9c0f..4cd49ac 100644
--- a/Test/Test.csproj
+++ b/Test/Test.csproj
@@ -16,7 +16,7 @@
-
+
diff --git a/WinModbus/WinModbus.csproj b/WinModbus/WinModbus.csproj
index e3c701a..a219ae5 100644
--- a/WinModbus/WinModbus.csproj
+++ b/WinModbus/WinModbus.csproj
@@ -18,7 +18,7 @@
-
+
diff --git a/XUnitTest/ModbusMessageTests.cs b/XUnitTest/ModbusMessageTests.cs
index 9b32d43..a96246d 100644
--- a/XUnitTest/ModbusMessageTests.cs
+++ b/XUnitTest/ModbusMessageTests.cs
@@ -1,9 +1,5 @@
using System;
-using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using NewLife;
using NewLife.Data;
using NewLife.IoT.Protocols;
@@ -20,7 +16,8 @@ public void Test1()
var dt = str.ToHex();
var msg = new ModbusMessage();
- msg.Read(dt);
+ var rs = msg.Read(dt);
+ Assert.Equal(dt.Length, rs);
Assert.NotNull(msg);
Assert.Equal(1, msg.Host);
@@ -42,7 +39,8 @@ public void Test2()
var dt = str.ToHex();
var msg = new ModbusMessage { Reply = true };
- msg.Read(dt);
+ var rs = msg.Read(dt);
+ Assert.Equal(dt.Length, rs);
Assert.NotNull(msg);
Assert.Equal(1, msg.Host);
@@ -76,5 +74,6 @@ public void Set()
msg.SetRequest(0x0002, 0xABCD);
Assert.Equal("00-02-AB-CD", msg.Payload.ToHex(256, "-"));
+ Assert.Equal("WriteRegister (0x0002, ABCD)", msg.ToString());
}
}
\ No newline at end of file
diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj
index 05a5ff9..f629093 100644
--- a/XUnitTest/XUnitTest.csproj
+++ b/XUnitTest/XUnitTest.csproj
@@ -14,7 +14,7 @@
-
+
all