Skip to content

File syn_cjt188.h

FileList > proto > syn_cjt188.h

Go to the source code of this file

CJ/T 188-2004 / CJ/T 188-2018 Smart Metering Protocol. More...

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

Classes

Type Name
struct SYN_CJT188_Decoder
Streaming Decoder State Machine for CJ/T 188.
struct SYN_CJT188_Frame
Decoded CJ/T 188 Protocol Frame.
struct SYN_CJT188_MeterData
Parsed CJ/T 188 Meter Response Data.

Public Functions

Type Name
uint8_t syn_cjt188_checksum (const uint8_t * buf, size_t len)
Calculate CJ/T 188 8-bit checksum.
bool syn_cjt188_decoder_feed (SYN_CJT188_Decoder * decoder, uint8_t byte, SYN_CJT188_Frame * out_frame)
Feed a single byte into the streaming decoder.
void syn_cjt188_decoder_init (SYN_CJT188_Decoder * decoder)
Initialize a CJ/T 188 streaming decoder instance.
size_t syn_cjt188_encode_read_req (uint8_t * out_buf, size_t buf_size, uint8_t meter_type, const uint8_t meter_id, const uint8_t vendor_id, uint16_t data_id, uint8_t seq)
Encode a CJ/T 188 Read Meter Data Request ( 0x01 ).
size_t syn_cjt188_encode_valve_ctrl (uint8_t * out_buf, size_t buf_size, uint8_t meter_type, const uint8_t meter_id, const uint8_t vendor_id, bool open_valve, uint8_t seq)
Encode a CJ/T 188 Valve Control Request ( 0x04 ).
bool syn_cjt188_parse_frame (const uint8_t * buf, size_t len, SYN_CJT188_Frame * out_frame)
Parse a raw CJ/T 188 binary frame.

Macros

Type Name
define SYN_CJT188_CTRL_READ_DATA 0x01U
define SYN_CJT188_CTRL_READ_DATA_RESP 0x81U
define SYN_CJT188_CTRL_WRITE_ADDR 0x15U
define SYN_CJT188_CTRL_WRITE_ADDR_RESP 0x95U
define SYN_CJT188_CTRL_WRITE_DATA 0x04U
define SYN_CJT188_CTRL_WRITE_DATA_RESP 0x84U
define SYN_CJT188_DI_READ_HIST_DATA 0xD120U
define SYN_CJT188_DI_READ_METER_DATA 0x901FU
define SYN_CJT188_DI_VALVE_CONTROL 0xA017U
define SYN_CJT188_END_BYTE 0x16U
define SYN_CJT188_MAX_FRAME_SIZE 128U
define SYN_CJT188_METER_COLD_WATER 0x10U
define SYN_CJT188_METER_COOLING 0x21U
define SYN_CJT188_METER_DRINK_WATER 0x12U
define SYN_CJT188_METER_GAS 0x30U
define SYN_CJT188_METER_HEAT 0x20U
define SYN_CJT188_METER_HOT_WATER 0x11U
define SYN_CJT188_METER_POWER 0x40U
define SYN_CJT188_METER_RECLAIMED_WATER 0x13U
define SYN_CJT188_MIN_FRAME_SIZE 13U
define SYN_CJT188_PREAMBLE_BYTE 0xFEU
define SYN_CJT188_START_BYTE 0x68U
define SYN_CJT188_VALVE_CLOSE 0x99U
define SYN_CJT188_VALVE_OPEN 0x55U

Detailed Description

Implements non-blocking, zero-malloc CJ/T 188 protocol framing, data encoding, valve control commands, 8-bit checksum verification, and streaming UART decoding for cold water, hot water, heat, and gas meters.

Public Functions Documentation

function syn_cjt188_checksum

Calculate CJ/T 188 8-bit checksum.

uint8_t syn_cjt188_checksum (
    const uint8_t * buf,
    size_t len
) 

Parameters:

  • buf Pointer to frame starting at 0x68.
  • len Length of frame from 0x68 up to payload end (excluding CS and 0x16).

Returns:

8-bit sum modulo 256.


function syn_cjt188_decoder_feed

Feed a single byte into the streaming decoder.

bool syn_cjt188_decoder_feed (
    SYN_CJT188_Decoder * decoder,
    uint8_t byte,
    SYN_CJT188_Frame * out_frame
) 

Parameters:

  • decoder Decoder instance.
  • byte Received byte from UART/RS-485.
  • out_frame Output frame structure populated when a complete frame arrives.

Returns:

true if a complete valid frame was decoded, false otherwise.


function syn_cjt188_decoder_init

Initialize a CJ/T 188 streaming decoder instance.

void syn_cjt188_decoder_init (
    SYN_CJT188_Decoder * decoder
) 


function syn_cjt188_encode_read_req

Encode a CJ/T 188 Read Meter Data Request ( 0x01 ).

size_t syn_cjt188_encode_read_req (
    uint8_t * out_buf,
    size_t buf_size,
    uint8_t meter_type,
    const uint8_t meter_id,
    const uint8_t vendor_id,
    uint16_t data_id,
    uint8_t seq
) 

