ADVANCED: mbusReceive (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.94.00


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

For wired M-Bus, the frame must be requested using mbusDataRequest or mbusSend, and this function will block for a moment while it tries to read the response.

Wireless M-Bus devices may automatically send the frames and the timeout parameter can be used to determine how long this function should wait for a response.

The filter parameter can be used to only receive frames from a specific wireless M-Bus device.

 

 

Input:

handle : SYSHANDLE

A handle to the connection

 

frame : PTR

The buffer to store the frame in.

 

maxsize : INT

The size of the buffer in bytes.

 

filter : STRING

For Wireless M-Bus only: Specifies the address or address filter to receive frames from.

 

timeout : INT Default 0

Number of seconds to wait for a Wireless M-Bus frame. Use 0 seconds to return immediately if there is no data in the buffer.

 

Output:

size : INT

The number of bytes received.

 

 

Returns: INT

1

- Success

0

- Not supported

-1

- Invalid handle

-2

- Frame is too small.

-7

- Invalid filter.

-9

- Communication error

-10

- Timeout before data was received.

-14

- Invalid timeout value.

 

Declaration:

FUNCTION mbusReceive : INT;
VAR_INPUT
   handle   : SYSHANDLE;
   frame    : PTR;
   maxsize  : INT;
   filter   : STRING;
   timeout  : INT:=0;
   size     : ACCESS INT;
END_VAR;

 

Example:

// Dump all received frames from ELS meters, to e.g. help with debugging.
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;
 
PROGRAM wmbus_raw_rec;
// These are the local variables of the program block
VAR
   rc       : INT;
   len      : INT;
   rx_frame : ARRAY [1..300OF USINT;
   str      : STRING;
   i        : INT;
END_VAR;
// The next code will only be executed once after the program starts
 
   rc := mbusOpen(type:=2handle:=mbmode:=_MBUS_MODE_T1_C);
   DebugFmt(message:="mbusOpen(): \1", v1:=rc);
   
   // Receive from all devices
   rc := mbusFilterEnable(handle:=mbenable:=FALSE);
   DebugFmt(message:="mbusFilterEnable(): \1", v1:=rc);
 
BEGIN
   // Code from this point until END will be executed repeatedly
 
   // Filter will only receive from ELS meters (16#1593)
   rc := mbusReceive(handle:=mbfilter:="FFFFFFFF9315FFFF", frame := ADDR(rx_frame), maxsize:=300size := lentimeout:=30);
   DebugFmt(message:="mbusReceive(): \1, len=\2", v1:=rcv2:=len);
   IF rc = 1 THEN
      str :="";
      FOR i := 1 TO len DO
         str := str + sintToHex(v:=rx_frame[i])+" ";
         // Break up string after 32 bytes
         IF i MOD 32 = 0 THEN
            DebugMsg(message:=str);
            str := "";
         END_IF;
      END_FOR;
      DebugMsg(message:=str);
   END_IF;
   Sleep(delay:=1000);
END;
 
END_PROGRAM;