Documentation
¶
Overview ¶
Package sml decodes Smart Message Language (SML) data from German smart electricity meters.
SML is a binary TLV-encoded protocol defined by the German BSI. It is used by virtually all modern German smart meters to transmit readings over an infrared optical interface. This package provides a pure-Go parser with zero external dependencies.
Decoding a single payload ¶
If you already have a complete SML payload (transport layer stripped), use Decode or DecodeWithOptions:
file, err := sml.Decode(payload)
for _, entry := range file.Readings() {
if v, ok := entry.ScaledValue(); ok {
fmt.Printf("%s = %f %s\n", entry.OBISString(), v, entry.UnitString())
}
}
Streaming from an io.Reader ¶
For continuous reading from a serial port or other byte stream, use Listen or ListenMessages. These handle SML transport framing (start/end sequences, escape processing, CRC validation) automatically via the transport sub-package:
err := sml.Listen(ctx, serialPort, func(file *sml.File) error {
for _, entry := range file.Readings() {
// process readings
}
return nil
})
Value types ¶
SML values preserve their wire encoding width. The Value interface is satisfied by named types (OctetString, Bool, Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64) which can be distinguished via a type switch. For meter readings, the ListEntry.ScaledValue convenience method applies the scaler and returns a float64.
Index ¶
- Constants
- func FormatReadingValue(e *ListEntry) (string, bool)
- func Listen(ctx context.Context, r io.Reader, handler Handler) error
- func ListenMessages(ctx context.Context, r io.Reader, handler MessageHandler) error
- func ToInt64(v Value) (int64, bool)
- func UnitName(code uint8) string
- type AttentionResponse
- type Bool
- type CloseResponse
- type DecodeOptions
- type File
- type GetListResponse
- type GetProcParameterResponse
- type GetProfileListResponse
- type GetProfilePackResponse
- type Handler
- type Int8
- type Int16
- type Int32
- type Int64
- type ListEntry
- type Message
- type MessageBody
- type MessageHandler
- type OctetString
- type OpenResponse
- type PeriodEntry
- type ProfileObjHeaderEntry
- type ProfileObjPeriodEntry
- type Time
- type TreeEntry
- type TreePath
- type Uint8
- type Uint16
- type Uint32
- type Uint64
- type Value
Constants ¶
const ( TimeSecIndex uint8 = 0x01 // TimeSecIndex indicates the value is a seconds index (secIndex). TimeTimestamp uint8 = 0x02 // TimeTimestamp indicates the value is a UNIX timestamp. )
Variables ¶
This section is empty.
Functions ¶
func FormatReadingValue ¶
FormatReadingValue formats a ListEntry as "OBIS#value#unit", matching the output format of the C sml_server reference tool. Returns ("", false) if the entry has a nil Value or an OBIS code that isn't exactly 6 bytes.
func Listen ¶
Listen reads SML frames from r, decodes them, and calls handler for each file. Blocks until r is exhausted (io.EOF), ctx is cancelled, or handler returns a non-nil error. Frames that fail CRC validation or SML decoding are silently skipped.
func ListenMessages ¶
ListenMessages calls handler once per message rather than once per file. Frames that fail CRC validation or SML decoding are silently skipped.
Types ¶
type AttentionResponse ¶
type AttentionResponse struct {
ServerID []byte
AttentionNo []byte
AttentionMsg *string
AttentionDetails []TreeEntry
}
AttentionResponse is the body of an SML attention response message.
type Bool ¶
type Bool bool // Bool is an SML boolean value.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type CloseResponse ¶
type CloseResponse struct {
Signature []byte
}
CloseResponse is the body of an SML close response message.
type DecodeOptions ¶
type DecodeOptions struct {
Strict bool // default: true — fail on first malformed message
}
DecodeOptions controls decoder behavior.
type File ¶
type File struct {
Messages []*Message
}
File is a decoded SML transmission containing one or more messages.
func DecodeWithOptions ¶
func DecodeWithOptions(data []byte, opts DecodeOptions) (*File, error)
DecodeWithOptions parses SML binary data with configurable strictness. In strict mode (default), the first parse error aborts decoding. In non-strict mode, malformed messages are skipped and partial results are returned alongside a joined error.
type GetListResponse ¶
type GetListResponse struct {
ClientID []byte
ServerID []byte
ListName []byte
ActSensorTime *Time
ValList []ListEntry
Signature []byte
ActGatewayTime *Time
}
GetListResponse is the body of an SML get-list response message.
type GetProcParameterResponse ¶
GetProcParameterResponse is the body of an SML get-proc-parameter response message.
type GetProfileListResponse ¶
type GetProfileListResponse struct {
ServerID []byte
ActTime *Time
RegPeriod *uint32
ParameterTreePath TreePath
ValTime *Time
Status *uint64
ValList []PeriodEntry
RawData []byte
PeriodSignature []byte
}
GetProfileListResponse is the body of an SML get-profile-list response message.
type GetProfilePackResponse ¶
type GetProfilePackResponse struct {
ServerID []byte
ActTime *Time
RegPeriod *uint32
ParameterTreePath TreePath
HeaderList []ProfileObjHeaderEntry
PeriodList []ProfileObjPeriodEntry
RawData []byte
PeriodSignature []byte
}
GetProfilePackResponse is the body of an SML get-profile-pack response message.
type Int8 ¶
type Int8 int8 // Int8 is a 1-byte signed integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type Int16 ¶
type Int16 int16 // Int16 is a 2-byte signed integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type Int32 ¶
type Int32 int32 // Int32 is a 4-byte signed integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type Int64 ¶
type Int64 int64 // Int64 is an 8-byte signed integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type ListEntry ¶
type ListEntry struct {
ObjName []byte // OBIS code (typically 6 bytes)
Status *uint64
ValTime *Time
Unit *uint8 // DLMS unit code
Scaler *int8
Value Value
Signature []byte
}
ListEntry represents a single meter reading.
func (*ListEntry) OBISString ¶
OBISString formats the 6-byte OBIS code as "A-B:C.D.E*F". Returns "" if ObjName is not exactly 6 bytes.
func (*ListEntry) ScaledValue ¶
ScaledValue returns the numeric value multiplied by 10^Scaler as a float64. Returns (0, false) if Value is nil, non-numeric, or Scaler is nil.
func (*ListEntry) UnitString ¶
UnitString returns the DLMS unit name for the entry's unit code. Returns "" if Unit is nil or the code is unknown.
type Message ¶
type Message struct {
TransactionID []byte
GroupID uint8
AbortOnError uint8
Body MessageBody
CRC uint16
}
Message is a single SML message within a file.
type MessageBody ¶
type MessageBody interface {
// contains filtered or unexported methods
}
MessageBody is implemented by each specific message type.
type MessageHandler ¶
MessageHandler is called for each message. Return non-nil error to stop listening.
type OctetString ¶
type OctetString []byte // OctetString is a raw byte sequence.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type OpenResponse ¶
type OpenResponse struct {
Codepage *string
ClientID []byte
ReqFileID []byte
ServerID []byte
RefTime *Time
SmlVersion *uint8
}
OpenResponse is the body of an SML open response message.
type PeriodEntry ¶
PeriodEntry is used in GetProfileListResponse.
type ProfileObjHeaderEntry ¶
ProfileObjHeaderEntry is used in GetProfilePackResponse.
type ProfileObjPeriodEntry ¶
type ProfileObjPeriodEntry struct {
ValTime *Time
Status *uint64
ValueList []PeriodEntry
}
ProfileObjPeriodEntry is used in GetProfilePackResponse.
type Uint8 ¶
type Uint8 uint8 // Uint8 is a 1-byte unsigned integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type Uint16 ¶
type Uint16 uint16 // Uint16 is a 2-byte unsigned integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
type Uint32 ¶
type Uint32 uint32 // Uint32 is a 4-byte unsigned integer.
Concrete Value types. Named types preserve the wire encoding size so callers can distinguish between e.g. a 1-byte and a 4-byte unsigned integer via a type switch on Value.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
smlreader
command
Command smlreader reads SML binary data and prints meter readings in the same format as the C sml_server reference tool (OBIS#value#unit).
|
Command smlreader reads SML binary data and prints meter readings in the same format as the C sml_server reference tool (OBIS#value#unit). |
|
Package transport handles SML transport layer framing.
|
Package transport handles SML transport layer framing. |