Skip to content

Commit

Permalink
升级到v11基础组件
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 28, 2024
1 parent 78ee7e4 commit 7efa2c6
Show file tree
Hide file tree
Showing 27 changed files with 129 additions and 77 deletions.
3 changes: 2 additions & 1 deletion NewLife.Modbus/Drivers/ModbusDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using NewLife.Data;
using NewLife.IoT.Protocols;
using NewLife.IoT.ThingModels;
using NewLife.IoT.ThingSpecification;
Expand Down Expand Up @@ -144,7 +145,7 @@ public override IDictionary<String, Object> 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());
}
Expand Down
12 changes: 6 additions & 6 deletions NewLife.Modbus/ModbusSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -99,15 +99,15 @@ 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;
}

return null;
}

Packet OnWriteRegister(ModbusMessage msg)
IPacket OnWriteRegister(ModbusMessage msg)
{
var regs = Host.Registers;
if (regs == null) return null;
Expand All @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion NewLife.Modbus/NewLife.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.IoT" Version="2.1.2024.203" />
<PackageReference Include="NewLife.IoT" Version="2.3.2024.1106" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 9 additions & 9 deletions NewLife.Modbus/Protocols/Modbus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public virtual void Open() { }
/// <param name="address">地址。例如0x0002</param>
/// <param name="value">数据值</param>
/// <returns>返回响应消息的负载部分</returns>
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;
Expand All @@ -69,7 +69,7 @@ public virtual Packet SendCommand(FunctionCodes code, Byte host, UInt16 address,
/// <param name="host">主机。一般是1</param>
/// <param name="data">数据</param>
/// <returns>返回响应消息的负载部分</returns>
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;
Expand All @@ -95,7 +95,7 @@ public virtual Packet SendCommand(FunctionCodes code, Byte host, Packet data)
/// <param name="count">个数。寄存器个数或线圈个数</param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
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)
{
Expand All @@ -115,7 +115,7 @@ public virtual Packet Read(FunctionCodes code, Byte host, UInt16 address, UInt16
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">线圈数量。一般要求8的倍数</param>
/// <returns>线圈状态字节数组</returns>
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
Expand Down Expand Up @@ -144,7 +144,7 @@ public Packet ReadCoil(Byte host, UInt16 address, UInt16 count)
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">输入数量。一般要求8的倍数</param>
/// <returns>输入状态字节数组</returns>
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
Expand Down Expand Up @@ -173,7 +173,7 @@ public Packet ReadDiscrete(Byte host, UInt16 address, UInt16 count)
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">寄存器数量。每个寄存器2个字节</param>
/// <returns>寄存器值数组</returns>
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
Expand Down Expand Up @@ -202,7 +202,7 @@ public Packet ReadRegister(Byte host, UInt16 address, UInt16 count)
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">输入寄存器数量。每个寄存器2个字节</param>
/// <returns>输入寄存器值数组</returns>
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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion NewLife.Modbus/Protocols/ModbusAsciiMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ public override Boolean Read(Stream stream, Object context)
/// <param name="data">数据包</param>
/// <param name="reply">是否响应</param>
/// <returns></returns>
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;
}

/// <summary>解析消息</summary>
/// <param name="data"></param>
/// <param name="reply"></param>
/// <returns></returns>
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;
}

/// <summary>写入消息到数据流</summary>
/// <param name="stream">数据流</param>
/// <param name="context">上下文</param>
Expand Down
6 changes: 3 additions & 3 deletions NewLife.Modbus/Protocols/ModbusIp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public override void Open()
/// <param name="data">目标数据包</param>
/// <param name="match">是否匹配请求</param>
/// <returns>响应消息</returns>
protected abstract ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match);
protected abstract ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match);

/// <summary>接收响应</summary>
/// <returns></returns>
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++)
{
// 阻塞读取
Expand Down
12 changes: 11 additions & 1 deletion NewLife.Modbus/Protocols/ModbusIpMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ public override Boolean Read(Stream stream, Object context)
/// <param name="data">数据包</param>
/// <param name="reply">是否响应</param>
/// <returns></returns>
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;
}

