From 9c3fdbb9f42413d5c587fe8ab75724b5418dcd96 Mon Sep 17 00:00:00 2001 From: Emeric Martineau <11473190+emeric-martineau@users.noreply.github.com> Date: Sun, 14 Jan 2024 12:28:57 +0100 Subject: [PATCH] Allow user set timeout --- README.md | 7 +++++++ src/lib.rs | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 347317c..ec24755 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,13 @@ print!("First channel power: {}", jsy_my_194.channel1.power()); That's all! +## Changelog + +### 1.0.1 + +- Rewrite module to be more easy to understand +- Add new public `read_with_timeout` method to allow user set his timeout + ## License The code is released under MIT License to allow every body to use it in all conditions. If you love open-source software and this crate, please give some money to [HaikuOS](https://haiku-os.org/) or [ReactOS](https://reactos.org). diff --git a/src/lib.rs b/src/lib.rs index 0f4acde..97a9daf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,8 @@ //! | 15 | negative kwh2 | 55, 56, 57, 58 | //! | 16 | crc | 59, 60 | //! +use std::time::Duration; + use embedded_hal::blocking::delay::DelayMs; pub mod error; @@ -265,12 +267,17 @@ where } } - /// Read data. + // Read and wait 100ms pub fn read(&mut self) -> Result<(), error::UartError> { + self.read_with_timeout(100) + } + + /// Read data. + pub fn read_with_timeout(&mut self, timeout_ms: u32) -> Result<(), error::UartError> { // send segment to JSY-MK-194 self.uart.write(&self.segment_write)?; - let is_read_data = self.uart.read(&mut self.segment_read, 100); + let is_read_data = self.uart.read(&mut self.segment_read, timeout_ms); match is_read_data { Ok(data_size) => {