M-Bus (Meter-bus)

M-Bus (Meter-bus) library #

Load the library:

require('mbus')

Library functions #

init(port, baud [, duplex]) #

Sets serial port and baud rate to use for all other mbus functions.

Must be called before any other mbus functions.

Parameters:

  • port - serial port to use (example: "/dev/RS232" or "/dev/ttyUSB0")
  • baud - serial baud rate (example: 2400)
  • duplex - serial port duplex mode, set to "half" for RS-485 (default is "full")

scan([min [, max [, retries]]]) #

Scans given primary address range. Returns table with found devices.

Parameters:

  • min - min scan address, defaults to 0
  • max - max scan address, defaults to 250
  • retries - number of extra ping retries for each device, defaults to 0

scansecondary([mask]) #

Scans secondary address range using the given address mask. Returns table with found devices.

Parameters:

  • mask - secondary address mask, defaults to "FFFFFFFFFFFFFFFF"

getdata(addr [, frames]) #

Gets all data from the device with the given primary or secondary address. Returns table on success, nil plus error message on failure.

Parameters:

  • addr - primary (integer) or secondary (hex string, 16 characters) device address
  • frames - maximum number of frames that the device can reply with, defaults to 1

writeaddr(old, new) #

Sets a new primary address. Returns true on success, nil plus error message on failure.

Parameters:

  • old - old (current) primary address
  • new - new primary address

writevif4(addr, vif, value) #

Changes VIF to the given 4-byte integer value for the given primary address. Returns true on success, nil plus error message on failure.

Parameters:

  • addr - primary device address
  • vif - VIF field id (example: 0x13 for counter value)
  • value - 4-byte value

send(...) #

Sends arbitrary data to the port. Header (0x68 LEN LEN 0x68) and checksum + ending byte (0x16) are added automatically. Returns true on success, nil plus error message on failure.

Parameters:

  • ... - data bytes, each argument is a number in 0..255 range

lasterror() #

Returns a string containing the last error that occurred during some mbus function calls.