/// <summary>解析消息</summary>
/// <param name="data"></param>
/// <param name="reply"></param>
/// <returns></returns>
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;
}

/// <summary>写入消息到数据流</summary>
/// <param name="stream">数据流</param>
/// <param name="context">上下文</param>
Expand Down
37 changes: 25 additions & 12 deletions NewLife.Modbus/Protocols/ModbusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ModbusMessage : IAccessor

/// <summary>负载数据</summary>
[IgnoreDataMember]
public Packet Payload { get; set; }
public IPacket Payload { get; set; }
#endregion

#region 构造
Expand Down Expand Up @@ -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;
}
Expand All @@ -73,14 +73,24 @@ public virtual Boolean Read(Stream stream, Object context)
/// <param name="data">数据包</param>
/// <param name="reply">是否响应</param>
/// <returns></returns>
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;

return null;
}

/// <summary>解析消息</summary>
/// <param name="data"></param>
/// <param name="reply"></param>
/// <returns></returns>
public static ModbusMessage Read(Byte[] data, Boolean reply = false)
{
var msg = new ModbusMessage { Reply = reply };
return msg.Read(new MemoryStream(data), null) ? msg : null;
}

/// <summary>写入消息到数据流</summary>
/// <param name="stream">数据流</param>
/// <param name="context">上下文</param>
Expand All @@ -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;
}

/// <summary>消息转数据包</summary>
/// <returns></returns>
public Packet ToPacket()
public IPacket ToPacket()
{
var ms = new MemoryStream();
Write(ms, null);

ms.Position = 0;
return new Packet(ms);
return new ArrayPacket(ms);
}

/// <summary>创建响应</summary>
Expand Down Expand Up @@ -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;
}

/// <summary>设置请求地址和数据,填充负载数据</summary>
/// <param name="address"></param>
/// <param name="data"></param>
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
}
9 changes: 8 additions & 1 deletion NewLife.Modbus/Protocols/ModbusRtuMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ public override Boolean Read(Stream stream, Object context)
/// <param name="data">数据包</param>
/// <param name="reply">是否响应</param>
/// <returns></returns>
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;
}

/// <summary>解析消息</summary>
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;
}

/// <summary>写入消息到数据流</summary>
/// <param name="stream">数据流</param>
/// <param name="context">上下文</param>
Expand Down
6 changes: 3 additions & 3 deletions NewLife.Modbus/Protocols/ModbusRtuOverTcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class ModbusRtuOverTcp : ModbusIp

/// <summary>接收响应</summary>
/// <returns></returns>
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++)
{
// 阻塞读取
Expand All @@ -48,7 +48,7 @@ protected override Packet ReceiveCommand()
/// <param name="data">目标数据包</param>
/// <param name="match">是否匹配请求</param>
/// <returns>响应消息</returns>
protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match)
protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match)
{
match = true;

Expand Down
6 changes: 3 additions & 3 deletions NewLife.Modbus/Protocols/ModbusRtuOverUdp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class ModbusRtuOverUdp : ModbusIp

/// <summary>接收响应</summary>
/// <returns></returns>
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++)
{
// 阻塞读取
Expand All @@ -48,7 +48,7 @@ protected override Packet ReceiveCommand()
/// <param name="data">目标数据包</param>
/// <param name="match">是否匹配请求</param>
/// <returns>响应消息</returns>
protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match)
protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match)
{
match = true;

Expand Down
2 changes: 1 addition & 1 deletion NewLife.Modbus/Protocols/ModbusTcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void Init(IDictionary<String, Object> parameters)
/// <param name="data">目标数据包</param>
/// <param name="match">是否匹配请求</param>
/// <returns>响应消息</returns>
protected override ModbusMessage ReadMessage(ModbusMessage request, Packet data, out Boolean match)
protected override ModbusMessage ReadMessage(ModbusMessage request, IPacket data, out Boolean match)
{
match = true;

Expand Down
Loading

0 comments on commit 7efa2c6

Please sign in to comment.