SenGao 2 주 전
부모
커밋
623b4aac9b

+ 19 - 7
CommunicationProtocols/ModbusSimulationProtocol/Data/SlaveDataStore.cs

@@ -4,28 +4,40 @@ namespace ModbusSimulationProtocol.Data;
 
 public class SlaveDataStore : ISlaveDataStore
 {
-    private readonly IPointSource<bool> _coilInputs;
     private readonly IPointSource<bool> _coilDiscretes;
+    private readonly IPointSource<bool> _coilInputs;
     private readonly IPointSource<ushort> _holdingRegisters;
     private readonly IPointSource<ushort> _inputRegisters;
 
     public SlaveDataStore(
-        int coilCount,
-        int discreteInputCount,
+        int coilDiscreteCount,
+        int coilInputCount,
         int holdingRegisterCount,
         int inputRegisterCount)
     {
-        _coilInputs = new PointSource<bool>(coilCount, writeable: true);
-        _coilDiscretes = new PointSource<bool>(discreteInputCount, writeable: false);
+        _coilDiscretes = new PointSource<bool>(coilDiscreteCount, writeable: true);
+        _coilInputs = new PointSource<bool>(coilInputCount, writeable: false);
         _holdingRegisters = new PointSource<ushort>(holdingRegisterCount, writeable: true);
         _inputRegisters = new PointSource<ushort>(inputRegisterCount, writeable: false);
     }
 
-    public IPointSource<bool> CoilInputs => _coilInputs;
-
+    /// <summary>
+    /// Read and Write
+    /// </summary>
     public IPointSource<bool> CoilDiscretes => _coilDiscretes;
 
+    /// <summary>
+    /// Read Only
+    /// </summary>
+    public IPointSource<bool> CoilInputs => _coilInputs;
+
+    /// <summary>
+    /// Read and Write
+    /// </summary>
     public IPointSource<ushort> HoldingRegisters => _holdingRegisters;
 
+    /// <summary>
+    /// Read Only
+    /// </summary>
     public IPointSource<ushort> InputRegisters => _inputRegisters;
 }

+ 1 - 1
CommunicationProtocols/ModbusSimulationProtocol/Interface/IModbusSlaveService.cs

@@ -1,6 +1,6 @@
 namespace ModbusSimulationProtocol.Interface;
 
-public interface IModbusSlaveService
+public interface IModbusSlaveService : IDisposable
 {
     void Initialize(string ip, ushort port);
     void ActivateNetwork();

+ 1 - 1
CommunicationProtocols/ModbusSimulationProtocol/Services/ModbusSlaveService.cs

@@ -6,7 +6,7 @@ using System.Net.Sockets;
 
 namespace ModbusSimulationProtocol.Services;
 
-public class ModbusSlaveService : IModbusSlaveService, IDisposable
+public class ModbusSlaveService : IModbusSlaveService
 {
     private readonly IModbusLogger? _logger;
 

+ 7 - 7
Minics.sln

@@ -93,10 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdsCommunicatorNet8", "Comm
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLCIOPointTool", "Tools\PLCIOPointTool\PLCIOPointTool.csproj", "{9C3E268E-4268-479E-9508-EFD76639B899}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusSlaveSim", "Tools\ModbusSlaveSim\ModbusSlaveSim.csproj", "{722495E5-E221-47F9-9045-7FC9E08948BC}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusSimulationProtocol", "CommunicationProtocols\ModbusSimulationProtocol\ModbusSimulationProtocol.csproj", "{2B31769C-22A4-41CF-97B0-16A18488E81F}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusMasterSlaveSim", "Tools\ModbusMasterSlaveSim\ModbusMasterSlaveSim.csproj", "{2BC402FF-09C6-4C5E-8166-8EF16D125CAB}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -239,14 +239,14 @@ Global
 		{9C3E268E-4268-479E-9508-EFD76639B899}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9C3E268E-4268-479E-9508-EFD76639B899}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9C3E268E-4268-479E-9508-EFD76639B899}.Release|Any CPU.Build.0 = Release|Any CPU
