Skip to content

Commit

Permalink
Make byte size configurable
Browse files Browse the repository at this point in the history
* Make byte size configurable
* Format code

Closes #76.
  • Loading branch information
mthmulders authored Oct 26, 2023
1 parent d869a31 commit e667dcf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/dsmr/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)]
Expand Down
20 changes: 20 additions & 0 deletions src/dsmr/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -46,11 +47,16 @@ fn read_serial_settings(settings: &HashMap<String, String>) -> Result<SerialSett
},
None => ParityBitSetting::None,
};
let byte_size = match settings.get("serial_bytesize") {
Some(value) => value.parse::<u8>().unwrap(),
None => 8,
};

Ok(SerialSettings {
port: serial_port.to_string(),
baud_rate: serial_baudrate,
parity_bit,
byte_size,
})
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e667dcf

Please sign in to comment.