Parameters:

  • out_buf Buffer to write encoded frame into.
  • buf_size Capacity of out_buf (at least 17 bytes for 4x FE + frame).
  • meter_type Meter type (e.g. SYN_CJT188_METER_COLD_WATER).
  • meter_id 5-byte BCD meter ID array.
  • vendor_id 2-byte vendor code array.
  • data_id Data ID (e.g. SYN_CJT188_DI_READ_METER_DATA).
  • seq Sequence number.

Returns:

Total bytes written to out_buf, or 0 if buffer too small.


function syn_cjt188_encode_valve_ctrl

Encode a CJ/T 188 Valve Control Request ( 0x04 ).

size_t syn_cjt188_encode_valve_ctrl (
    uint8_t * out_buf,
    size_t buf_size,
    uint8_t meter_type,
    const uint8_t meter_id,
    const uint8_t vendor_id,
    bool open_valve,
    uint8_t seq
) 

Parameters:

  • out_buf Buffer to write encoded frame into.
  • buf_size Capacity of out_buf (at least 18 bytes).
  • meter_type Meter type.
  • meter_id 5-byte BCD meter ID array.
  • vendor_id 2-byte vendor code array.
  • open_valve true for OPEN (0x55), false for CLOSE (0x99).
  • seq Sequence number.

Returns:

Total bytes written, or 0 if buffer too small.


function syn_cjt188_parse_frame

Parse a raw CJ/T 188 binary frame.

bool syn_cjt188_parse_frame (
    const uint8_t * buf,
    size_t len,
    SYN_CJT188_Frame * out_frame
) 

Parameters:

  • buf Pointer to frame buffer (may begin with optional preamble 0xFE).
  • len Length of raw buffer.
  • out_frame Pointer to output frame structure.

Returns:

true if valid frame parsed, false otherwise.


Macro Definition Documentation

define SYN_CJT188_CTRL_READ_DATA

#define SYN_CJT188_CTRL_READ_DATA `0x01U`

Read meter data request


define SYN_CJT188_CTRL_READ_DATA_RESP

#define SYN_CJT188_CTRL_READ_DATA_RESP `0x81U`

Read meter data response


define SYN_CJT188_CTRL_WRITE_ADDR

#define SYN_CJT188_CTRL_WRITE_ADDR `0x15U`

Write meter address request


define SYN_CJT188_CTRL_WRITE_ADDR_RESP

#define SYN_CJT188_CTRL_WRITE_ADDR_RESP `0x95U`

Write meter address resp


define SYN_CJT188_CTRL_WRITE_DATA

#define SYN_CJT188_CTRL_WRITE_DATA `0x04U`

Write data / control req


define SYN_CJT188_CTRL_WRITE_DATA_RESP

#define SYN_CJT188_CTRL_WRITE_DATA_RESP `0x84U`

Write data / control resp


define SYN_CJT188_DI_READ_HIST_DATA

#define SYN_CJT188_DI_READ_HIST_DATA `0xD120U`

Read historical data


define SYN_CJT188_DI_READ_METER_DATA

#define SYN_CJT188_DI_READ_METER_DATA `0x901FU`

Read meter current data


define SYN_CJT188_DI_VALVE_CONTROL

#define SYN_CJT188_DI_VALVE_CONTROL `0xA017U`

Valve control command


define SYN_CJT188_END_BYTE

#define SYN_CJT188_END_BYTE `0x16U`

Frame end delimiter


define SYN_CJT188_MAX_FRAME_SIZE

#define SYN_CJT188_MAX_FRAME_SIZE `128U`

Max supported frame length


define SYN_CJT188_METER_COLD_WATER

#define SYN_CJT188_METER_COLD_WATER `0x10U`

Cold water meter


define SYN_CJT188_METER_COOLING

#define SYN_CJT188_METER_COOLING `0x21U`

Heat meter (cooling)


define SYN_CJT188_METER_DRINK_WATER

#define SYN_CJT188_METER_DRINK_WATER `0x12U`

Direct drinking water meter


define SYN_CJT188_METER_GAS

#define SYN_CJT188_METER_GAS `0x30U`

Gas meter


define SYN_CJT188_METER_HEAT

#define SYN_CJT188_METER_HEAT `0x20U`

Heat / caloric meter (heat)


define SYN_CJT188_METER_HOT_WATER

#define SYN_CJT188_METER_HOT_WATER `0x11U`

Domestic hot water meter


define SYN_CJT188_METER_POWER

#define SYN_CJT188_METER_POWER `0x40U`

Power meter


define SYN_CJT188_METER_RECLAIMED_WATER

#define SYN_CJT188_METER_RECLAIMED_WATER `0x13U`

Reclaimed water meter


define SYN_CJT188_MIN_FRAME_SIZE

#define SYN_CJT188_MIN_FRAME_SIZE `13U`

Min frame without preamble


define SYN_CJT188_PREAMBLE_BYTE

#define SYN_CJT188_PREAMBLE_BYTE `0xFEU`

Leading preamble byte


define SYN_CJT188_START_BYTE

#define SYN_CJT188_START_BYTE `0x68U`

Frame start delimiter


define SYN_CJT188_VALVE_CLOSE

#define SYN_CJT188_VALVE_CLOSE `0x99U`

Close valve command


define SYN_CJT188_VALVE_OPEN

#define SYN_CJT188_VALVE_OPEN `0x55U`

Open valve command



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