51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Sick.SafetyScanners.Helpers;
|
|
|
|
namespace Sick.SafetyScanners.Cola2.Commands;
|
|
|
|
/// <summary>
|
|
/// Command to create a new COLA2 session
|
|
/// </summary>
|
|
public sealed class CreateSessionCommand : CommandBase
|
|
{
|
|
private const byte HeartbeatTimeoutSeconds = 60;
|
|
private const uint ClientId = 1;
|
|
|
|
public CreateSessionCommand()
|
|
: base(0x4F, 0x58) // 'O' and 'X' in ASCII
|
|
{
|
|
}
|
|
|
|
public override bool CanBeExecutedWithoutSessionId => true;
|
|
|
|
protected override ReadOnlyMemory<byte> AddTelegramData()
|
|
{
|
|
var data = new byte[5];
|
|
var span = data.AsSpan();
|
|
|
|
// Heartbeat timeout (1 byte)
|
|
ReadWriteHelper.WriteUint8BigEndian(span, 0, HeartbeatTimeoutSeconds);
|
|
|
|
// Client ID (4 bytes)
|
|
ReadWriteHelper.WriteUint32BigEndian(span, 1, ClientId);
|
|
|
|
return data;
|
|
}
|
|
|
|
protected override bool ProcessReplyInternal(ReadOnlyMemory<byte> replyData,
|
|
byte replyCommandType, byte replyCommandMode)
|
|
{
|
|
// Reply should be 'O' (0x4F) and 'A' (0x41) for Acknowledge
|
|
// Note: Session ID is NOT in reply data, it's in the packet header (offset 10)
|
|
// In C++ reference, ParseTCPPacket reads session ID from header and sets it to Command
|
|
// In C# implementation, Cola2Session extracts it from parseResult.SessionId
|
|
if ((replyCommandType == 0x4F && replyCommandMode == 0x41) ||
|
|
(replyCommandType == 'O' && replyCommandMode == 'A'))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|