modbus_relay/config/
rtu.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4
5use super::{DataBits, Parity, RtsType, StopBits};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(deny_unknown_fields)]
9pub struct Config {
10    pub device: String,
11    pub baud_rate: u32,
12    pub data_bits: DataBits,
13    pub parity: Parity,
14    pub stop_bits: StopBits,
15
16    /// Flow control settings for the serial port
17    pub rts_type: RtsType,
18    pub rts_delay_us: u64,
19
20    /// Whether to flush the serial port after writing
21    pub flush_after_write: bool,
22
23    /// Timeout for the entire transaction (request + response)
24    #[serde(with = "humantime_serde")]
25    pub transaction_timeout: Duration,
26
27    /// Timeout for individual read/write operations on serial port
28    #[serde(with = "humantime_serde")]
29    pub serial_timeout: Duration,
30
31    /// Maximum size of the request/response buffer
32    pub max_frame_size: u64,
33}
34
35impl Default for Config {
36    fn default() -> Self {
37        Self {
38            device: "/dev/ttyAMA0".to_string(),
39            baud_rate: 9600,
40            data_bits: DataBits::default(),
41            parity: Parity::default(),
42            stop_bits: StopBits::default(),
43            rts_type: RtsType::default(),
44            rts_delay_us: 3500,
45            flush_after_write: true,
46            transaction_timeout: Duration::from_secs(5),
47            serial_timeout: Duration::from_secs(1),
48            max_frame_size: 256,
49        }
50    }
51}
52
53impl Config {
54    pub fn serial_port_info(&self) -> String {
55        format!(
56            "{} ({} baud, {} data bits, {} parity, {} stop bits)",
57            self.device, self.baud_rate, self.data_bits, self.parity, self.stop_bits
58        )
59    }
60}