Доделана реализация с Modbus.
This commit is contained in:
parent
fdeeca4e79
commit
22aeb49964
@ -2,20 +2,25 @@
|
||||
|
||||
namespace ApiServer.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/state")]
|
||||
public class StateContoller
|
||||
{
|
||||
[HttpGet]
|
||||
public byte GetActualState()
|
||||
{
|
||||
return GetActualStateFromCom.CurrentState;
|
||||
}
|
||||
[ApiController]
|
||||
[Route("api/state")]
|
||||
public class StateContoller
|
||||
{
|
||||
[HttpGet]
|
||||
public byte GetActualState()
|
||||
{
|
||||
return GetActualStateFromCom.CurrentState;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("modbus")]
|
||||
public bool GetActualState(int inputIndex)
|
||||
[Route("modbus")]
|
||||
public ActionResult<bool> GetActualState(int inputIndex)
|
||||
{
|
||||
if (inputIndex > 1)
|
||||
{
|
||||
return new NotFoundResult();
|
||||
//throw new ArgumentOutOfRangeException(nameof(inputIndex), "Значение может быть от 0 до 1 включительно.");
|
||||
}
|
||||
return GetActualStateFromComWithModbus.CurrentState[inputIndex];
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,17 @@
|
||||
using Modbus.Device;
|
||||
using System;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ApiServer
|
||||
{
|
||||
public class GetActualStateFromComWithModbus : IHostedService, IDisposable
|
||||
public class GetActualStateFromComWithModbus : BackgroundService, IDisposable
|
||||
{
|
||||
public static bool[] CurrentState { get; set; }
|
||||
|
||||
static SerialPort _serialPort;
|
||||
static ModbusSerialMaster _serialMaster;
|
||||
private static SerialPort? _serialPort;
|
||||
private static ModbusSerialMaster? _serialMaster;
|
||||
|
||||
static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
_serialMaster = ModbusSerialMaster.CreateRtu(_serialPort);
|
||||
|
||||
byte slaveID = 1;
|
||||
ushort startAddress = 0;
|
||||
ushort numOfPoints = 2;
|
||||
CurrentState = _serialMaster.ReadInputs(slaveID, startAddress, numOfPoints);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_serialPort = new SerialPort();
|
||||
_serialPort.PortName = "COM5";
|
||||
@ -33,20 +19,19 @@ namespace ApiServer
|
||||
_serialPort.DataBits = 8;
|
||||
_serialPort.Parity = Parity.None;
|
||||
_serialPort.StopBits = StopBits.One;
|
||||
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
|
||||
_serialPort.Open();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
_serialMaster = ModbusSerialMaster.CreateRtu(_serialPort);
|
||||
byte slaveID = 1;
|
||||
ushort startAddress = 0;
|
||||
ushort numOfPoints = 2;
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_serialPort.Dispose();
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
CurrentState = _serialMaster.ReadInputs(slaveID, startAddress, numOfPoints);
|
||||
await Task.Delay(100, stoppingToken);
|
||||
}
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user