canitpSessionCreate (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, NX-400, LX2, LX5

Firmware version:

1.96.00


Create an ISO-TP session using an already opened CAN port.

 

A callback function is used to notify the application that a message is received. (See CALLBACK for more information)

 

The ISO-TP protocol uses two way communication so the CAN write capability must be enabled.

If CAN write is not enabled, or monitor mode is used, then messages cannot be received or send.

 

A maximum of 30 sessions can be created on a CAN port, if none of its filters are in use by the other functions.

To create more than a total of 16 connections, it is necessary to reduce the size of the internal transmit and receive buffers. If e.g. only sending small messages, the transmit buffer can be reduced significantly, increasing the total number of connections.

 

 

 

Input:

port : SINT (1/2) (default 1)

The port of the CAN bus to use for ISO-TP.

 

src : DINT

The CAN ID to filter for incoming ISO-TP messages.

 

dst : DINT

The CAN ID where ISO-TP messages are sent to.

 

xtd : BOOL (default TRUE)

Set to FALSE to use standard identifiers (11 bits), leave at TRUE for extended identifiers (29 bits).

 

cb_receive : CALLBACK

The callback function for received messages. (See the canitpRecv callback declaration below)

 

cb_data : PTR

The buffer where the received message will be stored.

 

cb_arg : DINT

User argument to pass on to the callback. Can e.g. be used to select the buffer to read from.

 

cb_size : INT (1..4095)

The size of the buffer to place the received data in.

If an incoming ISO-TP message is too large for the receive buffer it will be discarded.

 

rx_size : INT (6..4095, default 4095)

The size of the internal receive buffer. Can be reduced to increase the number of simultaneous sessions.

 

tx_size : INT (6..4095, default 4095)

The size of the internal transmit buffer. Can be reduced to increase the number of simultaneous sessions.

 

 

Output:

handle : SYSHANDLE

The handle to this ISO-TP session.

 

 

Returns: INT

1

- Successful.

0

- Not available.

-2

- The CAN port is not open

-3

- Invalid source address

-4

- Invalid destination address

-5

- Invalid function pointer

-6

- Invalid buffer or size

-7

- Failed to create CAN filter for session.

-8

- Not enough resources for connection, try reducing internal buffer size or closing some of the sessions.

 

Declaration:

FUNCTION canitpSessionCreate : INT;
VAR_INPUT
  handle  : ACCESS SYSHANDLE;
  src     : DINT;
  dst     : DINT;
  cb_receive : CALLBACK;
  cb_data : PTR;
  cb_arg  : DINT;
  cb_size : INT;
  rx_size : INT;
  tx_size : INT;
  xtd     : BOOL := TRUE;
  port    : SINT := 1;
END_VAR;

 

Callback declaration:

FUNCTION CALLBACK canitpRecv
VAR_INPUT
  handle  : SYSHANDLE;
  arg     : DINT;
  size    : INT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
VAR
  buf   : ARRAY [1..4096] OF SINT;
END_VAR;
 
FUNCTION CALLBACK canitpRecv
VAR_INPUT
  handle : SYSHANDLE; //Handle to session
  arg    : DINT; //User data
  size   : INT; //Size of the data received.
END_VAR;
  DebugMsg(message := strFromMemory(src := PTR(arg), len := size));
END_FUNCTION;
 
 
PROGRAM example;
VAR
  handle: SYSHANDLE;
  rc    : INT;
  str   : STRING;
  out   : ARRAY [1..4095] OF SINT;
  len   : INT;
END_VAR;
 
  // Open can
  canOpen(baud := 250, monitor := FALSE);
 
  // Create ISO-TP session
  rc := canitpSessionCreate(handle:=handle,
                  port     := 1,
                  src      := 16#12345678,
                  dst      := 16#12345679,
                  cb_receive := @canitpRecv,
                  cb_data    := ADDR(buf),
                  cb_arg     := DINT(ADDR(buf)),
                  cb_size    := SIZEOF(buf));
  IF rc < 1 THEN
    DebugFmt(message := "canitpSessionCreate=\1", v1 := rc);
    RETURN;
  END_IF;
 
  // Build message
  str := "Hello from RTCU device";
 
  // Send message
  len := strLen(str := str);
  strToMemory(str := str, dst := ADDR(out), len := len);
  rc  := canitpSessionSend(handle:=handle, data := ADDR(out), size := len);
  IF rc < 1 THEN
    DebugFmt(message := "canitpSessionSend=\1", v1 := rc);
    RETURN;
  END_IF;
 
BEGIN
 
END;
END_PROGRAM;