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§
Structs§
- CpBuilder
- Builder for the
CP=&&...&§ion. - Framer
- Incremental frame extractor for the ASCII HJ212 transport:
##+ 4 digits length + payload + 4 hex CRC + optional\r\n. - Hj212
Flag - Parsed representation of the
Flagfield in HJ212. - Hj212
Packet - Payload
Builder - Builder for the main HJ212 payload section (everything between
##{LEN}and{CRC}), plus convenience methods to wrap into a full frame.
Enums§
- Hj212
Error - Media
Format - Media
Type - 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.