Exemplo n.º 1
0
        /// <summary>
        /// Reads the specified number of values of type <typeparamref name="TRead"/> from and writes the provided array of type <typeparamref name="TWrite"/> to the holding registers. The write operation is performed before the read.
        /// </summary>
        /// <typeparam name="TRead">Determines the type of the returned data.</typeparam>
        /// <typeparam name="TWrite">Determines the type of the provided data.</typeparam>
        /// <param name="unitIdentifier">The unit identifier is used to communicate via devices such as bridges, routers and gateways that use a single IP address to support multiple independent Modbus end units. Thus, the unit identifier is the address of a remote slave connected on a serial line or on other buses. Use the default values 0x00 or 0xFF when communicating to a Modbus server that is directly connected to a TCP/IP network.</param>
        /// <param name="readStartingAddress">The holding register start address for the read operation.</param>
        /// <param name="readCount">The number of elements of type <typeparamref name="TRead"/> to read.</param>
        /// <param name="writeStartingAddress">The holding register start address for the write operation.</param>
        /// <param name="dataset">The data of type <typeparamref name="TWrite"/> to write to the server.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        public async Task <Memory <TRead> > ReadWriteMultipleRegistersAsync <TRead, TWrite>(int unitIdentifier, int readStartingAddress, int readCount, int writeStartingAddress, TWrite[] dataset, CancellationToken cancellationToken = default) where TRead : unmanaged
            where TWrite : unmanaged
        {
            var unitIdentifier_converted       = this.ConvertUnitIdentifier(unitIdentifier);
            var readStartingAddress_converted  = this.ConvertUshort(readStartingAddress);
            var readCount_converted            = this.ConvertUshort(readCount);
            var writeStartingAddress_converted = this.ConvertUshort(writeStartingAddress);

            if (this.SwapBytes)
            {
                ModbusUtils.SwitchEndianness(dataset.AsSpan());
            }

            var readQuantity = this.ConvertSize <TRead>(readCount_converted);
            var byteData     = MemoryMarshal.Cast <TWrite, byte>(dataset).ToArray();

            var dataset2 = SpanExtensions.Cast <byte, TRead>(await this.ReadWriteMultipleRegistersAsync(unitIdentifier_converted, readStartingAddress_converted, readQuantity, writeStartingAddress_converted, byteData).ConfigureAwait(false));

            if (this.SwapBytes)
            {
                ModbusUtils.SwitchEndianness(dataset2);
            }

            return(dataset2);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the specified number of values of type <typeparamref name="T"/> from the holding registers.
        /// </summary>
        /// <typeparam name="T">Determines the type of the returned data.</typeparam>
        /// <param name="unitIdentifier">The unit identifier is used to communicate via devices such as bridges, routers and gateways that use a single IP address to support multiple independent Modbus end units. Thus, the unit identifier is the address of a remote slave connected on a serial line or on other buses. Use the default values 0x00 or 0xFF when communicating to a Modbus server that is directly connected to a TCP/IP network.</param>
        /// <param name="startingAddress">The holding register start address for the read operation.</param>
        /// <param name="count">The number of elements of type <typeparamref name="T"/> to read.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        public async Task <Memory <T> > ReadHoldingRegistersAsync <T>(int unitIdentifier, int startingAddress, int count, CancellationToken cancellationToken = default) where T : unmanaged
        {
            var unitIdentifier_converted  = this.ConvertUnitIdentifier(unitIdentifier);
            var startingAddress_converted = this.ConvertUshort(startingAddress);
            var count_converted           = this.ConvertUshort(count);

            var dataset = SpanExtensions.Cast <byte, T>(await
                                                        this.ReadHoldingRegistersAsync(unitIdentifier_converted, startingAddress_converted, this.ConvertSize <T>(count_converted)).ConfigureAwait(false));

            if (this.SwapBytes)
            {
                ModbusUtils.SwitchEndianness(dataset);
            }

            return(dataset);
        }