chPeek (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Firmware version:

1.00 / 1.00.00


The "chPeek()" function works like the chRead() function except for the fact that the message will not be removed from the channel and will never be blocked.

 

 

Input:

ch : CHANNEL

The channel to peek the message from.        

 

msg : PTR

Pointer to buffer where the message is to be stored.

 

lenmax : INT

Size of the buffer that msg points to.

If the size of the message in the channel is larger than lenmax, only lenmax bytes will be read.

 

Returns: INT

-1

Channel not initialized.

>0

Number of bytes actually read from channel.

 

Declaration:

FUNCTION chPeek : INT;
VAR_INPUT
   ch     : CHANNEL;
   msg    : PTR;
   lenmax : INT;
END_VAR;

 

 

Example:

THREAD_BLOCK Thread;
VAR_INPUT
   ch : CHANNEL;
   id : SINT;
   ...
END_VAR;
VAR
   buf : ARRAY[1..30] OF SINT;
   len : INT;
   ...
END_VAR;
   ...
   // Do while channel initialized
   WHILE chStatus(ch:=ch) > -1 DO
      // peek at data
      len := chPeek(ch:=ch,msg:=ADDR(buf),lenmax:=SIZEOF(buf));
      IF len > 0 THEN
         // Is package sent to me?
         IF buf[1] = id THEN
            // Read data
            chRead(ch:=ch,msg:=ADDR(buf),lenmax:=SIZEOF(buf));
            // Act on data
            ...
         END_IF;
      END_IF;
      ...
   END_WHILE;
END_THREAD_BLOCK;