This function can be used to extract the FMS/J1939 PGN from a CAN message received with canReceiveMessage.
To create a CAN filter from a PGN, please see canFMSFilterCreate.
Input:
Id : DINT (16#0...16#FFFFFFFF) (Default 0)
The CAN message ID to extract PGN from.
Returns: DINT
Declaration:
FUNCTION canFMSGetPGN : DINT;
VAR_INPUT
Id : DINT := 0;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
can : canReceiveMessage;
buf : ARRAY[1..8] OF SINT;
END_VAR;
FUNCTION_BLOCK ParseCruiseControl;
VAR_INPUT
data : PTR;
END_VAR;
VAR_OUTPUT
speed : DINT;
clutch : BOOL;
break : BOOL;
cruise : BOOL;
val : SINT;
END_VAR;
VAR
can : ARRAY [1..8] OF SINT;
temp : SINT;
high : DINT;
END_VAR;
memcpy(dst := ADDR(can), src := data, len := 8);
speed := shl32(in := can[2], n := 8) + can[3];
IF shr8(in := can[4], n := 6) = 1 THEN
clutch := TRUE;
ELSE
clutch := FALSE;
END_IF;
temp := shr8(in := can[4], n := 4) AND 16#03;
IF temp = 1 THEN
break := TRUE;
ELSE
break := FALSE;
END_IF;
IF (can[4] AND 16#03) = 1 THEN
cruise := TRUE;
ELSE
cruise := FALSE;
END_IF;
val := can[7] AND 16#1F;
END_FUNCTION_BLOCK;
FUNCTION ParseEngineSpeed : DINT;
VAR_INPUT
data : PTR;
END_VAR;
VAR
can : ARRAY [1..8] OF SINT;
tick : DINT;
END_VAR;
memcpy(dst := ADDR(can), src := data, len := 8);
tick := shl32(in := can[4], n := 8) + can[5];
ParseEngineSpeed := tick * 125/1000;
END_FUNCTION;
PROGRAM example;
VAR
PGN : DINT;
CCVS : ParseCruiseControl;
END_VAR;
canOpen(baud := 250);
can.data := ADDR(buf);
canFMSFilterCreate(PGN := 65265);
canFMSFilterCreate(PGN := 61444);
BEGIN
can();
IF can.ready THEN
PGN := canFMSGetPGN(id := can.id);
IF PGN = 65265 THEN
CCVS(data := ADDR(buf));
DebugFmt(message := "speed= \4", v4 := CCVS.speed);
DebugFmt(message := "clutch= \1", v1 := INT(CCVS.clutch));
DebugFmt(message := "break= \1", v1 := INT(CCVS.break));
DebugFmt(message := "cruise= \1", v1 := INT(CCVS.cruise));
DebugFmt(message := "unknown= \1", v1 := CCVS.val);
ELSIF PGN = 61444 THEN
DebugFmt(message := "rpm= \4", v4 := ParseEngineSpeed(data := ADDR(buf)));
END_IF;
END_IF;
END;
END_PROGRAM;
|