Examples - Wireless M-Bus Receiver

Top  Previous  Next

//-----------------------------------------------------------------------------
// Wireless M-Bus receiver example
// This example listens for data from the device from the OMS specification Annex N.2.3.
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
// Uncomment math.inc to add math library support.
INCLUDE math.inc
 
//  Input variables that can be configured via the configuration dialog (These are global)
VAR_INPUT
 
END_VAR;
 
//  Output variables that can be configured via the configuration dialog (These are global)
VAR_OUTPUT
 
END_VAR;
 
//  These are the global variables of the program
VAR
  mb      : SYSHANDLE;
  clock   : clockLinsecToTime;
  recInfo : mbusRecordGetInfo;
END_VAR;
 
FUNCTION RegisterDevices;
VAR
  rc  : INT;
  key : ARRAY[1..16] OF USINT;
END_VAR;
 
  // wM-Bus Meter with integrated radio and Security profile B from OMS specification Annex N.2.3.
  key[1] := 16#00;
  key[2] := 16#01;
  key[3] := 16#02;
  key[4] := 16#03;
  key[5] := 16#04;
  key[6] := 16#05;
  key[7] := 16#06;
  key[8] := 16#07;
  key[9] := 16#08;
  key[10] := 16#09;
  key[11] := 16#0a;
  key[12] := 16#0b;
  key[13] := 16#0c;
  key[14] := 16#0d;
  key[15] := 16#0e;
  key[16] := 16#0f;
 
  rc := mbusSlaveRegister(handle:=mb, sec_addr:="1234567893153303", index:=1, key:=ADDR(key));
  DebugFmt(message:="mbusSlaveRegister(): \1", v1:=rc);
END_FUNCTION;
 
 
FUNCTION UnRegisterDevices;
VAR
  rc : INT;
  i  : SINT;
END_VAR;
  FOR i:=1 TO 64 DO
    rc := mbusSlaveUnRegister(handle:=mb, index:=i);
    DebugFmt(message:="mbusSlaveUnRegister(): \1", v1:=rc);
  END_FOR;
END_FUNCTION;
 
 
FUNCTION GetTime:STRING;
VAR_INPUT
  linsec : DINT;
END_VAR;
  clock(linsec:=linsec);
  GetTime:=strFormat(format:="\1:\2:\3", v1:=clock.hour, v2:=clock.minute, v3:=clock.second);
END_FUNCTION;
 
FUNCTION GetDate:STRING;
VAR_INPUT
  linsec : DINT;
END_VAR;
  clock(linsec:=linsec);
  GetDate:=strFormat(format:="\1-\2-\3", v1:=clock.year, v2:=clock.month, v3:=clock.day);
END_FUNCTION;
 
 
FUNCTION DumpRecords;
VAR
  rc, type  : INT;
  count, i  : INT;
  str, unit : STRING;
  scale     : FLOAT;  
  d         : DINT;
END_VAR;
    count := mbusRecordCount(handle:=mb);
    DebugFmt(message:=" Records: \1", v1:=count);
     
     
    FOR i := 0 TO count DO
        recInfo(handle:=mb, index:=i);
        IF recInfo.ready THEN
          str := "  Record "+intToStr(v:=i)+", Type: " + intToStr(v:=recInfo.type);
           
          scale:=1.0;
          unit:="";
           
          str := str + ", VIF: "+sintToHex(v:=recInfo.VIF)+", VIFE: "
               + sintToHex(v:=recInfo.VIFE[1])+" "+sintToHex(v:=recInfo.VIFE[2]);
          DebugFmt(message:=str);
          DebugFmt(message:="   Addr: "+recInfo.address);
          DebugFmt(message:="   Dev: \1, Func: \2, Tar \4, Stor: "+dintToStr(v:=recInfo.storage),
              v1:=recInfo.device, v2:=recInfo.func, v4:=recInfo.tariff);
           
          DebugFmt(message:="   Type: "+recInfo.text);
           
          type := mbusRecordGetType(handle:=mb, index:=i);
          DebugFmt(message:="   Data Type: \1", v1:=type);
           
          IF recInfo.VIF = 16#14 THEN
              // Volume [1e-2  m^3]
              unit := "m^3";
              scale := 0.01;
          END_IF;
           
           
          IF type = _MBUS_TYPE_INT32 THEN
              rc := mbusRecordGetInt(handle:=mb, index:=i, value:=d);
              DebugMsg(message:="   Value:     "+floatToStr(v:=FLOAT(d)*scale)+" "+unit);
          END_IF;
          IF type = _MBUS_TYPE_TIME THEN
              rc := mbusRecordGetLinsec(handle:=mb, index:=i, value:=d);
              IF recInfo.VIF = 16#6D THEN
                // time + date
                DebugMsg(message:="   Value:     "+GetDate(linsec:=d)+" "+GetTime(linsec:=d));
              ELSE
                // Just date
                DebugMsg(message:="   Value:     "+GetDate(linsec:=d));
              END_IF;
          END_IF;
 
        END_IF;
    END_FOR;
     
     
END_FUNCTION;
 
 
PROGRAM wmbus_rec;
// These are the local variables of the program block
VAR
  rc   : INT;
  info : mbusRecordSlaveInfo;
END_VAR;
// The next code will only be executed once after the program starts
 
  rc := mbusOpen(type:=2, handle:=mb, mode:=_MBUS_MODE_T1_C);
  DebugFmt(message:="mbusOpen(): \1", v1:=rc);
 
 
  // Enable this line to clear the registrations
  //UnRegisterDevices();
 
  // Create device registrations
  RegisterDevices();
 
 
  // Only receive from registered devices
  rc := mbusFilterEnable(handle:=mb, enable:=TRUE);
  DebugFmt(message:="mbusFilterEnable(): \1", v1:=rc);
 
BEGIN
// Code from this point until END will be executed repeatedly
 
  // Wait for 15 seconds for data from 1234567893153303
  rc := mbusDataReceive(handle:=mb, filter:="1234567893153303", timeout:=15);
  DebugFmt(message:="mbusDataReceive: \1", v1:=rc);
 
  IF rc = 1 THEN
    info(handle:=mb);  
    IF info.ready THEN
        DebugFmt(message:="Info for \4:", v4:=info.id);
        DebugFmt(message:=" Enc:  \1", v1:=info.enc_state);
        DebugFmt(message:=" Man:  "+info.manufacturer);
        DebugFmt(message:=" Ver:  \1", v1:=info.version);
        DebugFmt(message:=" Med:  \1", v1:=info.medium);
        DebugFmt(message:=" AN :  \1", v1:=info.accessnumber);
        DebugFmt(message:=" Sta:  \1", v1:=info.status);
        DebugFmt(message:=" Addr: "+info.sec_addr);
        DebugFmt(message:=" Sig:  \1", v1:=info.signal);
    END_IF;
    DumpRecords();
  END_IF;
 
END;
 
END_PROGRAM;