SysCANRxMsg, receives a CAN message

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

In ricezione il controller CAN riceve solo i messaggi che passano dalla impostazione del filtro (Funzione SysCANSetMode) e ne esegue il caricamento in un buffer, nei sistemi Cortex M7 il buffer può contenere 64 messaggi. Nel buffer i messaggi sono inseriti secondo il loro ordine di ricezione, ma con questa funzione è possibile estrarre dal buffer un messaggio che soddisfi la condizione di filtro definita.

Mask determina quali bit del CanID messaggio debbano essere confrontati con il bit di ID. Se un bit di Mask è FALSE il corrispondente bit di CanID verrà accettato indipendentemente dal valore del bit di ID. Se un bit di Mask è TRUE, il corrispondente bit di CanID verrà confrontato con il valore del bit di ID; se corrispondono, viene accettato, altrimenti viene rifiutato.

La funzione ricerca nel buffer dei messaggi ricevuti un messaggio che soddisfi la condizione di filtro indicata, se trovato lo trasferisce in un buffer di tipo SYSCANMESSAGE il cui indirizzo deve essere indicato nella variabile Msg e ritorna TRUE, il messaggio ritornato viene eliminato dal buffer.

Information Circle

Funzione

CODESYS: Non disponibile

LogicLab: eLLabXUnified12Lib

Descrizione

Mask (UDINT) Valore di maschera filtro pacchetti CAN.
ID (UDINT) Valore di filtro pacchetti CAN.
Msg (@SYSCANMESSAGE)) Indirizzo variabile di tipo SYSCANMESSAGE in cui trasferire il messaggio ricevuto.

La funzione ritorna (BOOL) TRUE se messaggio trovato e trasferito nel buffer.

Immagine Fct SysCANRxMsg

Esempi

Come utilizzare gli esempi.
Nell’esempio sono ricevuti tutti i messaggi CAN sia a 11 che a 29 bit. Sulla ricezione del messaggio ne viene eseguito il report nella console di spionaggio.

LogicLab (Ptp116, ST_SysCANRxMsg)
PROGRAM ST_SysCANRxMsg
VAR
    i : UDINT; (* Auxiliary counter *)
    j : USINT; (* Auxiliary counter *)
    MIDx : USINT; (* Messages index *)
    SBf : STRING[ 64 ]; (* Spy buffer *)
    CANMsg : SYSCANMESSAGE; (* CAN message *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysCANRxMsg"
// *****************************************************************************
// All messages are accepted the SysSpyData function allows to enable the Spy
// console to display the received messages.
//
// A Spy console report of some received messages.
// 
// 08:37:28(2.00)|Rx|[11 Bits] MsgID:00000456, Length:2 - 12 34
// 08:37:30(2.00)|Rx|[29 Bits] MsgID:88123456, Length:8 - 01 23 45 67 89 AB CD EF 
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Mode examples, use the one which meets your requirements.

    IF (SysFirstLoop) THEN
        i:=SysCANSetMode(CAN_SPEED#CAN_125KBIT, 16#000007FF, 16#00000000); //Accept only 11 bits messages
        i:=SysCANSetMode(CAN_SPEED#CAN_250KBIT, 16#80000000, 16#00000000); //Accept only 29 bits messages
        i:=SysCANSetMode(CAN_SPEED#CAN_500KBIT, 16#00000000, 16#00000000); //Accept all 11 and 29 bits messages
    END_IF;

    // -------------------------------------------------------------------------
    // MESSAGE RECEPTION
    // -------------------------------------------------------------------------
    // Check if a messages has been received, can be received only the messages
    // that are accepted by the filter set. 

    IF NOT(SysIsCANRxTxAv(FALSE)) THEN RETURN; END_IF;

    // -------------------------------------------------------------------------
    // 11 AND 29 BITS MESSAGES REPORT
    // -------------------------------------------------------------------------
    // Create report on spy console of all the 11 and 29 bits received messages.

    FOR MIDx:=0 TO 63 DO

        // Receive 11 or 29 bits mesage.

        IF NOT(SysCANRxMsg(16#00000000, 16#00000000, ADR(CANMsg))) THEN
            IF NOT(SysCANRxMsg(16#00000000, 16#80000000, ADR(CANMsg))) THEN EXIT; END_IF;
        END_IF;

        // Message report.

        IF NOT(CANMsg.MsgID.31) THEN      
            i:=SysVsnprintf(ADR(SBf), SIZEOF(SBf), ADR('[11 Bits] MsgID:%08X'), UDINT_TYPE, ADR(CANMsg.MsgID));
        ELSE
            i:=SysVsnprintf(ADR(SBf), SIZEOF(SBf), ADR('[29 Bits] MsgID:%08X'), UDINT_TYPE, ADR(CANMsg.MsgID));
        END_IF;

        i:=SysCVsnprintf(ADR(SBf), SIZEOF(SBf), ADR(', Length:%d -'), USINT_TYPE, ADR(CANMsg.Length));

        FOR j:=0 TO CANMsg.Length-1 DO
            i:=SysCVsnprintf(ADR(SBf), SIZEOF(SBf), ADR(' %02X'), USINT_TYPE, ADR(CANMsg.Data[j]));
        END_FOR;

        i:=SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Rx'), ADR(SBf));
    END_FOR;

// [End of file]
Was this article helpful?