diff --git a/.gitignore b/.gitignore
index 1c9a181..c0239a5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -240,3 +240,4 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
+/测试用模拟工具
diff --git a/ConsoleAppStd/ConsoleAppStd.csproj b/ConsoleAppStd/ConsoleAppStd.csproj
index 820f23d..a076d6e 100644
--- a/ConsoleAppStd/ConsoleAppStd.csproj
+++ b/ConsoleAppStd/ConsoleAppStd.csproj
@@ -2,11 +2,11 @@
Exe
- net5.0
+ net6.0
-
+
diff --git a/ConsoleAppStd/Program.cs b/ConsoleAppStd/Program.cs
index 423e386..9da83e9 100644
--- a/ConsoleAppStd/Program.cs
+++ b/ConsoleAppStd/Program.cs
@@ -1,4 +1,5 @@
-using System;
+using EasyModbus;
+using System;
namespace ConsoleAppStd
{
@@ -7,16 +8,174 @@ class Program
static void Main(string[] args)
{
- Console.WriteLine("Hello World!");
-
+ //EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("COM3");
+ //modbusClient.UnitIdentifier = 254;
+ //modbusClient.Connect();
EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient();
modbusClient.Connect("127.0.0.1", 502);
- bool[] response = modbusClient.ReadDiscreteInputs(0, 2);
+ ushort startingAddress = 0;
+ int maxQuantity = 10;
+ // DOTest(startingAddress, maxQuantity, modbusClient);
+ // DITest(startingAddress,maxQuantity,modbusClient);
+
+ //AOTest(startingAddress,maxQuantity,modbusClient);
+ //AIReadTest(startingAddress, maxQuantity, modbusClient);
+ AIWriteTest(startingAddress, maxQuantity, modbusClient);
modbusClient.Disconnect();
- Console.WriteLine("Value of Discrete Input #1: " + response[0].ToString());
- Console.WriteLine("Value of Discrete Input #2: " + response[1].ToString());
Console.ReadKey();
}
+
+ private static void AIWriteTest(ushort startingAddress, int _, ModbusClient modbusClient)
+ {
+ modbusClient.WriteMultipleRegisters(startingAddress, new byte[] { 0, 99, 0, 77, 0, 55, 0, 33, 0, 11 });
+ modbusClient.WriteMultipleRegisters(startingAddress, new short[] { 1010, 99, 88, 77, 66, 55, 44, 33, 22, 11 });
+ modbusClient.WriteMultipleRegisters(startingAddress, new ushort[] { 11, 22, 33, 44, 55 });
+ modbusClient.WriteMultipleRegisters(startingAddress, new int[] { 11, 22, 33, 44, 55 });
+ modbusClient.WriteMultipleRegisters(startingAddress, new long[] { 11, 22 });
+
+ modbusClient.WriteSingleRegister(startingAddress, (byte)11);
+ modbusClient.WriteSingleRegister(startingAddress, (short)22);
+ var ret = modbusClient.ReadWriteMultipleRegisters(startingAddress, 2, 5, new int[] { 32, 34 });
+ }
+
+ private static void AIReadTest(ushort startingAddress, int maxQuantity, ModbusClient modbusClient)
+ {
+ {
+ Console.WriteLine($"Start AIReadTest Byte");
+ var response = modbusClient.ReadInputRegisters(startingAddress, maxQuantity * 2);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Byte Value of InputRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AIReadTest Int16");
+ var response = modbusClient.ReadInputRegisters(startingAddress, maxQuantity);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int16 Value of InputRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AIReadTest Int32");
+ var response = modbusClient.ReadInputRegisters(startingAddress, maxQuantity / 2);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int32 Value of InputRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AIReadTest Int64");
+ var response = modbusClient.ReadInputRegisters(startingAddress, maxQuantity / 4);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int64 Value of InputRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"Finish AIReadTest");
+ Console.WriteLine($"--------------------------------------");
+ }
+ }
+
+ private static void AOTest(ushort startingAddress, int maxQuantity, ModbusClient modbusClient)
+ {
+ {
+ Console.WriteLine($"Start AOTest Byte");
+ var response = modbusClient.ReadHoldingRegisters(startingAddress, maxQuantity * 2);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Byte Value of HoldingRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AOTest Int16");
+ var response = modbusClient.ReadHoldingRegisters(startingAddress, maxQuantity);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int16 Value of HoldingRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AOTest Int32");
+ var response = modbusClient.ReadHoldingRegisters(startingAddress, maxQuantity / 2);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int32 Value of HoldingRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"--------------------------------------");
+ }
+
+ {
+ Console.WriteLine($"Start AOTest Int64");
+ var response = modbusClient.ReadHoldingRegisters(startingAddress, maxQuantity / 4);
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Int64 Value of HoldingRegisters #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"Finish AOTest");
+ Console.WriteLine($"--------------------------------------");
+ }
+ }
+
+ private static void DITest(ushort startingAddress, int maxQuantity, ModbusClient modbusClient)
+ {
+ {
+ Console.WriteLine($"Start DITest");
+ var response = modbusClient.ReadDiscreteInputs(startingAddress, maxQuantity);
+
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Value of Discrete Input #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"Finish DITest");
+ Console.WriteLine($"--------------------------------------");
+ }
+ }
+
+ ///
+ ///
+ ///
+ ///
+ /// 最大读取寄存器数量
+ ///
+ private static void DOTest(ushort startingAddress, int maxQuantity, ModbusClient modbusClient)
+ {
+ {
+ Console.WriteLine($"Start DOTest");
+ var response = modbusClient.ReadCoils(startingAddress, maxQuantity);
+
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Value of Discrete Output #{i + 1:00}: {response[i]}");
+ response[i] = !response[i];
+ }
+
+ Console.WriteLine($"--------------------------------------");
+ Console.WriteLine($"取反");
+ Console.WriteLine($"--------------------------------------");
+
+ modbusClient.WriteMultipleCoils(0, response);
+
+ response = modbusClient.ReadCoils(startingAddress, maxQuantity);
+
+ for (int i = 0; i < response.Length; i++)
+ {
+ Console.WriteLine($"Value of Discrete Output #{i + 1:00}: {response[i]}");
+ }
+ Console.WriteLine($"Finish DOTest");
+ Console.WriteLine($"--------------------------------------");
+ }
+ }
}
+
}
diff --git a/EasyModbus.sln b/EasyModbus.sln
index f19c59c..07165dc 100644
--- a/EasyModbus.sln
+++ b/EasyModbus.sln
@@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32811.315
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyModbus_Net40", "EasyModbus\EasyModbus_Net40.csproj", "{7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}"
-EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyModbusServerSimulator", "EasyModbusServerSimulator\EasyModbusServerSimulator.csproj", "{871BD4C1-69F0-45B9-8807-1620E6A896A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyModbusClientExample", "EasyModbusClientExample\EasyModbusClientExample.csproj", "{9412077F-7DFF-45D0-AFF9-7D5303118EA7}"
@@ -13,9 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyModbusAdvancedClient",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAppStd", "ConsoleAppStd\ConsoleAppStd.csproj", "{62AFCA11-8005-4D14-9F29-89DED1076335}"
EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyModbus_V5.0", "EasyModbus_NET5\EasyModbus_V5.0.csproj", "{83AFFC60-BC95-46B1-861F-B1F80270FBFA}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyModbus_Net60", "EasyModbus_Net60\EasyModbus_Net60.csproj", "{6BD5A885-D0A3-44C0-BF73-E2118519EAFE}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyModbus", "EasyModbus\EasyModbus.csproj", "{16573E74-5836-4792-8935-92CDCEBBEB84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -45,54 +41,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|ARM.ActiveCfg = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|ARM.Build.0 = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|x64.ActiveCfg = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|x64.Build.0 = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|x86.ActiveCfg = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Debug|x86.Build.0 = Debug|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|Any CPU.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|Any CPU.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|ARM.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|ARM.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|x64.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|x64.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|x86.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial|x86.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|Any CPU.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|Any CPU.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|ARM.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|ARM.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|x64.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|x64.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|x86.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial1|x86.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|Any CPU.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|Any CPU.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|ARM.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|ARM.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|x64.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|x64.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|x86.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial2|x86.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|Any CPU.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|Any CPU.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|ARM.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|ARM.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|x64.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|x64.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|x86.ActiveCfg = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.DebugCommercial3|x86.Build.0 = DebugCommercial|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|Any CPU.Build.0 = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|ARM.ActiveCfg = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|ARM.Build.0 = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|x64.ActiveCfg = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|x64.Build.0 = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|x86.ActiveCfg = Release|Any CPU
- {7657FC4A-AEDF-4F17-B3E9-0D0DFB1CE23B}.Release|x86.Build.0 = Release|Any CPU
{871BD4C1-69F0-45B9-8807-1620E6A896A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{871BD4C1-69F0-45B9-8807-1620E6A896A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{871BD4C1-69F0-45B9-8807-1620E6A896A9}.Debug|ARM.ActiveCfg = Debug|Any CPU
@@ -285,108 +233,60 @@ Global
{62AFCA11-8005-4D14-9F29-89DED1076335}.Release|x64.Build.0 = Release|Any CPU
{62AFCA11-8005-4D14-9F29-89DED1076335}.Release|x86.ActiveCfg = Release|Any CPU
{62AFCA11-8005-4D14-9F29-89DED1076335}.Release|x86.Build.0 = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|ARM.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|ARM.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|x64.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|x64.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|x86.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Debug|x86.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|Any CPU.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|Any CPU.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|ARM.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|ARM.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|x64.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|x64.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|x86.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial|x86.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|Any CPU.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|Any CPU.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|ARM.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|ARM.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|x64.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|x64.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|x86.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial1|x86.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|Any CPU.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|Any CPU.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|ARM.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|ARM.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|x64.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|x64.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|x86.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial2|x86.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|Any CPU.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|Any CPU.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|ARM.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|ARM.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|x64.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|x64.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|x86.ActiveCfg = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.DebugCommercial3|x86.Build.0 = Debug|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|Any CPU.Build.0 = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|ARM.ActiveCfg = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|ARM.Build.0 = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|x64.ActiveCfg = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|x64.Build.0 = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|x86.ActiveCfg = Release|Any CPU
- {83AFFC60-BC95-46B1-861F-B1F80270FBFA}.Release|x86.Build.0 = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|ARM.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|ARM.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|x64.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|x64.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|x86.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Debug|x86.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|Any CPU.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|Any CPU.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|ARM.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|ARM.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|x64.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|x64.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|x86.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial|x86.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|Any CPU.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|Any CPU.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|ARM.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|ARM.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|x64.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|x64.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|x86.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial1|x86.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|Any CPU.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|Any CPU.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|ARM.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|ARM.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|x64.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|x64.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|x86.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial2|x86.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|Any CPU.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|Any CPU.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|ARM.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|ARM.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|x64.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|x64.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|x86.ActiveCfg = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.DebugCommercial3|x86.Build.0 = Debug|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|Any CPU.Build.0 = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|ARM.ActiveCfg = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|ARM.Build.0 = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|x64.ActiveCfg = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|x64.Build.0 = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|x86.ActiveCfg = Release|Any CPU
- {6BD5A885-D0A3-44C0-BF73-E2118519EAFE}.Release|x86.Build.0 = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|ARM.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|ARM.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|x64.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Debug|x86.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|Any CPU.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|Any CPU.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|ARM.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|ARM.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|x64.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|x64.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|x86.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial|x86.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|Any CPU.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|Any CPU.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|ARM.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|ARM.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|x64.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|x64.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|x86.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial1|x86.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|Any CPU.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|Any CPU.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|ARM.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|ARM.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|x64.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|x64.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|x86.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial2|x86.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|Any CPU.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|Any CPU.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|ARM.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|ARM.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|x64.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|x64.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|x86.ActiveCfg = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.DebugCommercial3|x86.Build.0 = Debug|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|Any CPU.Build.0 = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|ARM.ActiveCfg = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|ARM.Build.0 = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|x64.ActiveCfg = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|x64.Build.0 = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|x86.ActiveCfg = Release|Any CPU
+ {16573E74-5836-4792-8935-92CDCEBBEB84}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
- VisualSVNWorkingCopyRoot = .
SolutionGuid = {7628B1C9-8DDB-4DBD-A71D-D369A4A166DE}
+ VisualSVNWorkingCopyRoot = .
EndGlobalSection
EndGlobal
diff --git a/EasyModbus/AdvancedModbusClient.cs b/EasyModbus/AdvancedModbusClient.cs
index 2c322f0..e5ef069 100644
--- a/EasyModbus/AdvancedModbusClient.cs
+++ b/EasyModbus/AdvancedModbusClient.cs
@@ -1,32 +1,606 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
namespace EasyModbus
{
public partial class ModbusClient
{
- /*
- public enum DataType { Short = 0, UShort = 1, Long = 2, ULong = 3, Float = 4, Double = 5 };
- public object[] ReadHoldingRegisters(int startingAddress, int quantity, DataType dataType, RegisterOrder registerOrder)
+ ///
+ /// Read Holding Registers from Master device (FC3).
+ /// 读取AO
+ ///
+ /// only byte short ushort int uint float long ulong double
+ /// 从第几个寄存器开始读取 一个寄存器 2 byte
+ /// 要读取多少数据
+ /// 接收到的原始数据 是高位在前还是地位在前 高低位关系理解可能有误 还未测试 如错误 取相反值即可
+ ///
+ public T[] ReadHoldingRegisters(ushort startingAddress, int quantity, RegisterOrder registerOrder = RegisterOrder.HighLow) where T : struct
{
- int quantityToRead = quantity;
- if (dataType == DataType.Long | dataType == DataType.ULong | dataType == DataType.Float)
+ var quantityToRead = GetQuanityToRead(quantity);
+
+ var data = this.ReadHoldingRegistersBuffer(startingAddress, quantityToRead);
+
+ return SetRegistersResult(quantity, registerOrder, data);
+ }
+
+ ///
+ /// Read Input Registers from Master device (FC4).
+ /// 读取AI
+ ///
+ /// only byte short ushort int uint float long ulong double
+ /// 从第几个寄存器开始读取 一个寄存器 2 byte
+ /// 要读取多少数据
+ /// 接收到的原始数据 是高位在前还是地位在前 高低位关系理解可能有误 还未测试 如错误 取相反值即可
+ ///
+ public T[] ReadInputRegisters(ushort startingAddress, int quantity, RegisterOrder registerOrder = RegisterOrder.HighLow) where T : struct
+ {
+ var quantityToRead = GetQuanityToRead(quantity);
+
+ var data = this.ReadInputRegistersBuffer(startingAddress, quantityToRead);
+
+ return SetRegistersResult(quantity, registerOrder, data);
+ }
+
+ ///
+ /// Write multiple registers to Master device (FC16).
+ /// 写AO
+ ///
+ /// only byte short ushort int uint float long ulong double
+ /// 从第几个寄存器开始读取 一个寄存器 2 byte
+ ///
+ /// 接收到的原始数据 是高位在前还是地位在前 高低位关系理解可能有误 还未测试 如错误 取相反值即可
+ public void WriteMultipleRegisters(ushort startingAddress, T[] values, RegisterOrder registerOrder = RegisterOrder.HighLow) where T : struct
+ {
+ if (debug)
+ {
+ StringBuilder debugString = new StringBuilder();
+ for (int i = 0; i < values.Length; i++)
+ {
+ debugString.Append(values[i]);
+ debugString.Append(" ");
+ }
+ StoreLogData.Instance.Store("FC16 (Write multiple Registers to Server device), StartingAddress: " + startingAddress + ", Values: " + debugString, System.DateTime.Now);
+ }
+
+ WriteMultipleRegistersBuffer(startingAddress, SetRegistersWriteData(values, registerOrder));
+ }
+
+ ///
+ /// Write single Register to Master device (FC6).
+ /// 写AO
+ ///
+ /// only byte short ushort
+ /// 从第几个寄存器开始写 一个寄存器 2 byte
+ ///
+ /// 接收到的原始数据 是高位在前还是地位在前 高低位关系理解可能有误 还未测试 如错误 取相反值即可
+ public void WriteSingleRegister(ushort startingAddress, T value, RegisterOrder registerOrder = RegisterOrder.HighLow) where T : struct
+ {
+ if (debug)
+ {
+ StoreLogData.Instance.Store("FC6 (Write single register to Master device), StartingAddress: " + startingAddress + ", Value: " + value, System.DateTime.Now);
+ }
+
+ byte[] data;
+ if (typeof(T) == typeof(byte))
+ {
+ data = new byte[2];
+ data[0] = Convert.ToByte(value);
+ }
+ else if (typeof(T) == typeof(short))
+ {
+ data = BitConverter.GetBytes(Convert.ToInt16(value));
+ }
+ else if (typeof(T) == typeof(ushort))
+ {
+ data = BitConverter.GetBytes(Convert.ToUInt16(value));
+ }
+ else
+ {
+ throw new Exception("只支持 byte short ushort ");
+ }
+
+ if (registerOrder == RegisterOrder.LowHigh)
+ {
+ byte temp = data[0];
+ data[0] = data[1];
+ data[1] = temp;
+ }
+
+ WriteSingleRegisterBuffer(startingAddress, data);
+ }
+
+ ///
+ /// Read/Write Multiple Registers (FC23).
+ /// 读写AO
+ ///
+ /// 从第几个寄存器开始读取 一个寄存器 2 byte <
+ /// 要读取多少数据
+ /// 从第几个寄存器开始写 一个寄存器 2 byte
+ /// Values to write
+ /// Int Array which contains the Holding registers
+ public T[] ReadWriteMultipleRegisters(ushort startingAddressRead, int quantityRead, ushort startingAddressWrite, T[] values
+ , RegisterOrder registerOrder = RegisterOrder.HighLow) where T : struct
+ {
+ if (debug)
+ {
+ var debugString = new StringBuilder();
+ for (int i = 0; i < values.Length; i++)
+ {
+ debugString.Append(values[i]);
+ debugString.Append(" ");
+ }
+ StoreLogData.Instance.Store("FC23 (Read and Write multiple Registers to Server device), StartingAddress Read: " + startingAddressRead + ", Quantity Read: " + quantityRead + ", startingAddressWrite: " + startingAddressWrite + ", Values: " + debugString, System.DateTime.Now);
+ }
+
+ var quantityToRead = GetQuanityToRead(quantityRead);
+
+ var data = this.ReadWriteMultipleRegistersBuffer(startingAddressRead, quantityToRead, startingAddressWrite, SetRegistersWriteData(values, registerOrder));
+
+ return SetRegistersResult(quantityRead, registerOrder, data);
+ }
+
+ ///
+ /// 设置AO 写数据
+ ///
+ /// only byte short ushort int uint float long ulong double
+ ///
+ ///
+ ///
+ ///
+ private static byte[] SetRegistersWriteData(T[] values, RegisterOrder registerOrder) where T : struct
+ {
+ byte[] data;
+ if (typeof(T) == typeof(byte))
+ {
+ if (values.Length % 2 == 0)
+ {
+ data = values.Cast().ToArray();
+ }
+ else
+ {
+ data = new byte[values.Length + 1];
+
+ Array.Copy(values, data, values.Length);
+ }
+ }
+ else if (typeof(T) == typeof(short))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 2];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(ushort))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 2];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(int))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 4];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(uint))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 4];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(float))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 4];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(double))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 8];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(long))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 8];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else if (typeof(T) == typeof(ulong))
+ {
+ var valueRets = values.Cast().ToArray();
+ data = new byte[valueRets.Length * 8];
+
+ for (int i = 0; i < valueRets.Length; i++)
+ {
+ var tempBytes = BitConverter.GetBytes(valueRets[i]);
+
+ for (int j = 0; j < tempBytes.Length; j++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ data[i * tempBytes.Length + j] = tempBytes[tempBytes.Length - j - 1];
+ }
+ else
+ {
+ data[i * tempBytes.Length + j] = tempBytes[j];
+ }
+ }
+ }
+ }
+ else
+ {
+ throw new Exception("只支持 byte short ushort int uint float long ulong double ");
+ }
+
+ return data;
+ }
+
+ ///
+ /// 获取实际读取几个寄存器
+ ///
+ /// only byte short ushort int uint float long ulong double
+ ///
+ ///
+ /// 数据类型不符合要求
+ private static int GetQuanityToRead(int quantity) where T : struct
+ {
+ int quantityToRead;
+ if (typeof(T) == typeof(byte))
+ {
+ quantityToRead = quantity / 2;
+ if (quantity % 2 != 0)
+ {
+ quantityToRead = quantityToRead + 1;
+ }
+ }
+ else if (typeof(T) == typeof(short)
+ || typeof(T) == typeof(ushort))
+ {
+ quantityToRead = quantity;
+ }
+ else if (typeof(T) == typeof(int)
+ || typeof(T) == typeof(uint)
+ || typeof(T) == typeof(float))
+ {
quantityToRead = quantity * 2;
- if (dataType == DataType.Float)
+ }
+ else if (typeof(T) == typeof(double)
+ || typeof(T) == typeof(long)
+ || typeof(T) == typeof(ulong))
+ {
quantityToRead = quantity * 4;
- int[] response = this.ReadHoldingRegisters(startingAddress, quantityToRead);
- switch (dataType)
+ }
+ else
+ {
+ throw new Exception("只支持 byte short ushort int uint float long ulong double ");
+ }
+
+ return quantityToRead;
+ }
+
+ ///
+ /// 设置AI/AO实际返回值
+ ///
+ /// only byte short ushort int uint float long ulong double
+ ///
+ ///
+ ///
+ ///
+ private static T[] SetRegistersResult(int quantity, RegisterOrder registerOrder, byte[] data) where T : struct
+ {
+ T[] result = new T[quantity];
+ if (typeof(T) == typeof(byte))
+ {
+ Array.Copy(data.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(short))
+ {
+ var ret = new short[quantity];
+ byte[] temp = new byte[2];
+
+ for (int i = 0; i < quantity; i++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ for (int j = 0; j < temp.Length; j++)
+ {
+ temp[j] = data[i * temp.Length + j];
+ }
+
+ for (int j = 0; j < temp.Length; j++)
+ {
+ data[i * temp.Length + j] = temp[temp.Length - j - 1];
+ }
+ }
+
+ ret[i] = BitConverter.ToInt16(data, i * temp.Length);
+ }
+ Array.Copy(ret.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(ushort))
+ {
+ var ret = new ushort[quantity];
+ byte[] temp = new byte[2];
+
+ for (int i = 0; i < quantity; i++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ for (int j = 0; j < temp.Length; j++)
+ {
+ temp[j] = data[i * temp.Length + j];
+ }
+
+ for (int j = 0; j < temp.Length; j++)
+ {
+ data[i * temp.Length + j] = temp[temp.Length - j - 1];
+ }
+ }
+
+ ret[i] = BitConverter.ToUInt16(data, i * temp.Length);
+ }
+ Array.Copy(ret.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(int))
+ {
+ var ret = new int[quantity];
+ byte[] temp = new byte[4];
+
+ for (int i = 0; i < quantity; i++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ for (int j = 0; j < temp.Length; j++)
+ {
+ temp[j] = data[i * temp.Length + j];
+ }
+
+ for (int j = 0; j < temp.Length; j++)
+ {
+ data[i * temp.Length + j] = temp[temp.Length - j - 1];
+ }
+ }
+
+ ret[i] = BitConverter.ToInt32(data, i * temp.Length);
+ }
+ Array.Copy(ret.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(uint))
+ {
+ var ret = new uint[quantity];
+ byte[] temp = new byte[4];
+
+ for (int i = 0; i < quantity; i++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ for (int j = 0; j < temp.Length; j++)
+ {
+ temp[j] = data[i * temp.Length + j];
+ }
+
+ for (int j = 0; j < temp.Length; j++)
+ {
+ data[i * temp.Length + j] = temp[temp.Length - j - 1];
+ }
+ }
+
+ ret[i] = BitConverter.ToUInt32(data, i * temp.Length);
+ }
+ Array.Copy(ret.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(float))
+ {
+ var ret = new float[quantity];
+ byte[] temp = new byte[4];
+
+ for (int i = 0; i < quantity; i++)
+ {
+ if (registerOrder == RegisterOrder.HighLow)
+ {
+ for (int j = 0; j < temp.Length; j++)
+ {
+ temp[j] = data[i * temp.Length + j];
+ }
+
+ for (int j = 0; j < temp.Length; j++)
+ {
+ data[i * temp.Length + j] = temp[temp.Length - j - 1];
+ }
+ }
+
+ ret[i] = BitConverter.ToSingle(data, i * temp.Length);
+ }
+ Array.Copy(ret.Cast().ToArray(), result, result.Length);
+ }
+ else if (typeof(T) == typeof(double))
{
- case DataType.Short: return response.Cast