-		{722495E5-E221-47F9-9045-7FC9E08948BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{722495E5-E221-47F9-9045-7FC9E08948BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{722495E5-E221-47F9-9045-7FC9E08948BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{722495E5-E221-47F9-9045-7FC9E08948BC}.Release|Any CPU.Build.0 = Release|Any CPU
 		{2B31769C-22A4-41CF-97B0-16A18488E81F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{2B31769C-22A4-41CF-97B0-16A18488E81F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2B31769C-22A4-41CF-97B0-16A18488E81F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2B31769C-22A4-41CF-97B0-16A18488E81F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{2BC402FF-09C6-4C5E-8166-8EF16D125CAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{2BC402FF-09C6-4C5E-8166-8EF16D125CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{2BC402FF-09C6-4C5E-8166-8EF16D125CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{2BC402FF-09C6-4C5E-8166-8EF16D125CAB}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -282,8 +282,8 @@ Global
 		{EF692000-6C78-4EA6-81BF-58371231510E} = {2783D72A-0926-427D-B70D-5F3F5FA757FC}
 		{D784466C-1F32-4621-1B3D-D1AC0CC08709} = {165A518A-753D-4062-A100-9367AF27F900}
 		{9C3E268E-4268-479E-9508-EFD76639B899} = {2783D72A-0926-427D-B70D-5F3F5FA757FC}
-		{722495E5-E221-47F9-9045-7FC9E08948BC} = {2783D72A-0926-427D-B70D-5F3F5FA757FC}
 		{2B31769C-22A4-41CF-97B0-16A18488E81F} = {165A518A-753D-4062-A100-9367AF27F900}
+		{2BC402FF-09C6-4C5E-8166-8EF16D125CAB} = {2783D72A-0926-427D-B70D-5F3F5FA757FC}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {2E8CB4A6-1AFA-4CB2-BA42-1BAFC71AD68B}

+ 16 - 0
Tools/ModbusMasterSlaveSim/ModbusLogger.cs

@@ -0,0 +1,16 @@
+using NModbus;
+
+namespace ModbusMasterSlaveSim;
+
+public class ModbusLogger : IModbusLogger
+{
+    public void Log(LoggingLevel level, string message)
+    {
+        Console.WriteLine($"{level}->{message}");
+    }
+
+    public bool ShouldLog(LoggingLevel level)
+    {
+        throw new NotImplementedException();
+    }
+}

+ 16 - 0
Tools/ModbusMasterSlaveSim/ModbusMasterSlaveSim.csproj

@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net8.0</TargetFramework>
+    <OutputPath>$(SolutionDir)Binary\Tools\ModbusMasterSlaveSim</OutputPath>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+    <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\..\CommunicationProtocols\ModbusSimulationProtocol\ModbusSimulationProtocol.csproj" />
+  </ItemGroup>
+
+</Project>

+ 35 - 0
Tools/ModbusMasterSlaveSim/Program.cs

@@ -0,0 +1,35 @@
+using ModbusSimulationProtocol.Interface;
+using ModbusSimulationProtocol.Services;
+using NModbus;
+using System.Net.Sockets;
+
+namespace ModbusMasterSlaveSim;
+
+internal class Program
+{
+    static void Main(string[] args)
+    {
+        IModbusLogger logger =new ModbusLogger();
+        IModbusSlaveService modbusSlaveService =new ModbusSlaveService(logger);
+        modbusSlaveService.Initialize("127.0.0.1", 1502);
+        modbusSlaveService.AddSlave(1, 64, 64, 64, 64);
+        modbusSlaveService.AddSlave(2, 128, 128, 128, 128);
+        modbusSlaveService.ActivateNetwork();
+
+        TcpClient masterTcpClient = new TcpClient("127.0.0.1", 1502);
+        var factory = new ModbusFactory();
+        var master = factory.CreateMaster(masterTcpClient);
+
+        Task.Factory.StartNew(async () => 
+        {
+            Console.WriteLine("Waiting for 3s...");
+            await Task.Delay(3000);
+
+            master.WriteSingleCoil(1, 0, true);
+            var rst = master.ReadCoils(1, 0, 2);
+        });
+
+
+        Task.Delay(Timeout.Infinite).GetAwaiter().GetResult();
+    }
+}

+ 0 - 9
Tools/ModbusSlaveSim/App.xaml

@@ -1,9 +0,0 @@
-<Application x:Class="ModbusSlaveSim.App"
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:local="clr-namespace:ModbusSlaveSim"
-             StartupUri="MainWindow.xaml">
-    <Application.Resources>
-         
-    </Application.Resources>
-</Application>

+ 0 - 13
Tools/ModbusSlaveSim/App.xaml.cs

@@ -1,13 +0,0 @@
-using System.Configuration;
-using System.Data;
-using System.Windows;
-
-namespace ModbusSlaveSim;
-
-/// <summary>
-/// Interaction logic for App.xaml
-/// </summary>
-public partial class App : Application
-{
-}
-

+ 0 - 10
Tools/ModbusSlaveSim/AssemblyInfo.cs

@@ -1,10 +0,0 @@
-using System.Windows;
-
-[assembly:ThemeInfo(
-    ResourceDictionaryLocation.None,            //where theme specific resource dictionaries are located
-                                                //(used if a resource is not found in the page,
-                                                // or application resource dictionaries)
-    ResourceDictionaryLocation.SourceAssembly   //where the generic resource dictionary is located
-                                                //(used if a resource is not found in the page,
-                                                // app, or any theme specific resource dictionaries)
-)]

+ 0 - 12
Tools/ModbusSlaveSim/MainWindow.xaml

@@ -1,12 +0,0 @@
-<Window x:Class="ModbusSlaveSim.MainWindow"
-        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-        xmlns:local="clr-namespace:ModbusSlaveSim"
-        mc:Ignorable="d"
-        Title="MainWindow" Height="450" Width="800">
-    <Grid>
-        
-    </Grid>
-</Window>

+ 0 - 23
Tools/ModbusSlaveSim/MainWindow.xaml.cs

@@ -1,23 +0,0 @@
-using System.Text;
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
-
-namespace ModbusSlaveSim;
-
-/// <summary>
-/// Interaction logic for MainWindow.xaml
-/// </summary>
-public partial class MainWindow : Window
-{
-    public MainWindow()
-    {
-        InitializeComponent();
-    }
-}

+ 0 - 18
Tools/ModbusSlaveSim/ModbusSlaveSim.csproj

@@ -1,18 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
-  <PropertyGroup>
-    <OutputType>WinExe</OutputType>
-    <TargetFramework>net8.0-windows</TargetFramework>
-    <OutputPath>$(SolutionDir)Binary\Tools\ModbusSlaveSim</OutputPath>
-    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
-    <Nullable>enable</Nullable>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <UseWPF>true</UseWPF>
-    <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
-  </PropertyGroup>
-
-  <ItemGroup>
-    <PackageReference Include="Prism.DryIoc" />
-    <PackageReference Include="CommunityToolkit.Mvvm" />
-  </ItemGroup>
-</Project>