Ejemplo n.º 1
0
            public override void WriteResponse(ModbusDevice device, IMessageBufferWriter writer)
            {
                writer.Push((byte)((device.Address >> 0) & 0xFF));
                writer.Push((byte)ModbusFunctionCode.ReadCoils);

                device.MemoryMap.OutputCoils.CopyTo(writer, Offset, Count);
            }
Ejemplo n.º 2
0
            public override void UpdateMemory(ModbusDevice device)
            {
                if (MessageBuffer == null)
                {
                    throw new InvalidOperationException();
                }

                device.MemoryMap.OutputRegisters.CopyFrom(MessageBuffer, WriteOffset, WriteCount);
            }
Ejemplo n.º 3
0
            public override void WriteResponse(ModbusDevice device, IMessageBufferWriter writer)
            {
                writer.Push((byte)((device.Address >> 0) & 0xFF));
                writer.Push((byte)FunctionCode);

                writer.Push((byte)((Offset >> 8) & 0xFF));
                writer.Push((byte)((Offset >> 0) & 0xFF));
                writer.Push((byte)((Count >> 8) & 0xFF));
                writer.Push((byte)((Count >> 0) & 0xFF));
            }
Ejemplo n.º 4
0
            public override void WriteResponse(ModbusDevice device, IMessageBufferWriter writer)
            {
                writer.Push((byte)((device.Address >> 0) & 0xFF));
                writer.Push((byte)ModbusFunctionCode.WriteSingleRegister);

                writer.Push((byte)((Offset >> 8) & 0xFF));
                writer.Push((byte)((Offset >> 0) & 0xFF));
                writer.Push((byte)((device.MemoryMap.OutputRegisters[Offset] >> 8) & 0xFF));
                writer.Push((byte)((device.MemoryMap.OutputRegisters[Offset] >> 0) & 0xFF));
            }
Ejemplo n.º 5
0
        private async Task <T> WriteAsync <T>(ModbusDevice device, ModbusFunctionCode functionCode, int zeroBasedOffset, Action <IMessageBufferWriter> writeAction, Func <IMessageBufferReader, Task <T> > readValueFunc, 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));
                writeAction(writer);
            }, cancellationToken);

            T returnedValue = default;

            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();
                }

                if (await reader.PushByteFromStreamAsync(cancellationToken) != zeroBasedOffset)
                {
                    throw new InvalidOperationException();
                }

                returnedValue = await readValueFunc(reader);
            }, cancellationToken);

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

            return(returnedValue !);
        }
Ejemplo n.º 6
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 !);
        }
Ejemplo n.º 7
0
        private T Write <T>(ModbusDevice device, ModbusFunctionCode functionCode, int zeroBasedOffset, Action <IMessageBufferWriter> writeAction, Func <IMessageBufferReader, T> readValueFunc)
        {
            var requestContext = new ModbusTransportContext()
            {
                TransactionIdentifier = GetTransactionIdentifier()
            };

            ModbusTransport.SendMessage(requestContext,
                                        (writer) =>
            {
                writer.Push(device.Address);
                writer.Push((byte)functionCode);
                writer.Push((byte)((zeroBasedOffset >> 8) & 0xFF));
                writer.Push((byte)((zeroBasedOffset >> 0) & 0xFF));
                writeAction(writer);
            });

            T returnedValue = default;

            var responseContext = ModbusTransport.ReceiveMessage(
                (reader) =>
            {
                if (reader.PushByteFromStream() != device.Address)
                {
                    throw new InvalidOperationException();
                }

                byte receivedFunctionCode = reader.PushByteFromStream();

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

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

                if ((ushort)((reader.PushByteFromStream() << 8) + (reader.PushByteFromStream() << 0)) != zeroBasedOffset)
                {
                    throw new InvalidOperationException();
                }

                returnedValue = readValueFunc(reader);
            });

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

            return(returnedValue !);
        }
Ejemplo n.º 8
0
 public Task <int> WriteSingleRegisterAsync(ModbusDevice device, int zeroBasedOffset, int value, CancellationToken cancellationToken)
 => WriteAsync(device, ModbusFunctionCode.WriteSingleRegister, zeroBasedOffset,
               writer =>
 {
     writer.Push((byte)((value >> 8) & 0xFF));
     writer.Push((byte)((value >> 0) & 0xFF));
 },
               async reader =>
 {
     return(await reader.PushShortFromStreamAsync(cancellationToken));
 }, cancellationToken);
