DriverGetSize (FUN) ¶ FUNCTION DriverGetSize : CAA.SIZE Calculates the needed memory size for DriverOpenP . InOut: Scope Name Type Comment Return DriverGetSize CAA.SIZE Required memory size for DriverOpenP Input usiNetId USINT number of CAN interface [0..n] xSupport29Bit BOOL FALSE : only 11-Bit IDs, TRUE : support also 29-Bit ctMessages CAA.COUNT number of transmit messages which should be allocated peError POINTER TO ERROR optional pointer to error enum
DriverOpenH (FUN) ¶ FUNCTION DriverOpenH : CAA.HANDLE Opens a CAN interface and allocates memory from heap. Not every hardware supports each baud rate. If a baud rate is not supported or if the interface cannot be opened due to different reasons, the function returns CAA.gc_hINVALID . The function can be called several times on the same interface, so that multiple parts of a program can work on it. You can set uiBaudrate to 0 if the interface is already opened or if the default baudrate from configuration file should be used. See following section in configuration file: [CmpCAACanL2] ;If setting PersistentBaudrate is enabled (0: disabled; 1: enabled) ;the baudrate will be stored into Net.X.DefaultBaudrate (where X equals NetId) when opening the driver. PersistentBaudrate=1 ;Default baudrate for CAN network 0. This baudrate will be used when the network is opened (e.g from CmpBlkDrvCan or CANopen) with the baudrate set to 0. ;Net.0.DefaultBaudrate=1000 Net.0.DefaultBaudrate=1000 InOut: Scope Name Type Comment Return DriverOpenH CAA.HANDLE handle of CAN interface or CAA.gc_hINVALID if failed. Input usiNetId USINT number of CAN interface [0..n] uiBaudrate UINT Baudrate in kBit/s e.g. 1000 for 1 Mbit xSupport29Bits BOOL FALSE : only 11-Bit IDs, TRUE : support also 29-Bit ctMessages CAA.COUNT number of transmit messages which should be allocated peError POINTER TO ERROR optional pointer to error enum
DriverOpenP (FUN) ¶ FUNCTION DriverOpenP : CAA.HANDLE See DriverOpenH for details. In contrast to DriverOpenH no dynamic memory will be allocated. Memory specified by pMemory and szMemSize is used. Use DriverGetSize to calculate the required memory size. Please note that required memory size can change with new runtime version. Note pMemory should be aligned to a 4 byte address on 32bit systems and 8 byte address on 64bit. To get sure, you can declare the memory with ARRAY [0..n] of __XWORD. Please keep also in mind that required memory size is different for 32 and 64 bit systems. InOut: Scope Name Type Comment Return DriverOpenP CAA.HANDLE handle of CAN interface or CAA.gc_hINVALID if failed. Input usiNetId USINT number of CAN interface [0..n] uiBaudrate UINT Baudrate in kBit/s e.g. 1000 for 1 Mbit xSupport29Bits BOOL FALSE : only 11-Bit IDs, TRUE : support also 29-Bit szMemSize CAA.SIZE size of allocated memory pMemory CAA.PVOID pointer to aligned memory peError POINTER TO ERROR optional pointer to error enum
FreeMessage (FUN) ¶ FUNCTION FreeMessage : ERROR This function releases a message that has been created with CreateMessage , read with Read or duplicated with CloneMessage . Note Do not free a message after it has been passed to the CL2 layer. Example: Creating a message, then passing the message handle to CL2 by calling Write and releasing it afterwards may result in unexpected behaviour. InOut: Scope Name Type Comment Return FreeMessage ERROR ERROR.NO_ERROR or appropriate error code Input hMessage CAA.HANDLE handle of message to be freed
Read (FUN) ¶ FUNCTION Read : CAA.HANDLE Receives messages from a receiver. Note To avoid losing receive messages an application has to read all messages of all receivers each cycle. All messages should be processed and released by FreeMessage afterwards. Note Normally a receiver returns CAA.gc_hINVALID if all messages are read by application. But receivers with xAlwaysNewest = TRUE return always the newest message even if it was already received. The application has to free the message each time it is returned by CL2.Read!!! In case of an always newest receiver the data pctMsgLeft points to should be interpreted as follows: 0 : old message; 1 : new message; 2 : message overflow Example VAR hDriver : CAA.HANDLE ; hReceiver : CAA.HANDLE ; hMsg : CAA.HANDLE ; ctMsgLeft : CAA.COUNT ; eError : CL2.ERROR ; END_VAR //Create a MaskReceiver which receives all messages with CAN ID 16#80. hReceiver := CL2.CreateMaskReceiver ( hDriver := hDriver , cobIdValue := 16#80 , //cobID value cobIdMask := 16#FFFFFFFF , //cobID mask ==> all bits of value are relevant xRTRValue := FALSE , //no RTR messages xRTRMask := TRUE , //activate RTR filter ==> xRTRValue will be checked x29BitIdValue := FALSE , //no 29 bit CAN messages x29BitIdMask := TRUE , //activate 29 bit filter ==> x29BitIdValue will be checked xTransmitValue := FALSE , //only receive messages, no transmit message loopback xTransmitMask := TRUE , //activate transmit mask filter ==> xTransmitValue will be checked xAlwaysNewest := FALSE , //FALSE := receiver with queue; TRUE: only newest message eEvent := CB.EVENT.NO_EVENT , //no receive event xEnableSyncWindow := FALSE , //not implemented peError := ADR ( eError ) //optional pointer to error ); REPEAT //receive a message from hReceiver hMsg := CL2.Read ( hReceiverId := hReceiver , pctMsgLeft := ADR ( ctMsgLeft ), peError := ADR ( eError )); IF hMsg <> CAA.gc_hINVALID THEN //TODO: Process message (CL2.GetMessageDataPointer, CL2.GetMessageId, ...) CL2.FreeMessage ( hMsg ); //release message hMsg := CAA.gc_hINVALID ; //to avoid using an already released message END_IF UNTIL ctMsgLeft = 0 END_REPEAT InOut: Scope Name Type Comment Return Read CAA.HANDLE handle of received CAN message or CAA.gc_hINVALID if receiver is empty. Input hReceiverId CAA.HANDLE receiver handle pctMsgLeft POINTER TO CAA.COUNT Optional pointer to ctMsgLeft . After calling CL2.Read ctMsgLeft contains the number of remaining messages in receive queue. peError POINTER TO ERROR optional pointer to error enum
RegisterIdArea (FUN) ¶ FUNCTION RegisterIdArea : ERROR This function registers a CAN identifier area to an IdAreaReceiver created by CreateIdAreaReceiver . If the parameter cobIdStart equals cobIdEnd only one identifier is registered. The values of the masks activate the evaluation of the value parameters. If the mask value is FALSE the value parameter is ignored. Receivers can be also used for Tx loopback. If xTransmitMask is set to FALSE or xTransmitMask and xTransmitValue are set to TRUE CAN messages which are sent via Write will be received (applies to all transmit messages on the CAN interface). Use function IsTransmitMessage to distinguish between receive and transmit messages. Note Current implementation of this receiver type does not support 29 bit identifiers. Set x29BitIdValue always to FALSE and x29BitIdMask to TRUE . Otherwise an error will be returned. Example See CreateIdAreaReceiver . InOut: Scope Name Type Comment Return RegisterIdArea ERROR ERROR.NO_ERROR if area was registered successfully, else appropriate error code Input hReceiverId CAA.HANDLE Retrun value of CreateIdAreaReceiver cobIdStart CL2I.COBID start id of message to be received cobIdEnd CL2I.COBID end id of message to be received xRTRValue BOOL Value RTR bit; only evaluated if xRTRMask is set to TRUE xRTRMask BOOL Mask RTR bit x29BitIdValue BOOL Value 29 Bit Id; only evaluated if x29BitIdMask is set to TRUE x29BitIdMask BOOL Mask 29 Bit Id xTransmitValue BOOL Value Transmit message; only evaluated if xTransmitMask is set to TRUE xTransmitMask BOOL Mask Transmit message
UnregisterIdArea (FUN) ¶ FUNCTION UnregisterIdArea : ERROR This function removes a CAN identifier area previously added by RegisterIdArea . InOut: Scope Name Type Comment Return UnregisterIdArea ERROR error Input hReceiverId CAA.HANDLE Retrun value of CreateIdAreaReceiver cobIdStart CL2I.COBID start id of message to be received cobIdEnd CL2I.COBID end id of message to be received xRTRValue BOOL Value RTR bit; only evaluated if xRTRMask is set to TRUE xRTRMask BOOL Mask RTR bit x29BitIdValue BOOL Value 29 Bit Id; only evaluated if x29BitIdMask is set to TRUE x29BitIdMask BOOL Mask 29 Bit Id xTransmitValue BOOL Value Transmit message; only evaluated if xTransmitMask is set to TRUE xTransmitMask BOOL Mask Transmit message
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 : 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 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
Diagnostic Information ¶ Functions for getting diagnostic information about a given CAN Network. GetBaudrate (Function) GetBusAlarm (Function) GetBusState (Function) GetBusload (Function) GetDiagnosis (Function) GetLostCounter (Function) GetReceiveCounter (Function) GetReceiveErrorCounter (Function) GetReceivePoolSize (Function) GetReceiveQueueLength (Function) GetTransmitCounter (Function) GetTransmitErrorCounter (Function) GetTransmitPoolSize (Function) GetTransmitQueueLength (Function) IsSendingActive (Function) ResetBusAlarm (Function)
GetBaudrate (FUN) ¶ FUNCTION GetBaudrate : UINT This function returns the current baudrate of the bus. Information can be also read by GetDiagnosis . InOut: Scope Name Type Comment Return GetBaudrate UINT baudrate in kBit/s Input hDriver CAA.HANDLE handle of CAN interface peError POINTER TO ERROR optional pointer to error enum