public async Task <WriteCoilsResult> WriteHoldingCoilAsync(bool value, ushort address, CancellationToken cancellationToken) { lock (_isInitializedLock) { if (_isInitialized == false) { throw new ModbusException("This Modbus RTU Device must be Initialized first before any Requests can be Processed"); } } if (address > MaximumAddress) { throw new ArgumentOutOfRangeException(nameof(address), "The Address is greater than the Maximum Allowed Value of '" + MaximumAddress + "'"); } WriteHoldingCoilRequest request = WriteHoldingCoilRequest.CreateNew(this, address, value); ProcessRequestResult requestResult = await _channel.ProcessRequestAsync(request, _timeout, _retries, _delayBetweenMessages, cancellationToken); WriteHoldingCoilResponse.Validate(request, requestResult.Response); return(new WriteCoilsResult { BytesSent = requestResult.BytesSent, PacketsSent = requestResult.PacketsSent, BytesReceived = requestResult.BytesReceived, PacketsReceived = requestResult.PacketsReceived, Duration = requestResult.Duration, }); }
internal static void Validate(WriteHoldingCoilRequest request, RTUResponse response) { if (response.Data.Length < 4) { throw new RTUException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '4'"); } Memory <byte> bytes = response.Data.AsMemory(); Span <byte> addressBytes = bytes.Slice(0, 2).Span; addressBytes.Reverse(); ushort address = BitConverter.ToUInt16(addressBytes); if (address != request.Address) { throw new RTUException("The Response Address of '" + address.ToString() + "' did not match the Expected Address '" + request.Address.ToString() + "'"); } Span <byte> valueBytes = bytes.Slice(2, 2).Span; valueBytes.Reverse(); ushort value = BitConverter.ToUInt16(valueBytes); ushort expectedValue = (ushort)(request.Value ? 0xFF00 : 0x0000); if (value != expectedValue) { throw new RTUException("The Response Coil Value of '" + value.ToString() + "' did not match the Expected Coil Value '" + expectedValue.ToString() + "'"); } }