br_crypto/
hex.rs

1/// modbus crc 封装校验码
2pub fn modbus_crc(code: &str) -> String {
3    let data = decode(code);
4    let mut tmp = 65535u16;
5    for buff in data.iter() {
6        tmp ^= *buff as u16;
7        for _ in 0..8 {
8            let t = tmp & 1u16;
9            if t == 1 {
10                tmp >>= 1;
11                tmp ^= 0xa001;
12            } else {
13                tmp >>= 1;
14            }
15        }
16    }
17    let ret1 = tmp >> 8;
18    let ret1 = ret1 | (tmp << 8);
19    format!("{code}{ret1:X}")
20}
21
22pub fn decode(str: &str) -> Vec<u8> {
23    hex::decode(str).unwrap_or_default()
24}
25
26pub fn encode(data: Vec<u8>) -> String {
27    hex::encode(data)
28}