diff --git a/src/dsmr/reader.rs b/src/dsmr/reader.rs index 5d6aeb3..1a8ce2f 100644 --- a/src/dsmr/reader.rs +++ b/src/dsmr/reader.rs @@ -108,14 +108,15 @@ pub fn connect_to_meter( consumer: &mut dyn super::TelegramConsumer, ) { log::info!( - "Connecting to {} using baud rate {} and parity bit {:#?}", + "Connecting to {} using baud rate {}, byte size {} and parity bit {:#?}", &serial_settings.port, &serial_settings.baud_rate, + &serial_settings.byte_size, &serial_settings.parity_bit ); let mut port = serialport::new(&serial_settings.port, serial_settings.baud_rate) - .data_bits(serialport::DataBits::Seven) + .data_bits(to_databits(&serial_settings.byte_size)) .flow_control(serialport::FlowControl::None) .parity(to_serial_port_parity_bit(&serial_settings.parity_bit)) .stop_bits(serialport::StopBits::One) @@ -134,6 +135,17 @@ fn to_serial_port_parity_bit(input: &ParityBitSetting) -> serialport::Parity { } } +fn to_databits(input: &u8) -> serialport::DataBits { + match input { + 0..=4 => serialport::DataBits::Eight, + 5 => serialport::DataBits::Five, + 6 => serialport::DataBits::Six, + 7 => serialport::DataBits::Seven, + 8 => serialport::DataBits::Eight, + 9..=u8::MAX => serialport::DataBits::Eight, + } +} + #[cfg(test)] mod tests { #[allow(unused_imports)] diff --git a/src/dsmr/settings.rs b/src/dsmr/settings.rs index 95d471d..5932879 100644 --- a/src/dsmr/settings.rs +++ b/src/dsmr/settings.rs @@ -12,6 +12,7 @@ pub struct SerialSettings { pub port: String, pub baud_rate: u32, pub parity_bit: ParityBitSetting, + pub byte_size: u8, } pub struct Host { @@ -46,11 +47,16 @@ fn read_serial_settings(settings: &HashMap) -> Result ParityBitSetting::None, }; + let byte_size = match settings.get("serial_bytesize") { + Some(value) => value.parse::().unwrap(), + None => 8, + }; Ok(SerialSettings { port: serial_port.to_string(), baud_rate: serial_baudrate, parity_bit, + byte_size, }) } @@ -178,6 +184,20 @@ mod tests { assert_eq!(value.parity_bit, ParityBitSetting::Even); } + #[test] + fn read_serial_settings_with_byte_size_seven() { + let mut settings = HashMap::new(); + settings.insert(String::from("serial_port"), String::from("/dev/ttyUSB0")); + settings.insert(String::from("serial_baudrate"), String::from("9600")); + settings.insert(String::from("serial_bytesize"), String::from("7")); + + let result = read_serial_settings(&settings); + + assert_eq!(result.is_ok(), true); + let value = result.unwrap(); + assert_eq!(value.byte_size, 7); + } + #[test] fn host_settings_single_pair() { let mut settings = HashMap::new();