From b2bc6c82a96ed793f21637e08814a7f20d125898 Mon Sep 17 00:00:00 2001 From: luosheng Date: Thu, 26 Oct 2023 10:27:57 +0800 Subject: [PATCH] Update --- .../ModbusRtuProtocolReceiver.cs | 51 +++++++++++++++---- Samples/ModbusTcpToRtu/ModbusTcpToRtu.csproj | 10 ++++ .../appsettings.Production.json | 21 ++++++++ Samples/ModbusTcpToRtu/service.bat | 4 ++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 Samples/ModbusTcpToRtu/appsettings.Production.json create mode 100644 Samples/ModbusTcpToRtu/service.bat diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolReceiver.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolReceiver.cs index 40ae8d8..dec919e 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolReceiver.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolReceiver.cs @@ -17,16 +17,49 @@ namespace Modbus.Net.Modbus return receiveBytes => { var writeContent = receiveBytes.Length > 6 ? new byte[receiveBytes.Length - 7] : null; - if (receiveBytes.Length > 6) Array.Copy(receiveBytes, 7, writeContent, 0, receiveBytes.Length - 7); - return new ReceiveDataDef() + if (receiveBytes.Length > 6) { - SlaveAddress = receiveBytes[0], - FunctionCode = receiveBytes[1], - StartAddress = (ushort)(receiveBytes[2] * 256 + receiveBytes[3]), - Count = (ushort)(receiveBytes[4] * 256 + receiveBytes[5]), - WriteByteCount = (byte)(receiveBytes.Length > 6 ? receiveBytes[6] : 0), - WriteContent = writeContent - }; + Array.Copy(receiveBytes, 7, writeContent, 0, receiveBytes.Length - 7); + return new ReceiveDataDef() + { + SlaveAddress = receiveBytes[0], + FunctionCode = receiveBytes[1], + StartAddress = (ushort)(receiveBytes[2] * 256 + receiveBytes[3]), + Count = (ushort)(receiveBytes[4] * 256 + receiveBytes[5]), + WriteByteCount = (byte)(receiveBytes.Length > 6 ? receiveBytes[6] : 0), + WriteContent = writeContent + }; + } + else if (receiveBytes.Length == 6) + { + if (receiveBytes[1] == 1 || receiveBytes[1] == 2) + { + writeContent = null; + return new ReceiveDataDef() + { + SlaveAddress = receiveBytes[0], + FunctionCode = receiveBytes[1], + StartAddress = (ushort)(receiveBytes[2] * 256 + receiveBytes[3]), + Count = (ushort)(receiveBytes[4] * 256 + receiveBytes[5]), + WriteByteCount = 2, + WriteContent = writeContent + }; + } + else + { + writeContent = new byte[2] { receiveBytes[4], receiveBytes[5] }; + return new ReceiveDataDef() + { + SlaveAddress = receiveBytes[0], + FunctionCode = receiveBytes[1], + StartAddress = (ushort)(receiveBytes[2] * 256 + receiveBytes[3]), + Count = 1, + WriteByteCount = 2, + WriteContent = writeContent + }; + } + } + else return null; }; } } diff --git a/Samples/ModbusTcpToRtu/ModbusTcpToRtu.csproj b/Samples/ModbusTcpToRtu/ModbusTcpToRtu.csproj index 573e877..3eaf3da 100644 --- a/Samples/ModbusTcpToRtu/ModbusTcpToRtu.csproj +++ b/Samples/ModbusTcpToRtu/ModbusTcpToRtu.csproj @@ -7,6 +7,16 @@ dotnet-ModbusTcpToRtu-b7b7d9ed-80ce-4790-86de-5c3cf21e0a2e + + + + + + + PreserveNewest + + + diff --git a/Samples/ModbusTcpToRtu/appsettings.Production.json b/Samples/ModbusTcpToRtu/appsettings.Production.json new file mode 100644 index 0000000..8e0045d --- /dev/null +++ b/Samples/ModbusTcpToRtu/appsettings.Production.json @@ -0,0 +1,21 @@ +{ + "Serilog": { + "MinimumLevel": { + "Default": "Error", + "Override": { + "Microsoft": "Error", + "Microsoft.Hosting.Lifetime": "Error" + } + } + }, + "Logging": { + "LogLevel": { + "Default": "Error", + "Microsoft": "Error", + "Microsoft.Hosting.Lifetime": "Error" + } + }, + "Quartz": { + "LogLevel": "Error" + } +} diff --git a/Samples/ModbusTcpToRtu/service.bat b/Samples/ModbusTcpToRtu/service.bat new file mode 100644 index 0000000..8c155d1 --- /dev/null +++ b/Samples/ModbusTcpToRtu/service.bat @@ -0,0 +1,4 @@ +sc delete ModbusTcpToRtu +sc create ModbusTcpToRtu "binPath=%~dp0ModbusTcpToRtu.exe" start= delayed-auto +sc start ModbusTcpToRtu +pause \ No newline at end of file