diff --git a/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 b/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 index e5f9916..d17502c 100644 Binary files a/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 and b/.vs/IoTGateway/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/IoTGateway/project-colors.json b/.vs/IoTGateway/project-colors.json index 8ead5b4..ab40e60 100644 --- a/.vs/IoTGateway/project-colors.json +++ b/.vs/IoTGateway/project-colors.json @@ -130,7 +130,12 @@ "ProjectGuid": "7752ad8c-04bf-4bd2-9272-cda4f78fa954", "DisplayName": "PluginInterface", "ColorIndex": 8 + }, + "89de240e-5393-4dd4-87c9-3ad9d44b6e7e": { + "ProjectGuid": "89de240e-5393-4dd4-87c9-3ad9d44b6e7e", + "DisplayName": "WalkingTec.Mvvm.Core", + "ColorIndex": 9 } }, - "NextColorIndex": 9 + "NextColorIndex": 10 } \ No newline at end of file diff --git a/.vs/IoTGateway/v17/.futdcache.v1 b/.vs/IoTGateway/v17/.futdcache.v1 index 855c04e..192a8d7 100644 Binary files a/.vs/IoTGateway/v17/.futdcache.v1 and b/.vs/IoTGateway/v17/.futdcache.v1 differ diff --git a/.vs/IoTGateway/v17/.suo b/.vs/IoTGateway/v17/.suo index 78b6469..70ea09d 100644 Binary files a/.vs/IoTGateway/v17/.suo and b/.vs/IoTGateway/v17/.suo differ diff --git a/.vs/IoTGateway/v17/fileList.bin b/.vs/IoTGateway/v17/fileList.bin index 4b52e0a..85df554 100644 Binary files a/.vs/IoTGateway/v17/fileList.bin and b/.vs/IoTGateway/v17/fileList.bin differ diff --git a/IoTGateway/Startup.cs b/IoTGateway/Startup.cs index b529133..a25b8f1 100644 --- a/IoTGateway/Startup.cs +++ b/IoTGateway/Startup.cs @@ -78,11 +78,12 @@ namespace IoTGateway services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IOptionsMonitor configs, DeviceService deviceService) + public void Configure(IApplicationBuilder app, IOptionsMonitor configs, DeviceService deviceService, ModbusSlaveService modbusSlaveService) { IconFontsHelper.GenerateIconFont(); diff --git a/IoTGateway/iotgateway.db b/IoTGateway/iotgateway.db index 132bff3..2bcf722 100644 Binary files a/IoTGateway/iotgateway.db and b/IoTGateway/iotgateway.db differ diff --git a/Plugins/Plugin/ModbusSlaveService.cs b/Plugins/Plugin/ModbusSlaveService.cs new file mode 100644 index 0000000..41479ff --- /dev/null +++ b/Plugins/Plugin/ModbusSlaveService.cs @@ -0,0 +1,65 @@ +using Modbus.Data; +using Modbus.Device; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Plugin +{ + public class ModbusSlaveService : IDisposable + { + TcpListener slaveTcpListener; + private Timer m_simulationTimer; + private object Lock=new object(); + private ModbusSlave slave; + private Task task { get; set; } = null; + public ModbusSlaveService() + { + byte slaveId = 1; + int port = 503; + IPAddress address = IPAddress.Any; + + // create and start the TCP slave + slaveTcpListener = new TcpListener(address, port); + slaveTcpListener.Start(); + slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener); + slave.DataStore = DataStoreFactory.CreateDefaultDataStore(); + slave.ListenAsync(); + m_simulationTimer = new Timer(DoSimulation, null, 1000, 1000); + } + + private void DoSimulation(object state) + { + try + { + lock (Lock) + { + for (int i = 1; i <= 20; i++) + { + if(i != 1|| i != 2|| i != 7) + slave.DataStore.HoldingRegisters[i] = (ushort)new Random().Next(0, short.MaxValue); + slave.DataStore.InputRegisters[i] = (ushort)new Random().Next(0, short.MaxValue); + slave.DataStore.CoilDiscretes[i] = new Random().Next() % 2 == 0; + slave.DataStore.InputDiscretes[i] = new Random().Next() % 2 == 0; + } + slave.DataStore.HoldingRegisters[1] = (ushort)new Random().Next(2000,3000); //前端要用的温度 + slave.DataStore.HoldingRegisters[2] = (ushort)new Random().Next(4000, 7000);//湿度 + slave.DataStore.HoldingRegisters[7] = (ushort)new Random().Next(0, 10000);//随机值 + } + } + catch (Exception ex) + { + Console.WriteLine($"modbus模拟数据失败了,{ex}"); + } + } + public void Dispose() + { + m_simulationTimer.Dispose(); + slaveTcpListener.Stop(); + } + } +} diff --git a/Plugins/Plugin/Plugin.csproj b/Plugins/Plugin/Plugin.csproj index 5ddbe29..57d1ccd 100644 --- a/Plugins/Plugin/Plugin.csproj +++ b/Plugins/Plugin/Plugin.csproj @@ -16,6 +16,7 @@ + diff --git a/iotgateway.db b/iotgateway.db index 132bff3..2bcf722 100644 Binary files a/iotgateway.db and b/iotgateway.db differ