Write (FUN)ΒΆ
FUNCTION Write : ERROR
This function writes a CAN or CAN FD message (see also CreateMessage and CreateFdMessage) via a CAN or CAN FD 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
:
In case ofCL2.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 settinghMessage
toCAA.gc_hINVALID
directly after callingCL2.Write
In case ofCL2.Write
returns any error, the message still belongs to the application.Do not forget to release the message!
Example
In this example a classic CAN driver will be opened and a classic CAN message (CANopen NMT reset message) will be sent.
VAR
xInitialized : BOOL;
hDriver : CAA.HANDLE;
hMsg : CAA.HANDLE;
pData : POINTER TO BYTE;
eError : CL2.ERROR;
END_VAR
IF NOT xInitialized THEN
hDriver := CL2.DriverOpenH(usiNetId := 0,
uiBaudrate := 1000,
xSupport29Bits := FALSE,
ctMessages := 100,
peError := ADR(eError)
);
IF hDriver <> CAA.gc_hINVALID THEN
xInitialized := TRUE;
ELSE
RETURN;
END_IF
END_IF
//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.GetMessageDataPointer2(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.FreeMessage(hMsg);
END_IF
END_IF
Example
In this example a CAN FD driver will be opened. The program sends a CAN FD and a classic CAN messge over the CAN FD interface.
VAR
xInitialized : BOOL;
hDriver : CAA.HANDLE;
hMsg : CAA.HANDLE;
pData : POINTER TO BYTE;
eError : CL2.ERROR;
usiIndex : USINT;
END_VAR
IF NOT xInitialized THEN
hDriver := CL2.FdDriverOpenH(usiNetId := 0,
uiNominalBaudrate := 500,
uiDataBaudrate := 4000,
xSupport29Bits := FALSE,
ctMessages := 100,
peError := ADR(eError)
);
IF hDriver <> CAA.gc_hINVALID THEN
xInitialized := TRUE;
ELSE
RETURN;
END_IF
END_IF
//Example 1: Send a CAN FD message
hMsg := CL2.CreateFdMessage(hFdDriver := hDriver,
cobID := 16#123,
usiLength := 64,
x29BitID := FALSE,
xBitrateSwitching := TRUE,
peError := ADR(eError)
);
IF hMsg <> CAA.gc_hINVALID THEN
//Get message data pointer
pData := CL2.GetMessageDataPointer2(hMessage := hMsg, peError := ADR(eError));
IF pData <> CAA.gc_pNULL THEN
//initialize message data
FOR usiIndex := 0 TO 63 DO
pData[usiIndex] := usiIndex;
END_FOR
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.FreeMessage(hMsg);
END_IF
END_IF
//Example 2: Send a classic CAN 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.GetMessageDataPointer2(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.FreeMessage(hMsg);
END_IF
END_IF
- InOut:
Scope
Name
Type
Comment
Return
Write
error
Input
hDriver
CAA.HANDLE
handle of CAN or CAN FD interface
hMessage
CAA.HANDLE
handle of classic CAN or CAN FD message to be written
usiPriority
USINT
priority of message:0: send immediately;1-n: priority (1: high, n: low), default is n = 8xEnableSyncWindow
BOOL
use SYNC window => not implemented; do not care