ADVANCED: mbusSend (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

LX4,NX-400

Firmware version:

1.94.00


This function is used to send raw M-Bus frames.

To send raw wired M-Bus frames to a slave that can not use the normal primary address, mbusSelectSecondary can be used to select the address to make it available as primary address 253.

 

When sending wireless M-Bus frames, the frame data must start with the Control field followed by the CI field and the rest of the data, skipping large parts of the header.

To change the address used when sending wireless M-Bus frames, use mbusSetSenderAddress.

 

 

Input:

handle : SYSHANDLE

A handle to the connection

 

frame : PTR

The frame to send

 

size : INT

The size of the frame in bytes.

 

 

Returns: INT

1

- Success

0

- Not supported

-1

- Invalid handle

-9

- Communication error

-13

- Invalid frame.

 

Declaration:

FUNCTION mbusSend : INT;
VAR_INPUT
  handle   : SYSHANDLE;
  frame   : PTR;
  size     : INT;
END_VAR;

 

Example:

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;