simple_modbus 0.1.1

A tool to search files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
pub mod serial;
pub mod stream;

use anyhow::Result;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::time::{Duration, Instant};
use stream::Stream;

/// Modbus从设备 寄存器地址
pub(crate) type Address = u16;

/// Modbus从设备 Id
pub(crate) type Id = u8;

/// Modbus使用16位数表示它的数据项(大端模式)
pub(crate) type Word = u16;

/// Modbus需要读或写的数据数量
pub(crate) type Quantity = u16;

const MODBUS_MAX_PACKET_SIZE: usize = 260;

pub enum Function {
    /// 读指定数量的保持寄存器的数据
    /// (modbus从设备ID, 要读的保持寄存器的起始地址, 要读的保持寄存器的数量)
    ReadHoldingRegisters(Id, Address, Quantity),

    /// 写单个寄存器
    /// (modbus从设备ID, 要写入的寄存器地址, 要写入这个寄存器的单个数据)
    WriteSingleRegister(Id, Address, Word),

    /// 写多个寄存器
    /// (modbus从设备ID, 要写入的寄存器的起始地址, 要写入这些寄存器的数据列表)
    WriteMultipleRegisters(Id, Address, Vec<Word>),

    /// 定制
    /// (要写的数据, 返回的数据)
    Custom(Vec<u8>, Vec<u8>),
}

pub struct Client {
    stream: Box<dyn Stream>,
    need_reply: bool,
}

impl Client {
    pub fn new(stream: Box<dyn Stream>) -> Result<Self> {
        Ok(Self {
            stream,
            need_reply: true,
        })
    }

