A Go implementation of the M-Bus (Meter-Bus) protocol for remote reading of utility meters.
go-mbus is a library that provides functionality for communicating with devices using the M-Bus (Meter-Bus) protocol, which is a European standard (EN 13757-2, EN 13757-3) for remote reading of utility meters (water, gas, electricity, heat, etc.).
The library allows you to:
- Ping M-Bus devices to check if they are alive
- Read data from M-Bus devices
- Parse and interpret M-Bus telegrams
- Work with various M-Bus frame types (Short Frame, Long Frame, Control Frame)
go get github.com/pdat-cz/go-mbusThis library depends on the following packages:
github.com/tarm/serial- For serial port communicationtycho-edge/pkg/log- For logging (you'll need to provide this package or modify the code to use a different logging mechanism)gopkg.in/yaml.v3- For YAML parsing
package main
import (
"fmt"
"github.com/pdat-cz/go-mbus"
)
func main() {
// Ping a device at address 1 on port /dev/ttyUSB0
pingState := mbus.Ping("/dev/ttyUSB0", 1)
if pingState.State {
fmt.Printf("Device at address %d is alive\n", pingState.Address)
// Read data from the device
deviceState := mbus.Read("/dev/ttyUSB0", 1)
if deviceState.Error == "" {
fmt.Printf("Device data: %+v\n", deviceState.Data)
} else {
fmt.Printf("Error reading device: %s\n", deviceState.Error)
}
} else {
fmt.Printf("Device at address %d is not responding: %s\n",
pingState.Address, pingState.Error)
}
}package main
import (
"fmt"
"github.com/pdat-cz/go-mbus"
)
func main() {
// Scan for devices on port /dev/ttyUSB0, addresses 1-250
fmt.Println("Scanning for M-Bus devices...")
for address := 1; address <= 250; address++ {
pingState := mbus.Ping("/dev/ttyUSB0", address)
if pingState.State {
fmt.Printf("Found device at address %d\n", address)
}
}
}For more detailed information about the M-Bus protocol and how to use this library, see:
- Protocol Documentation - Details about the M-Bus protocol
- Examples - More usage examples
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- M-Bus Documentation - Official M-Bus protocol documentation
- EN 13757-2 - Physical and link layer
- EN 13757-3 - Application layer