Exemplo n.º 1
0
        /// <summary>
        /// Reads words input registers
        /// </summary>
        /// <param name="offset">The register offset</param>
        /// <param name="count">Number of words to read</param>
        /// <returns>The words read</returns>
        public async Task <IModbusReadResponseBase> ReadInputRegistersAsync(int offset, int count, byte unit = 0x01)
        {
            if (tcpClient == null)
            {
                throw new Exception("Object not intialized");
            }

            var request = new ModbusRequest04(offset, count, unit);
            var buffer  = request.ToNetworkBuffer();

            using (var cancellationTokenSource = new CancellationTokenSource(socketTimeout))
            {
                using (cancellationTokenSource.Token.Register(() => transportStream.Close()))
                {
                    await transportStream.WriteAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token);
                }
            }
            var response = await ReadResponseAsync <ModbusReply04>();

            return(response);
        }
        /// <summary>
        /// Reads words input registers
        /// </summary>
        /// <param name="offset">The register offset</param>
        /// <param name="count">Number of words to read</param>
        /// <returns>The words read</returns>
        public short[] ReadInputRegisters(int offset, int count, byte unit = 0x01)
        {
            if (tcpClient == null)
            {
                throw new Exception("Object not intialized");
            }

            var request = new ModbusRequest04(offset, count, unit);
            var buffer  = request.ToNetworkBuffer();

            using (var cancellationTokenSource = new CancellationTokenSource(socketTimeout))
            {
                using (cancellationTokenSource.Token.Register(() => transportStream.Close()))
                {
                    transportStream.Write(buffer, 0, buffer.Length);
                }
            }
            var response = ReadResponse <ModbusReply04>();

            return(ReadAsShort(response.Data));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads floats from holding registers
        /// </summary>
        /// <param name="offset">The register offset</param>
        /// <param name="count">Number of floats to read</param>
        /// <returns>The floats read</returns>
        public async Task <float[]> ReadRegistersFloatsAsync(int offset, int count, byte unitIdentifier)
        {
            if (tcpClient == null)
            {
                throw new Exception("Object not intialized");
            }

            // Float is 2 word (expect count x 2)
            var request = new ModbusRequest04(offset, count, unitIdentifier);

            var buffer = request.ToNetworkBuffer();

            using (var cancellationTokenSource = new CancellationTokenSource(socketTimeout))
            {
                using (cancellationTokenSource.Token.Register(() => transportStream.Close()))
                {
                    await transportStream.WriteAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token);
                }
            }

            var response = await ReadResponseAsync <ModbusReply04>();

            Console.BackgroundColor = ConsoleColor.DarkGray;

            Console.Write($"\n[DEBUG] Analog Input request bytes:  ");
            foreach (byte b in buffer)
            {
                Console.Write($"{b.ToString("X2")}  ");
            }

            Console.Write($"\n[DEBUG] Analog Input response (value) bytes:  ");
            foreach (byte b in response.Data)
            {
                Console.Write($"{b.ToString("X2")}  ");
            }

            Console.ResetColor();

            return(ReadAsFloat(response.Data));
        }