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
GetBusAlarm (FUN) ¶ FUNCTION GetBusAlarm : BOOL This function returns if the CAN chip is in Bus Alarm state. In this state sending and receiving messages is not possible. A Bus Alarm can be reset by ResetBusAlarm which leads to a re-initialization of the CAN chip. Diagnostic Information can be also read by GetDiagnosis . InOut: Scope Name Type Comment Return GetBusAlarm BOOL TRUE if a bus alarm is pending. Input hDriver CAA.HANDLE handle of CAN interface peError POINTER TO ERROR optional pointer to error enum
GetBusState (FUN) ¶ FUNCTION GetBusState : BUSSTATE This function return the current state of the bus. Diagnostic Information can be also read by GetDiagnosis . InOut: Scope Name Type Comment Return GetBusState BUSSTATE current bus state Input hDriver CAA.HANDLE handle of CAN interface peError POINTER TO ERROR optional pointer to error enum
GetBusload (FUN) ¶ FUNCTION GetBusload : USINT This function is not implemented by all drivers. It returns the current busload in percent. Diagnostic Information can be also read by GetDiagnosis . InOut: Scope Name Type Comment Return GetBusload USINT bus load (0..100) in percent Input hDriver CAA.HANDLE handle of CAN interface peError POINTER TO ERROR optional pointer to error enum
GetDiagnosis (FUN) ¶ FUNCTION GetDiagnosis : ERROR Function returns all relevant diagnostic information for a specific driver. InOut: Scope Name Type Comment Return GetDiagnosis ERROR ERROR.NO_ERROR or appropriate error code Input hDriver CAA.HANDLE handle of CAN interface pDiagnosisInfo POINTER TO DIAGNOSIS_INFO Pointer to diagnostic information