    pub fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
        self.stream.set_timeout(timeout)
    }

    pub fn read_holding_registers(
        &mut self,
        id: Id,
        address: Address,
        quantity: Quantity,
    ) -> Result<Vec<Word>> {
        let bytes = self.read(Function::ReadHoldingRegisters(id, address, quantity))?;
        pack_bytes(bytes)
    }

    pub fn write_single_register(&mut self, id: Id, address: Address, value: Word) -> Result<()> {
        self.write(Function::WriteSingleRegister(id, address, value))
    }

    pub fn write_multiple_registers(
        &mut self,
        id: Id,
        address: Address,
        values: Vec<Word>,
    ) -> Result<()> {
        self.write(Function::WriteMultipleRegisters(id, address, values))
    }

    pub fn custom(&mut self, req: Vec<u8>, res: Vec<u8>) -> Result<Bytes> {
        self.read(Function::Custom(req, res))
    }

    pub fn set_need_reply(&mut self, need_reply: bool) {
        self.need_reply = need_reply;
    }

    fn get_reply_data(&self, mut reply: Bytes) -> Result<Bytes> {
        if reply.len() <= 5 {
            log::info!("data: {:?}", &reply);
            return Err(anyhow::anyhow!("数据异常, 没有取到有效的数据"));
        }
        let len = *reply.get(2).unwrap();
        if 5 + len as usize != reply.len() {
            log::info!("data: {:?}", &reply);
            return Err(anyhow::anyhow!("数据异常, 没有取到有效的数据"));
        }

        let _ = reply.split_to(3);
        Ok(reply.split_to(len as usize))
    }

    fn validate_reply(&self, req: &Bytes, reply: &BytesMut) -> Result<()> {
        let req_len = req.len();
        let reply_len = reply.len();

        // 检查数据长度, 仅仅简单的判断一下
        if req_len < 3 || reply_len < 3 {
            return Err(anyhow::anyhow!("数据异常"));
        }

        // 检查ID
        if req.get(0) != reply.get(0) {
            return Err(anyhow::anyhow!("数据异常, 响应ID与请求ID不一致"));
        }

        // 检查功能码
        if req.get(1) != reply.get(1) {
            return Err(anyhow::anyhow!("数据异常, 响应功能码与请求功能码不一致"));
        }

        // 检查reply的CRC
        let crc = ((reply[reply_len - 2] as u16) << 8) + (reply[reply_len - 1] as u16);
        let (data, _) = reply.split_at(reply_len - 2);
        if crc != calc_crc(data) {
            return Err(anyhow::anyhow!("数据异常, 响应数据CRC错误"));
        }
        Ok(())
    }

    fn transfer(&mut self, req: &Bytes, reply: &mut BytesMut, write: bool) -> Result<()> {
        match self.stream.write_all(req) {
            Ok(_) => {
                if let Err(e) = self.stream.flush() {
                    return Err(anyhow::anyhow!(format!("传输异常, E: {}", e.to_string())));
                }
                // 写操作 且设置为 不响应
                if write && !self.need_reply {
                    return Ok(());
                }

                match self.stream.read(reply) {
                    Ok(_) => {
                        // log::info!("reply: {:?}", &reply);
                        self.validate_reply(req, reply)?;
                    }
                    Err(e) => return Err(anyhow::anyhow!("read 传输异常, E: {:?}", &e)),
                }
            }
            Err(e) => return Err(anyhow::anyhow!("write_all 传输异常, E: {:?}", &e)),
        }
        Ok(())
    }

    fn read(&mut self, fun: Function) -> Result<Bytes> {
        let (req, mut reply) = Self::build_buffer(fun)?;
        self.transfer(&req, &mut reply, false)?;
        self.get_reply_data(reply.freeze())
    }

    fn write(&mut self, fun: Function) -> Result<()> {
        let (req, mut reply) = Self::build_buffer(fun)?;
        self.transfer(&req, &mut reply, true)
    }

    fn build_buffer(fun: Function) -> Result<(Bytes, BytesMut)> {
        // 6 表示: ID(1) + FUN(1) + ADDR(2) + CRC(2)
        let (req, reply) = match fun {
            Function::WriteSingleRegister(id, addr, data) => {
                // 2 表示: 需要2个字节, 用于保存一个word的data,
                let mut req = BytesMut::with_capacity(6 + 2);
                req.put_u8(id);
                req.put_u8(0x06);
                req.put_u16(addr);
                req.put_u16(data);
                let crc = calc_crc(&req);
                req.put_u16(crc);

                // reply 表示发送数据后, 返回的数据
                let reply = vec![0u8; 8];
                let reply = BytesMut::from(&reply[..]);
                (req, reply)
            }
            Function::WriteMultipleRegisters(id, addr, data) => {
                // 2 表示: 需要2个字节, 用于保存 数据的数量 即word的数量
                // 1 表示: 需要1个字节, 用于保存 要写的数据的字节数

                // byte_cnt 表示: 需要 byte_cnt 个字节, 用于保存 要写的数据

                let word_cnt = data.len() as u16;
                let byte_cnt = 2 * word_cnt as u8;
                let mut req = BytesMut::with_capacity(6 + 2 + 1 + byte_cnt as usize);
                req.put_u8(id);
                req.put_u8(0x10);
                req.put_u16(addr);
                req.put_u16(word_cnt);
                req.put_u8(byte_cnt);
                for d in data {
                    req.put_u16(d);
                }
                let crc = calc_crc(&req);
                req.put_u16(crc);

                let reply = vec![0u8; 8];
                let reply = BytesMut::from(&reply[..]);
                (req, reply)
            }
            Function::ReadHoldingRegisters(id, addr, quantity) => {
                // 2 表示: 需要2个字节, 用于保存 需要读取的数据的数量
                let mut req = BytesMut::with_capacity(6 + 2);
                req.put_u8(id);
                req.put_u8(0x03);
                req.put_u16(addr);
                req.put_u16(quantity);
                let crc = calc_crc(&req);
                req.put_u16(crc);

                let reply = vec![0u8; 5 + quantity as usize * 2];
                let reply = BytesMut::from(&reply[..]);
                (req, reply)
            }
            Function::Custom(req, res) => {
                let req = BytesMut::from(&req[..]);
                let reply = BytesMut::from(&res[..]);
                (req, reply)
            }
        };

        if req.is_empty() {
            return Err(anyhow::anyhow!("无效的数据: 发送的数据为空"));
        }

        if req.len() > MODBUS_MAX_PACKET_SIZE {
            return Err(anyhow::anyhow!("无效的数据: 发送的数据长度太大"));
        }

        Ok((req.freeze(), reply))
    }
}

pub fn calc_crc(data: &[u8]) -> u16 {
    let mut crc = 0xFFFF;
    for x in data {
        crc ^= u16::from(*x);
        for _ in 0..8 {
            let crc_odd = (crc & 0x0001) != 0;
            crc >>= 1;
            if crc_odd {
                crc ^= 0xA001;
            }
        }
    }
    crc << 8 | crc >> 8
}

pub fn pack_bytes(mut bytes: Bytes) -> Result<Vec<u16>> {
    let size = bytes.len();
    if size % 2 != 0 {
        return Err(anyhow::anyhow!("无效的数据, 字节数据非偶数"));
    }

    let mut res = Vec::with_capacity(size / 2 + 1);
    for _ in 0..size / 2 {
        res.push(bytes.get_u16());
    }
    Ok(res)
}

pub fn unpack_bytes(data: &[u16]) -> Vec<u8> {
    let size = data.len();
    let mut res = Vec::with_capacity(size * 2);
    for b in data {
        res.push((*b >> 8 & 0xff) as u8);
        res.push((*b & 0xff) as u8);
    }
    res
}

pub fn pack_bits(bits: &[Coil]) -> Vec<u8> {
    let bitcount = bits.len();
    let packed_size = bitcount / 8 + if bitcount % 8 > 0 { 1 } else { 0 };
    let mut res = vec![0; packed_size];
    for (i, b) in bits.iter().enumerate() {
        let v = match *b {
            Coil::On => 1u8,
            Coil::Off => 0u8,
        };
        res[(i / 8) as usize] |= v << (i % 8);
    }
    res
}