Ejemplo n.º 9
0
 public int WriteSingleRegister(ModbusDevice device, int zeroBasedOffset, int value)
 => Write(device, ModbusFunctionCode.WriteSingleRegister, zeroBasedOffset,
          writer =>
 {
     writer.Push((byte)((value >> 8) & 0xFF));
     writer.Push((byte)((value >> 0) & 0xFF));
 },
          reader =>
 {
     return(reader.PushShortFromStream());
 });
Ejemplo n.º 10
0
        public void WriteMultipleRegisters(ModbusDevice device, int zeroBasedOffset, int[] values)
        => Write(device, ModbusFunctionCode.WriteMultipleRegisters, zeroBasedOffset,
                 writer =>
        {
            int count = values.Length;
            writer.Push((byte)((count >> 8) & 0xFF));
            writer.Push((byte)((count >> 0) & 0xFF));

            values.CopyTo(writer);
        },
                 reader =>
        {
            return(reader.PushShortFromStream());
        });
Ejemplo n.º 11
0
        public Task WriteMultipleCoilsAsync(ModbusDevice device, int zeroBasedOffset, bool[] values, CancellationToken cancellationToken)
        => WriteAsync(device, ModbusFunctionCode.WriteMultipleCoils, zeroBasedOffset,
                      writer =>
        {
            int count = values.Length;
            writer.Push((byte)((count >> 8) & 0xFF));
            writer.Push((byte)((count >> 0) & 0xFF));

            values.CopyTo(writer);
        },
                      async reader =>
        {
            return(await reader.PushShortFromStreamAsync(cancellationToken));
        }, cancellationToken);
Ejemplo n.º 12
0
        public Task <bool> WriteSingleCoilAsync(ModbusDevice device, int zeroBasedOffset, bool value, CancellationToken cancellationToken)
        => WriteAsync(device, ModbusFunctionCode.WriteSingleCoil, zeroBasedOffset,
                      writer =>
        {
            writer.Push((byte)(value ? 0xFF : 0x00));
            writer.Push(0);
        },
                      async reader =>
        {
            var value = await reader.PushByteFromStreamAsync(cancellationToken) == 0xFF;
            reader.PushByteFromStream();

            return(value);
        }, cancellationToken);
Ejemplo n.º 13
0
        public bool WriteSingleCoil(ModbusDevice device, int zeroBasedOffset, bool value)
        => Write(device, ModbusFunctionCode.WriteSingleCoil, zeroBasedOffset,
                 writer =>
        {
            writer.Push((byte)(value ? 0xFF : 0x00));
            writer.Push(0);
        },
                 reader =>
        {
            var value = reader.PushByteFromStream() == 0xFF;
            reader.PushByteFromStream();

            return(value);
        });
Ejemplo n.º 14
0
 public abstract void UpdateMemory(ModbusDevice device);
Ejemplo n.º 15
0
 public Task ReadInputRegistersAsync(ModbusDevice device, int zeroBasedOffset, int count, CancellationToken cancellationToken)
 => ReadAsync(device, ModbusFunctionCode.ReadInputRegisters, zeroBasedOffset, count, receivedBuffer => device.MemoryMap.InputRegisters.CopyFrom(receivedBuffer, zeroBasedOffset, count), cancellationToken);
Ejemplo n.º 16
0
 public void ReadInputRegisters(ModbusDevice device, int zeroBasedOffset, int count)
 => Read(device, ModbusFunctionCode.ReadInputRegisters, zeroBasedOffset, count, receivedBuffer => device.MemoryMap.InputRegisters.CopyFrom(receivedBuffer, zeroBasedOffset, count));
Ejemplo n.º 17
0
 public void ReadCoils(ModbusDevice device, int zeroBasedOffset, int count)
 => Read(device, ModbusFunctionCode.ReadCoils, zeroBasedOffset, count, receivedBuffer => device.MemoryMap.OutputCoils.CopyFrom(receivedBuffer, zeroBasedOffset, count));
Ejemplo n.º 18
0
 public override void UpdateMemory(ModbusDevice device)
 {
     device.MemoryMap.OutputRegisters[Offset] = Value;
 }
Ejemplo n.º 19
0
 public abstract void WriteResponse(ModbusDevice device, IMessageBufferWriter writer);