Skip to content

Commit

Permalink
feat(#2): ScpiConnectionExtensions: Add ReadBytes() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtech committed Oct 3, 2023
1 parent 2438111 commit 736bd41
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ScpiNet/ScpiConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ScpiNet
{
Expand Down Expand Up @@ -50,6 +51,27 @@ public static async Task<string> ReadString(this IScpiConnection conn, int speci
return response.ToString().TrimEnd('\r', '\n');
}

/// <summary>
/// Reads a byte array from the device. The reading is done until all data is read.
/// </summary>
/// <param name="conn">Connection to read bytes from.</param>
/// <param name="specialTimeout">Special timeout (milliseconds). If zero (default value), uses Timeout property value for timeout.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Bytes retrieved from the device.</returns>
public static async Task<byte[]>ReadBytes(this IScpiConnection conn, int specialTimeout = 0, CancellationToken cancellationToken = default)
{
var result = new MemoryStream();
var buffer = new byte[1024];

ReadResult chunk;
do {
chunk = await conn.Read(buffer, -1, specialTimeout, cancellationToken);
result.Write(buffer, 0, chunk.Length);
} while (!chunk.Eof && chunk.Length > 0);

return result.ToArray();
}

/// <summary>
/// Gets instrument identification using *IDN? command.
/// </summary>
Expand Down

0 comments on commit 736bd41

Please sign in to comment.