Example #1
0
        public void ModbusFrameWriter_WriteFrame_CorrectlyWritesAReadInputRegistersRequest()
        {
            // Given
            var request = new RequestAdu
            {
                Header = new Header(transactionID: 1, unitID: 4),
                Pdu    = new RequestReadInputRegisters
                {
                    Address  = 0x0040,
                    Quantity = 0x000A,
                },
            };
            var arraybuffer = new ArrayBufferWriter <byte>();

            // When
            var writer   = new ModbusFrameWriter(arraybuffer);
            var position = writer.WriteFrame(request);

            arraybuffer.Advance(position);

            // Then
            var data = arraybuffer.WrittenSpan.ToArray();

            Assert.Equal(
                new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x04, 0x04, 0x00, 0x40, 0x00, 0x0A },
                data
                );
        }
Example #2
0
        public void ModbusFrameWriter_WriteFrame_CorrectlyWritesAWriteSingleCoilRequest()
        {
            // Given
            var request = new RequestAdu
            {
                Header = new Header(transactionID: 1, unitID: 4),
                Pdu    = new RequestWriteSingleCoil
                {
                    Address   = 0x0041,
                    CoilState = true,
                },
            };
            var arraybuffer = new ArrayBufferWriter <byte>();

            // When
            var writer   = new ModbusFrameWriter(arraybuffer);
            var position = writer.WriteFrame(request);

            arraybuffer.Advance(position);

            // Then
            var data = arraybuffer.WrittenSpan.ToArray();

            Assert.Equal(
                new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x04, 0x05, 0x00, 0x41, 0xFF, 0x00 },
                data
                );
        }
Example #3
0
        public void ModbusFrameWriter_WriteFrame_CorrectlyWritesAWriteMultipleCoilsRequest(object request, byte[] expected)
        {
            // Given
            var arraybuffer = new ArrayBufferWriter <byte>();

            // When
            var writer   = new ModbusFrameWriter(arraybuffer);
            var position = writer.WriteFrame((RequestAdu)request);

            arraybuffer.Advance(position);

            // Then
            var data = arraybuffer.WrittenSpan.ToArray();

            Assert.Equal(expected, data);
        }
Example #4
0
        public async Task <List <bool> > ReadCoils(ushort address, ushort quantity, CancellationToken token = default)
        {
            ThrowIfDisposed();

            ThrowIfNotConnected();

            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            var header = CreateHeader();
            var frame  = new RequestAdu
            {
                Header = header,
                Pdu    = new RequestReadCoils
                {
                    Address  = address,
                    Quantity = quantity,
                }
            };

            var written = _writer.WriteFrame(frame);

            _connection.Transport.Output.Advance(written);

            var waitForResponse = WaitForResponse <ResponseReadCoils>(frame, token).ConfigureAwait(false);
            var flushResult     = await _connection.Transport.Output.FlushAsync(token).ConfigureAwait(false);

            var response = await waitForResponse;

            var bits   = new BitArray(response.Coils);
            var result = new List <bool>();

            for (var i = 0; i < quantity; i++)
            {
                result.Add(bits[i]);
            }

            return(result);
        }