pub fn unpack_bits(bytes: &[u8], count: u16) -> Vec<Coil> {
    let mut res = Vec::with_capacity(count as usize);
    for i in 0..count {
        if (bytes[(i / 8u16) as usize] >> (i % 8)) & 0b1 > 0 {
            res.push(Coil::On);
        } else {
            res.push(Coil::Off);
        }
    }
    res
}

/// Single bit status values, used in read or write coil functions
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Coil {
    On,
    Off,
}

impl Coil {
    fn code(self) -> u16 {
        match self {
            Coil::On => 0xff00,
            Coil::Off => 0x0000,
        }
    }
}

impl From<bool> for Coil {
    fn from(b: bool) -> Coil {
        if b {
            Coil::On
        } else {
            Coil::Off
        }
    }
}

impl std::ops::Not for Coil {
    type Output = Coil;

    fn not(self) -> Coil {
        match self {
            Coil::On => Coil::Off,
            Coil::Off => Coil::On,
        }
    }
}

// #[test]
// fn test_function() -> Result<()> {
//     std::env::set_var("RUST_LOG", "DEBUG");
//     env_logger::init();

//     let stream = Box::new(serial::SerialStream::new("COM16", 19200)?);

//     let mut client = Client::new(stream)?;
//     client.set_timeout(Duration::from_millis(500))?;
//     // client.set_need_reply(false);

//     loop {
//         for id in [15, 16] {
//             // 使能
//             if let Err(e) = client.write_single_register(id, 0x0000, 0x01) {
//                 log::error!("{:?}", &e);
//                 return Err(anyhow::anyhow!("{}", e.to_string()));
//             }

//             // 设置速度
//             if let Err(e) = client.write_single_register(id, 0x0002, 1000) {
//                 log::error!("{:?}", &e);
//                 return Err(anyhow::anyhow!("{}", e.to_string()));
//             }
            
//             // 设置加速度
//             if let Err(e) = client.write_single_register(id, 0x0003, 2000) {
//                 log::error!("{:?}", &e);
//                 return Err(anyhow::anyhow!("{}", e.to_string()));
//             }

//             let pos = 0;
//             log::info!("pos: {:?}", &pos);

//             if let Err(e) = client.write_multiple_registers(
//                 id,
//                 0x0016,
//                 vec![(pos & 0xffff) as u16, (pos >> 16) as u16],
//             ) {
//                 log::error!("{:?}", &e);
//                 return Err(anyhow::anyhow!("{}", e.to_string()));
//             }
//             // std::thread::sleep(Duration::from_secs(3));

//             // match client.read_holding_registers(id, 0x0016, 2) {
//             //     Err(e) => {
//             //         log::error!("{:?}", &e);
//             //         return Err(anyhow::anyhow!("{}", e.to_string()));
//             //     }
//             //     Ok(data) => {
//             //         log::info!("data: {:?}", &data);
//             //     }
//             // }

//             // let pos = 0;
//             // if let Err(e) = client.write_multiple_registers(
//             //     id,
//             //     0x0016,
//             //     vec![(pos & 0xffff) as u16, (pos >> 16) as u16],
//             // ) {
//             //     log::error!("{:?}", &e);
//             //     return Err(anyhow::anyhow!("{}", e.to_string()));
//             // }
//             // // std::thread::sleep(Duration::from_secs(3));

//             // match client.read_holding_registers(id, 0x0016, 2) {
//             //     Err(e) => {
//             //         log::error!("{:?}", &e);
//             //         return Err(anyhow::anyhow!("{}", e.to_string()));
//             //     }
//             //     Ok(data) => {
//             //         log::info!("data: {:?}", &data);
//             //     }
//             // }
//         }
//     }
// }

// #[test]
// fn test_custom_function() -> Result<()> {
//     std::env::set_var("RUST_LOG", "DEBUG");
//     env_logger::init();

//     let stream = Box::new(serial::SerialStream::new("COM16", 19200)?);

//     let mut client = Client::new(stream)?;
//     client.set_timeout(Duration::from_millis(2000))?;

//     // 使用功能码 0x7a 修改设备地址

//     // ID 0x7A TargetId CrcH CrcL

//     let mut req = BytesMut::with_capacity(6);
//     req.put_u8(15);
//     req.put_u8(0x7A);
//     req.put_u16(0x00);
//     req.put_u8(2);
//     let crc = calc_crc(&req);
//     req.put_u16(crc);

//     let res = vec![0u8; 5];

//     let data = client.custom(req.to_vec(), res)?;
//     log::info!("data: {:?}", &data);

//     Ok(())
// }