Examples - Wireless M-Bus sender

Top  Previous  Next

//-----------------------------------------------------------------------------
// Wireless M-Bus sender example
// This example sends the data from the OMS specification Annex N.2.3.
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
// Uncomment math.inc to add math library support.
//INCLUDE math.inc
 
//  These are the global variables of the program
VAR
  mb  : SYSHANDLE;
END_VAR;
 
// Convert hex string into SINT array
FUNCTION ParseString:INT;
VAR_INPUT
  str : STRING;
  dst : PTR;
END_VAR;
VAR
  i   : INT;
  pos : INT;
  arr : ARRAY[0..300] OF SINT;
END_VAR;
  i:=1;
  pos := 0;
  WHILE i < strLen(str := str) DO
    IF strMid(str := str, start := i, length := 1) <> " " THEN
        arr[pos]:=hexToSint(hex:=strMid(str:=str, start:=i, length:=2));
        i := i + 2;
        pos := pos + 1;
    ELSE
        i := i + 1;
    END_IF;
  END_WHILE;
  memcpy(dst:=dst, src:=ADDR(arr), len:=pos);
  ParseString:=pos;
END_FUNCTION;
 
// Send packet from OMS Annex N.2.3: gas meter with internal radio, security profile B
FUNCTION SendN23;
VAR
  rc       : INT;
  len      : INT;
  tx_frame : ARRAY [1..300] OF USINT;
END_VAR;
  // Hex string with data from example N.2.3:
  len := ParseString(dst := ADDR(tx_frame), str:=
    // C  
     "44"+
    // ELL
     "8c2075"+
    // AFL                                
     "900f002c25b30a000021924d4f2fb66e01"+
    // TPL
     "7a7500200710"+
    // Encrypted TPL/APL
     " 9058475f4bc91df878b80a1b0f98b629 "+
    // Encrypted APL
     "024aac727942bfc549233c0140829b93"
  );
 
  // Set sender address to address from DLL
  rc := mbusSetSenderAddress(handle:=mb, sec_addr:="1234567893153303");
  DebugFmt(message:="mbusSetSenderAddress(): \1", v1:=rc);
 
  rc := mbusSend(handle:=mb, frame := ADDR(tx_frame), size := len);
  DebugFmt(message:="mbusSend(): \1", v1:=rc);
 
END_FUNCTION;
 
 
PROGRAM sender;
// These are the local variables of the program block
VAR
  rc : INT;
END_VAR;
// The next code will only be executed once after the program starts
  rc := mbusOpen(type:=2, handle:=mb);
  DebugFmt(message:="mbusOpen(): \1", v1:=rc);
 
BEGIN
// Code from this point until END will be executed repeatedly
 
  SendN23();
 
  Sleep(delay:=10000);
 
END;
END_PROGRAM;