|
@@ -1,11 +1,13 @@
|
|
using ModbusSimulationProtocol.Interface;
|
|
using ModbusSimulationProtocol.Interface;
|
|
using ModbusSimulationProtocol.Services;
|
|
using ModbusSimulationProtocol.Services;
|
|
|
|
+using ModBusTcp;
|
|
using NModbus;
|
|
using NModbus;
|
|
|
|
+using ProtocalGeneral;
|
|
using System.Net.Sockets;
|
|
using System.Net.Sockets;
|
|
|
|
|
|
namespace ModbusMasterSlaveSim;
|
|
namespace ModbusMasterSlaveSim;
|
|
|
|
|
|
-internal class Program
|
|
|
|
|
|
+internal class Program
|
|
{
|
|
{
|
|
static void Main(string[] args)
|
|
static void Main(string[] args)
|
|
{
|
|
{
|
|
@@ -24,27 +26,46 @@ internal class Program
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- TcpClient masterTcpClient = new TcpClient("127.0.0.1", 1502);
|
|
|
|
- var factory = new ModbusFactory();
|
|
|
|
- var master = factory.CreateMaster(masterTcpClient);
|
|
|
|
|
|
+ //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);
|
|
|
|
|
|
+ //Task.Factory.StartNew(async () =>
|
|
|
|
+ //{
|
|
|
|
+ // Console.WriteLine("Waiting for 3s...");
|
|
|
|
+ // await Task.Delay(3000);
|
|
|
|
|
|
- master.WriteSingleCoil(1, 10, true);
|
|
|
|
- var rst = master.ReadCoils(1, 10, 2);
|
|
|
|
- if (rst is not null)
|
|
|
|
- {
|
|
|
|
- foreach (var point in rst)
|
|
|
|
- {
|
|
|
|
- Console.WriteLine(point);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ // master.WriteSingleCoil(1, 10, true);
|
|
|
|
+ // var rst = master.ReadCoils(1, 10, 2);
|
|
|
|
+ // if (rst is not null)
|
|
|
|
+ // {
|
|
|
|
+ // foreach (var point in rst)
|
|
|
|
+ // {
|
|
|
|
+ // Console.WriteLine(point);
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ //});
|
|
|
|
|
|
|
|
+ Modbus_Tcp modbus_Tcp=new Modbus_Tcp();
|
|
|
|
+ modbus_Tcp.Initialize("Master", new ConnectionNotify());
|
|
|
|
+ modbus_Tcp.Open("127.0.0.1", 1502);
|
|
|
|
|
|
|
|
+ modbus_Tcp.SetValue(10, 100);
|
|
|
|
+ Task.Delay(3000).Wait();
|
|
|
|
+ modbusSlaveService.TryReadValues(modbusSlaveService.SlaveDataStore.HoldingRegisters, 10, 5, out var outPoints2);
|
|
Task.Delay(Timeout.Infinite).GetAwaiter().GetResult();
|
|
Task.Delay(Timeout.Infinite).GetAwaiter().GetResult();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+public class ConnectionNotify : ITcpConnectNority
|
|
|
|
+{
|
|
|
|
+ public void Connect(string ip, int port)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void DisConnect(string ip, int port)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|