72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using System.Text;
|
|
|
|
namespace Sick.ColaB.Commands;
|
|
|
|
/// <summary>
|
|
/// Command to read a value from the scanner (sRN - Read by Name)
|
|
/// </summary>
|
|
public sealed class ReadCommand(string variableName) : CommandBase($"sRN {variableName}")
|
|
{
|
|
public string VariableName { get; } = variableName;
|
|
|
|
public string? ReplyValue { get; private set; }
|
|
|
|
public byte[]? RawReplyData { get; private set; }
|
|
|
|
public override bool ProcessReply(ReadOnlyMemory<byte> replyData)
|
|
{
|
|
// Store raw reply data for binary parsing
|
|
RawReplyData = replyData.ToArray();
|
|
|
|
// Parse reply: "sAN <variableName> <value>" or "sRA <variableName> <value>"
|
|
// For binary data, only the header is ASCII, the rest is binary
|
|
// Try to find where ASCII header ends
|
|
var asciiHeaderEnd = -1;
|
|
for (int i = 0; i < replyData.Length && i < 100; i++)
|
|
{
|
|
if (replyData.Span[i] == 0 || replyData.Span[i] > 127)
|
|
{
|
|
// Found non-ASCII byte, header ends here
|
|
asciiHeaderEnd = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// If we found where ASCII ends, only decode that part
|
|
var headerLength = asciiHeaderEnd > 0 ? asciiHeaderEnd : Math.Min(replyData.Length, 100);
|
|
var replyString = Encoding.ASCII.GetString(replyData.Span[..headerLength]);
|
|
|
|
// Check if reply starts with "sAN" (Answer) or "sRA" (Read Answer)
|
|
if (!replyString.StartsWith("sAN", StringComparison.Ordinal) &&
|
|
!replyString.StartsWith("sRA", StringComparison.Ordinal))
|
|
{
|
|
WasSuccessful = false;
|
|
return false;
|
|
}
|
|
|
|
// Extract value after variable name
|
|
var parts = replyString.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (parts.Length >= 2 && parts[1] == VariableName)
|
|
{
|
|
// For binary data, ReplyValue will contain ASCII header only
|
|
// The actual binary data is in RawReplyData
|
|
if (parts.Length >= 3)
|
|
{
|
|
ReplyValue = string.Join(" ", parts.Skip(2));
|
|
}
|
|
else
|
|
{
|
|
// Binary data follows immediately after header
|
|
ReplyValue = string.Empty;
|
|
}
|
|
WasSuccessful = true;
|
|
return true;
|
|
}
|
|
|
|
WasSuccessful = false;
|
|
return false;
|
|
}
|
|
}
|
|
|