Skip to content

File syn_dlt645.h

FileList > proto > syn_dlt645.h

Go to the source code of this file

DL/T 645 Electricity Meter Communication Protocol (1997 & 2007). More...

  • #include "../common/syn_defs.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_DLT645_Decoder
Streaming byte-at-a-time decoder state.
struct SYN_DLT645_Frame
Decoded DL/T 645 frame representation.

Public Types

Type Name
enum SYN_DLT645_Cmd
typedef void(* SYN_DLT645_FrameCallback
Frame received callback.
enum SYN_DLT645_Ver

Public Functions

Type Name
uint8_t syn_dlt645_calc_checksum (const uint8_t * buf, size_t len)
Compute DL/T 645 arithmetic modulo-256 checksum over buffer.
void syn_dlt645_decoder_feed (SYN_DLT645_Decoder * dec, uint8_t rx_byte)
Feed a single byte into the streaming decoder.
void syn_dlt645_decoder_init (SYN_DLT645_Decoder * dec, SYN_DLT645_Ver version, SYN_DLT645_FrameCallback cb, void * ctx)
Initialize a streaming DL/T 645 decoder.
size_t syn_dlt645_encode (const SYN_DLT645_Frame * frame, uint8_t * out_buf, size_t out_capacity)
Encode a DL/T 645 frame into a binary transmission buffer.
SYN_Status syn_dlt645_parse (const uint8_t * in_buf, size_t len, SYN_DLT645_Ver version, SYN_DLT645_Frame * out_frame)
Parse a raw byte buffer into a DL/T 645 frame.

Macros

Type Name
define SYN_DLT645_ADDR_LEN 6
define SYN_DLT645_EOF 0x16
define SYN_DLT645_OFFSET 0x33
define SYN_DLT645_PREAMBLE 0xFE
define SYN_DLT645_SOF 0x68

Detailed Description

Implements DL/T 645-1997 and DL/T 645-2007 master-slave request/response encoding, decoding, checksum validation, and streaming reception.

Public Types Documentation

enum SYN_DLT645_Cmd

enum SYN_DLT645_Cmd {
    SYN_DLT645_CMD_READ_DATA = 0x11,
    SYN_DLT645_CMD_READ_DATA_RESP = 0x91,
    SYN_DLT645_CMD_WRITE_DATA = 0x14,
    SYN_DLT645_CMD_WRITE_DATA_RESP = 0x94,
    SYN_DLT645_CMD_READ_ADDR = 0x13,
    SYN_DLT645_CMD_READ_ADDR_RESP = 0x93,
    SYN_DLT645_CMD_WRITE_ADDR = 0x15,
    SYN_DLT645_CMD_WRITE_ADDR_RESP = 0x95,
    SYN_DLT645_CMD_CHANGE_BAUD = 0x17,
    SYN_DLT645_CMD_CHANGE_BAUD_RESP = 0x97,
    SYN_DLT645_CMD_ERROR_RESP = 0xD1
};

typedef SYN_DLT645_FrameCallback

Frame received callback.

typedef void(* SYN_DLT645_FrameCallback) (const SYN_DLT645_Frame *frame, void *ctx);


enum SYN_DLT645_Ver

enum SYN_DLT645_Ver {
    SYN_DLT645_VER_1997 = 0,
    SYN_DLT645_VER_2007 = 1
};

Public Functions Documentation

function syn_dlt645_calc_checksum

Compute DL/T 645 arithmetic modulo-256 checksum over buffer.

uint8_t syn_dlt645_calc_checksum (
    const uint8_t * buf,
    size_t len
) 

Parameters:

  • buf Buffer starting from SOF (0x68).
  • len Length of buffer from SOF to end of data field.

Returns:

Modulo-256 checksum byte.


function syn_dlt645_decoder_feed

Feed a single byte into the streaming decoder.

void syn_dlt645_decoder_feed (
    SYN_DLT645_Decoder * dec,
    uint8_t rx_byte
) 

Parameters:

  • dec Decoder instance.
  • rx_byte Single byte received from UART ISR.

function syn_dlt645_decoder_init

Initialize a streaming DL/T 645 decoder.

void syn_dlt645_decoder_init (
    SYN_DLT645_Decoder * dec,
    SYN_DLT645_Ver version,
    SYN_DLT645_FrameCallback cb,
    void * ctx
) 

Parameters:

  • dec Decoder instance.
  • version Expected protocol version.
  • cb Frame received callback.
  • ctx User callback context.

function syn_dlt645_encode

Encode a DL/T 645 frame into a binary transmission buffer.

size_t syn_dlt645_encode (
    const SYN_DLT645_Frame * frame,
    uint8_t * out_buf,
    size_t out_capacity
) 

Parameters:

  • frame Input frame data.
  • out_buf Destination output buffer.
  • out_capacity Output buffer capacity (min 20 bytes).

Returns:

Number of bytes written to out_buf (0 on error).


function syn_dlt645_parse

Parse a raw byte buffer into a DL/T 645 frame.

SYN_Status syn_dlt645_parse (
    const uint8_t * in_buf,
    size_t len,
    SYN_DLT645_Ver version,
    SYN_DLT645_Frame * out_frame
) 

Parameters:

  • in_buf Input buffer containing the complete frame.
  • len Length of input buffer.
  • version Protocol version expected (1997 or 2007).
  • out_frame Destination frame struct.

Returns:

SYN_OK on success, SYN_INVALID_PARAM or SYN_ERROR on validation failure.


Macro Definition Documentation

define SYN_DLT645_ADDR_LEN

#define SYN_DLT645_ADDR_LEN `6`

define SYN_DLT645_EOF

#define SYN_DLT645_EOF `0x16`

define SYN_DLT645_OFFSET

#define SYN_DLT645_OFFSET `0x33`

define SYN_DLT645_PREAMBLE

#define SYN_DLT645_PREAMBLE `0xFE`

define SYN_DLT645_SOF

#define SYN_DLT645_SOF `0x68`


The documentation for this class was generated from the following file src/syntropic/proto/syn_dlt645.h