Write (FUN)

FUNCTION Write : ERROR

This function writes a CAN message (see also CreateMessage) via a CAN interface specified by hDriver. If Write returns ERROR.NO_ERROR the message was successfully handed over to the lower layers and will be automatically released after successful sending (Note: Do not call FreeMessage on such handles anymore!). If the message cannot be sent the function returns the error code ERROR.SENDING_ERROR and sending process must be repeated later. Otherwise the message handle has to be manually freed by application (see FreeMessage).

Messages can be sent with different priorities. Messages with the same priority are sent in chronological order. Messages with higher priority (means lower usiPriority) are always sent prior to those with lower priority. Priority 0 has a special meaning: messages with priority 0 are sent as soon as possible prior to all other messages waiting for transport. If a priority level not valid for the driver is used the message is not sent and function returns an error code. If the applied coblD is registered for reception and transmit messages are activated for receiving (see TransmitMask and TransmitValue of Receiver), a message which was sent successfully can be received via Read with a current timestamp (if timestamps are supported by driver). The function IsTransmitMessage returns TRUE on such messages.

Note

Following rules are important when using CL2.Write:

  1. In case of CL2.Write returns ERROR.NO_ERROR the message was successfully handed over to the CL2 layer.

    Do not use the message handle anymore! It will be automatically released.

    To avoid using such handles it is helpful setting hMessage to CAA.gc_hINVALID directly after calling CL2.Write

  2. In case of CL2.Write returns any Error, the message still belongs to the application.

    Do not forget to release the message!

Example

In this example a CANopen NMT reset message will be sent.

VAR
  hDriver : CAA.HANDLE;
  hReceiver : CAA.HANDLE;
  hMsg : CAA.HANDLE;
  pData : POINTER TO CL2I.DATA;
  eError : CL2.ERROR;
END_VAR

//Create message
hMsg := CL2.CreateMessage(hDriver := hDriver,
                          cobID := 16#0,
                          usiLength := 2,
                          xRTR := FALSE,
                          x29BitID := FALSE,
                          peError := ADR(eError)
);

IF hMsg <> CAA.gc_hINVALID THEN
    //Get message data pointer
    pData := CL2.GetMessageDataPointer(hMessage := hMsg, peError := ADR(eError));
    IF pData <> CAA.gc_pNULL THEN
        //initialize message data
        pData^[0] := 16#81;
        pData^[1] := 16#0;
    END_IF

    //send message
    eError := CL2.Write(hDriver := hDriver,
                        hMessage := hMsg,
                        usiPriority := 1, //highest priority
                        xEnableSyncWindow := FALSE
    );

    IF eError <> CL2.ERROR.NO_ERROR THEN
        //sending was not successful ==> release the message
        CL2.Free_Message(hMsg);
    END_IF
END_IF
InOut:

Scope

Name

Type

Comment

Return

Write

ERROR

error

Input

hDriver

CAA.HANDLE

handle of CAN interface

hMessage

CAA.HANDLE

handle of message to be written

usiPriority

USINT

priority of message:

0: send immediately;

1-n: priority (1: high, n: low), default is n = 8

xEnableSyncWindow

BOOL

use SYNC window => not implemented; do not care