内置虚拟modbus设备,端口503
This commit is contained in:
parent
936b5c67f5
commit
e447489694
Binary file not shown.
@ -130,7 +130,12 @@
|
|||||||
"ProjectGuid": "7752ad8c-04bf-4bd2-9272-cda4f78fa954",
|
"ProjectGuid": "7752ad8c-04bf-4bd2-9272-cda4f78fa954",
|
||||||
"DisplayName": "PluginInterface",
|
"DisplayName": "PluginInterface",
|
||||||
"ColorIndex": 8
|
"ColorIndex": 8
|
||||||
|
},
|
||||||
|
"89de240e-5393-4dd4-87c9-3ad9d44b6e7e": {
|
||||||
|
"ProjectGuid": "89de240e-5393-4dd4-87c9-3ad9d44b6e7e",
|
||||||
|
"DisplayName": "WalkingTec.Mvvm.Core",
|
||||||
|
"ColorIndex": 9
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"NextColorIndex": 9
|
"NextColorIndex": 10
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -78,11 +78,12 @@ namespace IoTGateway
|
|||||||
services.AddSingleton<DrvierService>();
|
services.AddSingleton<DrvierService>();
|
||||||
services.AddSingleton<UAService>();
|
services.AddSingleton<UAService>();
|
||||||
services.AddSingleton<MyMqttClient>();
|
services.AddSingleton<MyMqttClient>();
|
||||||
|
services.AddSingleton<ModbusSlaveService>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IOptionsMonitor<Configs> configs, DeviceService deviceService)
|
public void Configure(IApplicationBuilder app, IOptionsMonitor<Configs> configs, DeviceService deviceService, ModbusSlaveService modbusSlaveService)
|
||||||
{
|
{
|
||||||
IconFontsHelper.GenerateIconFont();
|
IconFontsHelper.GenerateIconFont();
|
||||||
|
|
||||||
|
Binary file not shown.
65
Plugins/Plugin/ModbusSlaveService.cs
Normal file
65
Plugins/Plugin/ModbusSlaveService.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\IoTGateway.DataAccess\IoTGateway.DataAccess.csproj" />
|
<ProjectReference Include="..\..\IoTGateway.DataAccess\IoTGateway.DataAccess.csproj" />
|
||||||
<ProjectReference Include="..\..\IoTGateway.Model\IoTGateway.Model.csproj" />
|
<ProjectReference Include="..\..\IoTGateway.Model\IoTGateway.Model.csproj" />
|
||||||
|
<ProjectReference Include="..\Drivers\DriverModbusMaster\DriverModbusMaster.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
BIN
iotgateway.db
BIN
iotgateway.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user