internal static void CopyFrom(this IModbusRegistersTable table, MessageBufferSpan messageBufferSpan, int offset, int count)
 {
     for (var i = 0; i < count; i++)
     {
         table[i + offset] = (ushort)((messageBufferSpan[i * 2] << 8) + messageBufferSpan[i * 2 + 1] & 0xFFFF);
     }
 }
Exemplo n.º 2
0
        private async Task ReadAsync(ModbusDevice device, ModbusFunctionCode functionCode, int zeroBasedOffset, int count, Action <MessageBufferSpan> actionWithReturnedBuffer, CancellationToken cancellationToken)
        {
            var requestContext = new ModbusTransportContext()
            {
                TransactionIdentifier = GetTransactionIdentifier()
            };

            await ModbusTransport.SendMessageAsync(requestContext,
                                                   (writer) =>
            {
                writer.Push(device.Address);
                writer.Push((byte)functionCode);
                writer.Push((byte)((zeroBasedOffset >> 8) & 0xFF));
                writer.Push((byte)((zeroBasedOffset >> 0) & 0xFF));
                writer.Push((byte)((count >> 8) & 0xFF));
                writer.Push((byte)((count >> 0) & 0xFF));
            }, cancellationToken);

            MessageBufferSpan?receivedBuffer = null;

            var responseContext = await ModbusTransport.ReceiveMessageAsync(
                async (reader) =>
            {
                if (await reader.PushByteFromStreamAsync(cancellationToken) != device.Address)
                {
                    throw new InvalidOperationException();
                }

                byte receivedFunctionCode = await reader.PushByteFromStreamAsync(cancellationToken);

                if (receivedFunctionCode == ((byte)functionCode | 0x80))
                {
                    var exceptionCode = (ModbusExceptionCode)await reader.PushByteFromStreamAsync(cancellationToken);
                    throw new ModbusException(exceptionCode);
                }

                if (receivedFunctionCode != (byte)functionCode)
                {
                    throw new InvalidOperationException();
                }

                var byteCount = await reader.PushByteFromStreamAsync(cancellationToken);

                await reader.PushFromStreamAsync(byteCount, cancellationToken);

                receivedBuffer = new MessageBufferSpan(reader.Buffer, (ushort)(reader.Buffer.Length - byteCount), byteCount);
            }, cancellationToken);

            if (responseContext.TransactionIdentifier != requestContext.TransactionIdentifier)
            {
                throw new InvalidOperationException();
            }

            actionWithReturnedBuffer(receivedBuffer !);
        }
Exemplo n.º 3
0
            protected override async Task InitializeAsync(IMessageBufferReader reader, CancellationToken cancellationToken)
            {
                await base.InitializeAsync(reader, cancellationToken);

                Count = await reader.PushShortFromStreamAsync(cancellationToken);

                var byteCount = await reader.PushByteFromStreamAsync(cancellationToken);

                await reader.PushFromStreamAsync(byteCount, cancellationToken);

                MessageBuffer = new MessageBufferSpan(reader.Buffer, (ushort)(reader.Buffer.Length - byteCount - 2), byteCount);
            }
Exemplo n.º 4
0
            protected override void Initialize(IMessageBufferReader reader)
            {
                base.Initialize(reader);

                Count = reader.PushShortFromStream();

                var byteCount = reader.PushByteFromStream();

                reader.PushFromStream(byteCount);

                MessageBuffer = new MessageBufferSpan(reader.Buffer, (ushort)(reader.Buffer.Length - byteCount - 2), byteCount);
            }
        internal static void CopyFrom(this IModbusCoilsTable table, MessageBufferSpan messageBufferSpan, int offset, int count)
        {
            var n = Math.DivRem(count, 8, out var res);

            if (res > 0)
            {
                n++;
            }

            for (var i = 0; i < n; i++)
            {
                byte currentByte = messageBufferSpan[i];
                for (var j = 0; j < 8 && count > 0; j++)
                {
                    var index = i * 8 + j + offset;
                    if (index < table.Length)
                    {
                        table[index] =
                            ((currentByte & (byte)(0x1 << j)) > 0);
                    }
                    count--;
                }
            }
        }