Crate a

Crate a 

Source
Expand description

a implements the HJ 212 ASCII protocol (protocol-layer only).

This crate focuses on:

  • Receiving: stream deframing (Framer) + frame parsing (parse_frame)
  • Sending: building CP=&&...&& and payloads (CpBuilder, PayloadBuilder) + frame building (build_frame)

It intentionally does not include networking/serial I/O stacks.

§Parse a frame

use a::{build_frame, parse_frame};

let payload = "QN=1;ST=22;CN=2011;PW=123456;MN=ABC;Flag=7;CP=&&DataTime=20250101010101;a21026-Rtd=12.3&&";
let frame = build_frame(payload);
let pkt = parse_frame(&frame).unwrap();

assert_eq!(pkt.mn.as_deref(), Some("ABC"));
assert_eq!(pkt.cp.get("a21026-Rtd").map(String::as_str), Some("12.3"));

§Deframe from a stream (TCP/serial)

use a::{Framer, parse_frame};

let mut framer = Framer::new();
framer.push(b"##0025QN=1;ST=22;CN=2011;CP=&&&&");

while let Some(frame_bytes) = framer.next_frame() {
    let frame = String::from_utf8(frame_bytes).expect("HJ212 is ASCII");
    let _pkt = parse_frame(&frame)?;
}

§Build a sending frame

use a::{CpBuilder, PayloadBuilder, parse_frame};

let mut cp = CpBuilder::new();
cp.data_time("20250101010101")
  .rtd_flag("a21026", "12.3", "N");

let frame = PayloadBuilder::new("QN1", "123456", "ABC", cp.build()).frame();
let pkt = parse_frame(&frame).unwrap();
assert_eq!(pkt.mn.as_deref(), Some("ABC"));

Re-exports§

pub use crypto::decrypt_data_segment;
pub use crypto::encrypt_data_segment;
pub use crypto::BlockCipher16;
pub use crypto::EncryptedDataSegment;

Modules§

crypto

Structs§

CpBuilder
Builder for the CP=&&...&& section.
Framer
Incremental frame extractor for the ASCII HJ212 transport: ## + 4 digits length + payload + 4 hex CRC + optional \r\n.
Hj212Flag
Parsed representation of the Flag field in HJ212.
Hj212Packet
PayloadBuilder
Builder for the main HJ212 payload section (everything between ##{LEN} and {CRC}), plus convenience methods to wrap into a full frame.

Enums§

Hj212Error
MediaFormat
MediaType
Appendix H (HJ 212—2025) multimedia file transfer parameters.

Functions§

build_data_ack
Build a standard “数据应答” message (CN=9014) with an empty CP.
build_exe_rtn
Build a standard “执行结果” message (CN=9012) with CP=&&ExeRtn=...&&.
build_frame
Wrap a payload into a standard HJ212 frame (HJ 212—2025 6.3.2): ## + 4-digit length + payload + 4-hex CRC + \r\n.
build_frame_compat
Build a compat frame without the standard CRLF suffix.
build_frame_standard
Build a standard HJ212 frame (HJ 212—2025 6.3.2): ## + 4-digit length + payload + 4-hex CRC + \r\n.
build_notify_ack
Build a standard “通知应答” message (CN=9013) with an empty CP.
build_qn_rtn
Build a standard “请求应答” message (CN=9011) with CP=&&QnRtn=...&&.
crc16_ansi
HJ 212—2025 “ANSI CRC16” (poly 0xA001, init 0xFFFF) as described in the standard text.
crc16_hex_lower
Default CRC hex rendering used by frame builders/parsers: HJ212 ANSI CRC16.
crc16_hex_upper
Default CRC hex rendering used by frame builders/parsers: HJ212 ANSI CRC16.
crc16_modbus
CRC16/Modbus (poly 0xA001, init 0xFFFF), used by many historical HJ212 implementations.
crc16_modbus_hex_lower
Explicit CRC16/Modbus hex rendering (compat).
crc16_modbus_hex_upper
Explicit CRC16/Modbus hex rendering (compat).
parse_datatime_to_utc
Parse HJ212 DataTime (commonly: YYYYMMDDhhmmss) into UTC.
parse_frame
Parse one HJ212 frame like: ##0453QN=...;ST=..;...;CP=&&DataTime=...;a21005-Rtd=1.1,...;&&c0c1
parse_frame_strict
Parse a standard HJ212 frame strictly (HJ 212—2025 6.3.2): ## + 4-digit length + payload + 4-hex CRC + \r\n.