COM_CollectPacket

Reads data from a USB or serial/Bluetooth port, retrieves exactly one packet

Contents

Syntax

[type cmd statusbyte content] = COM_CollectPacket(handle)

[type cmd statusbyte content] = COM_CollectPacket(handle, 'dontcheck')

Description

[type cmd statusbyte content] = COM_CollectPacket(handle) reads one packet on the communication channel specified by the handle struct (PC system: handle struct containing e.g. serial handle, Linux system: handle struct containing file handle). The USB / Bluetooth handle struct can be obtained by the COM_OpenNXT or COM_GetDefaultNXT command. The return value type specifies the telegram type according to the LEGO Mindstorms communication protcol. The cmd value determines the specific command. status indicates if an error occured on the NXT brick. The function checkStatusByte is interpreting this information per default. The content column vector represents the remaining payload of the whole return packet. E.g. it contains the current battery level in milli volts, that then has to be converted to a valid integer from its byte representation (i.e. using wordbytes2dec).

[type cmd statusbyte content] = COM_CollectPacket(handle, 'dontcheck') disables the validation check of the status value by function checkStatusBytes.

varargin : set to 'dontcheck' if the status byte should not automatically be checked. Only use this if you expect error messages. Possible usage is for LSGetStatus, where this can happen...

For more details about the syntax of the return packet see the LEGO Mindstorms communication protocol.

Note:

This function uses the specific Bluetooth settings from the ini-file that was specified when opening the handle. Parameters used here are SendSendPause and SendReceivePause, which will cause this function to wait a certain amount of milliseconds between each consecutive send or receive operation to avoid packet loss or buffer overflows inside the blutooth stack.

Example

   COM_MakeBTConfigFile();

   handle = COM_OpenNXT('bluetooth.ini');

   [type cmd] = name2commandbytes('KEEPALIVE');
   content = [];  % no payload in NXT command KEEPALIVE
   packet = COM_CreatePacket(type, cmd, 'reply', content);

   COM_SendPacket(packet, handle);

   [type cmd statusbyte content] = COM_CollectPacket(handle);
   % Now you could check the statusbyte or interpret the content.
   % Or maybe check for valid type and cmd before...

See also

COM_CreatePacket, COM_SendPacket, COM_OpenNXT, COM_GetDefaultNXT, COM_MakeBTConfigFile, checkStatusByte,

Signature