/// <summary> /// Reads words holding 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[] ReadRegisters(int offset, int count, byte unit = 0x01) { if (tcpClient == null) { throw new Exception("Object not intialized"); } var request = new ModbusRequest03(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 <ModbusReply03>(); return(ReadAsShort(response.Data)); }
/// <summary> /// Reads words holding 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> ReadHoldingRegistersAsync(int offset, int count, byte unit = 0x01) { if (tcpClient == null) { throw new Exception("Object not intialized"); } var request = new ModbusRequest03(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 <ModbusReply03>(); return(response); }
/// <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) { if (tcpClient == null) { throw new Exception("Object not intialized"); } var request = new ModbusRequest03(offset, count * 2 /* Float is 2 word */); 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 <ModbusReply03>(); return(ReadAsFloat(response.Data)); }