Skip to content

Commit

Permalink
fix: Fix USB reading request's TransferSize field value.
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtech committed Sep 21, 2024
1 parent 3c5ad1f commit 1ebe89a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 5 additions & 2 deletions ScpiNet/ScpiConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public static async Task<string> ReadString(this IScpiConnection conn, int speci
{
ReadResult chunk;
StringBuilder response = new();
byte[] buffer = new byte[1024];

// Some devices (such as the Keysight multimeters) do not like when we require reading longer than some specific constraint.
// Therefore we will use only 128-bytes long buffer for generic string reads.
byte[] buffer = new byte[128];

do {
chunk = await conn.Read(buffer, -1, specialTimeout, cancellationToken);
chunk = await conn.Read(buffer, buffer.Length, specialTimeout, cancellationToken);
response.Append(Encoding.ASCII.GetString(chunk.Data, 0, chunk.Length));
} while (!chunk.Eof && chunk.Length > 0);

Expand Down
9 changes: 5 additions & 4 deletions ScpiNet/UsbScpiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,12 @@ protected async Task<int> ReadUsb(byte[] buffer, int timeout, CancellationToken
/// <param name="msgId">Message ID.</param>
/// <param name="eom">True to set EOM flag (end of message).</param>
/// <param name="data">Data to add after the header.</param>
/// <param name="length">Length of data to be transferred. If zero, the length is inferred from the data array length.</param>
/// <returns>Byte array containing the USB TMC request.</returns>
private byte[] CreateTmcRequest(UsbTmcMsgId msgId, bool eom, byte[] data)
private byte[] CreateTmcRequest(UsbTmcMsgId msgId, bool eom, byte[] data, uint length = 0)
{
uint len = 0;
if (data != null) {
uint len = length;
if (data != null && length == 0) {
len = (uint)data.Length;
}

Expand Down Expand Up @@ -762,7 +763,7 @@ public async Task<ReadResult> Read(byte[] buffer, int readLength = -1, int speci
await EnforceCommPause(cancellationToken);

// Write reading request to the device:
await WriteUsb(CreateTmcRequest(UsbTmcMsgId.DevDepMsgIn, false, null), cancellationToken);
await WriteUsb(CreateTmcRequest(UsbTmcMsgId.DevDepMsgIn, false, null, (uint)readLength), cancellationToken);

// Receive the answer:
byte[] receptionBuffer = new byte[readLength + headerSize];
Expand Down

0 comments on commit 1ebe89a

Please sign in to comment.