serSetDataReceiver (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

2.60.00


serSetDataReceiver registers a callback function that is called when raw data is received on a serial port.

The serial port must be opened with serOpen before the callback receiver is enabled. The callback receives the VPL port number, an ACCESS ARRAY containing the received bytes, and the number of valid bytes in the array.

The received data is delivered in blocks of up to 255 bytes. The data array is valid only while the callback function is running. If the application needs the data after the callback has returned, copy the bytes to application-owned storage.

serSetDataReceiver cannot be used at the same time as serFrameReceiver on the same serial port.

 

Input:

port : SINT (default: 0)

The serial port to configure.

 

cb : CALLBACK

The callback function to call when data is received. Set to 0 to disable the data receiver.

 

Returns: INT

0

- Success.

-1

- The serial port is not open.

-2

- serFrameReceiver is active on the serial port.

-3

- The serial port could not be configured.

-4

- Callback resources could not be allocated.

 

Declaration:

FUNCTION ALIGN serSetDataReceiver:INT;
VAR_INPUT
 port : SINT := 0;
 cb : CALLBACK;
END_VAR;
 
FUNCTION CALLBACK serDataReceiver;
VAR_INPUT
 port : SINT;
 data : ACCESS ARRAY[0..254OF USINT;
 size : USINT;
END_VAR;
END_FUNCTION;
 

The callback function must use the serDataReceiver declaration shown above.

 

Example:

INCLUDE rtcu.inc
 
FUNCTION CALLBACK OnSerialData;
VAR_INPUT
 port : SINT;
 data : ACCESS ARRAY[0..254OF USINT;
 size : USINT;
END_VAR;
VAR
 rc : INT;
END_VAR;
 
IF size 0 THEN
 rc := serSendData(port := port, data := ADDR(data[0]), size := size, timeout := 1000);
END_IF;
END_FUNCTION;
 
PROGRAM test;
VAR
 rc : INT;
END_VAR;
 
rc := serOpen(port := 1, baud := 115200);
rc := serSetDataReceiver(port := 1, cb := @OnSerialData);
 
END_PROGRAM;