protected bool SendCommand( byte[] command, byte[] reply, int expectedReplyLen )
{
bool result = false;
uint status;
IntPtr queue;
lock ( sync )
{
// check if GhostAPI stack was created (if device is connected)
if ( stack == IntPtr.Zero )
{
throw new NullReferenceException( "Not connected to RCX brick" );
}
// create command queue
status = GhostAPI.GhCreateCommandQueue( out queue );
if ( !GhostAPI.PBK_SUCCEEDED( status ) )
return false;
// append command to the queue
status = GhostAPI.GhAppendCommand( queue, command, command.Length, expectedReplyLen );
if ( GhostAPI.PBK_SUCCEEDED( status ) )
{
// execute command
status = GhostAPI.GhExecute( stack, queue );
if ( GhostAPI.PBK_SUCCEEDED( status ) )
{
IntPtr commandHandle;
uint replyLen;
// get first command and its reply data lenght
if (
( GhostAPI.PBK_SUCCEEDED( GhostAPI.GhGetFirstCommand( queue, out commandHandle ) ) ) &&
( GhostAPI.PBK_SUCCEEDED( GhostAPI.GhGetCommandReplyLen( commandHandle, out replyLen ) ) )
)
{
// check provided reply buffer size
if ( reply.Length < replyLen )
throw new ArgumentException( "Reply buffer is too small" );
// get reply data
status = GhostAPI.GhGetCommandReply( commandHandle, reply, replyLen );
if ( GhostAPI.PBK_SUCCEEDED( status ) )
{
// check that reply corresponds to command
if ( ( command[0] | 0x08 ) != (byte) ~reply[0] )
throw new ApplicationException( "Reply does not correspond to command" );
result = true;
}
}
}
}
// destroy command queue
GhostAPI.GhDestroyCommandQueue( queue );
}
return result;
}
}