Skip to content

Commit

Permalink
fix: Query() method correctly recognizes and strips the response header.
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtech committed Sep 14, 2023
1 parent 8fa1e42 commit f54549d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ScpiNet/ScpiDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ protected async Task<string> Query(string command)
Logger?.LogDebug($"Query: {command}");
await Connection.WriteString(command, true);
string response = await Connection.ReadString();

// The response should start with the :command, but instead of trailing
// question mark there is a space and the response. This header has to be removed:
string expectedHeader = ":" + command.Replace("?", " ");
if (!response.StartsWith(expectedHeader)) {
throw new Exception($"Cannot find response header: '{response}'.");
}

// Remove the header and return the response:
response = response.Substring(expectedHeader.Length);
Logger?.LogDebug($"Response: {response}");
return response;
}
Expand Down Expand Up @@ -169,11 +179,8 @@ protected async Task<Dictionary<string, string>> QueryDictionary(string command)
/// <returns>Double precision number which is result of the query.</returns>
protected async Task<double> QueryDouble(string command)
{
Logger?.LogDebug($"Query double: {command}");
string doubleStr = await Query(command);
double response = double.Parse(doubleStr, NumberStyle, CultureInfo.InvariantCulture);
Logger?.LogDebug($"Response: {response}");
return response;
return double.Parse(doubleStr, NumberStyle, CultureInfo.InvariantCulture);
}
}
}

0 comments on commit f54549d

Please sign in to comment.