Доделана реализация с Modbus.
This commit is contained in:
		
							parent
							
								
									fdeeca4e79
								
							
						
					
					
						commit
						22aeb49964
					
				| @ -14,8 +14,13 @@ namespace ApiServer.Controllers | ||||
| 
 | ||||
| 		[HttpGet] | ||||
| 		[Route("modbus")] | ||||
| 		public bool GetActualState(int inputIndex) | ||||
| 		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) | ||||
| 			while (!stoppingToken.IsCancellationRequested) | ||||
| 			{ | ||||
| 			return Task.CompletedTask; | ||||
| 				CurrentState = _serialMaster.ReadInputs(slaveID, startAddress, numOfPoints); | ||||
| 				await Task.Delay(100, stoppingToken); | ||||
| 			} | ||||
| 
 | ||||
| 		public void Dispose() | ||||
| 		{ | ||||
| 			_serialPort.Dispose(); | ||||
| 			await Task.CompletedTask; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user