内置虚拟modbus设备,端口503

This commit is contained in:
王海东 2022-02-16 23:10:52 +08:00
parent 936b5c67f5
commit e447489694
10 changed files with 74 additions and 2 deletions

View File

@ -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
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -78,11 +78,12 @@ namespace IoTGateway
services.AddSingleton<DrvierService>();
services.AddSingleton<UAService>();
services.AddSingleton<MyMqttClient>();
services.AddSingleton<ModbusSlaveService>();
}
// 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();

Binary file not shown.

View 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();
}
}
}

View File

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\..\IoTGateway.DataAccess\IoTGateway.DataAccess.csproj" />
<ProjectReference Include="..\..\IoTGateway.Model\IoTGateway.Model.csproj" />
<ProjectReference Include="..\Drivers\DriverModbusMaster\DriverModbusMaster.csproj" />
</ItemGroup>
</Project>

Binary file not shown.