Skip to main content

DalyBMS

Struct DalyBMS 

Source
pub struct DalyBMS { /* private fields */ }
Available on crate feature serialport only.
Expand description

The main struct for interacting with a Daly BMS over a serial port.

It handles sending commands and receiving/decoding responses from the BMS. Most methods require a mutable reference to self as they involve serial communication and may update internal state (like the last execution time or cached status).

Implementations§

Source§

impl DalyBMS

Source

pub fn new(port: &str) -> Result<Self, Error>

Creates a new DalyBMS instance.

§Arguments
  • port: The path to the serial port device (e.g., /dev/ttyUSB0 on Linux, COM3 on Windows).
§Returns

A Result containing the DalyBMS instance or an Error if the serial port cannot be opened or configured.

§Example
use dalybms_lib::serialport::DalyBMS;

let bms = DalyBMS::new("/dev/ttyUSB0");
if let Ok(mut bms_instance) = bms {
    // Use the BMS instance
    if let Ok(soc) = bms_instance.get_soc() {
        println!("SOC: {}%", soc.soc_percent);
    }
} else {
    eprintln!("Failed to connect to BMS: {:?}", bms.err());
}
Source

pub fn set_retry(&mut self, n_retries: u8)

sets the number of retries for a failed send_bytes operation

Source

pub fn set_timeout(&mut self, timeout: Duration) -> Result<(), Error>

Sets the timeout for serial port I/O operations.

§Arguments
  • timeout: The duration to wait for an operation to complete before timing out.
§Returns

A Result indicating success or an Error if the timeout could not be set.

Source

pub fn set_delay(&mut self, delay: Duration)

Sets the minimum delay between sending commands to the BMS.

If the provided delay is less than MINIMUM_DELAY from the protocol module, MINIMUM_DELAY will be used.

§Arguments
  • delay: The desired minimum delay between commands.
Source

pub fn get_soc(&mut self) -> Result<Soc, Error>

Retrieves the State of Charge (SOC) and other primary battery metrics.

§Returns

A Result containing the Soc data or an Error if the command fails or decoding is unsuccessful.

§Example
let soc_data = bms.get_soc()?;
println!("Voltage: {:.1}V, Current: {:.1}A, SOC: {:.1}%",
         soc_data.total_voltage, soc_data.current, soc_data.soc_percent);
Source

pub fn get_cell_voltage_range(&mut self) -> Result<CellVoltageRange, Error>

Retrieves the highest and lowest cell voltages in the battery pack.

§Returns

A Result containing the CellVoltageRange data or an Error.

Source

pub fn get_temperature_range(&mut self) -> Result<TemperatureRange, Error>

Retrieves the highest and lowest temperatures measured by the BMS.

§Returns

A Result containing the TemperatureRange data or an Error.

Source

pub fn get_mosfet_status(&mut self) -> Result<MosfetStatus, Error>

Retrieves the status of the charging and discharging MOSFETs, and other related data.

§Returns

A Result containing the MosfetStatus data or an Error.

Source

pub fn get_status(&mut self) -> Result<Status, Error>

Retrieves general status information from the BMS, including cell count and temperature sensor count.

This method also caches the retrieved status internally, as this information is required by other methods like get_cell_voltages and get_cell_temperatures. It’s recommended to call this method at least once before calling those methods.

§Returns

A Result containing the Status data or an Error.

Source

pub fn get_cell_voltages(&mut self) -> Result<CellVoltages, Error>

Retrieves the voltage of each individual cell in the battery pack.

Note: get_status() must be called at least once before this method to determine the number of cells.

§Returns

A Result containing a CellVoltages of cell voltages or an Error. Returns Error::StatusError if get_status() was not called previously.

Source

pub fn get_cell_temperatures(&mut self) -> Result<Vec<i32>, Error>

Retrieves the temperature from each individual temperature sensor.

Note: get_status() must be called at least once before this method to determine the number of temperature sensors.

§Returns

A Result containing a Vec<i32> of temperatures in Celsius or an Error. Returns Error::StatusError if get_status() was not called previously.

Source

pub fn get_balancing_status(&mut self) -> Result<Vec<bool>, Error>

Retrieves the balancing status of each individual cell.

Note: get_status() must be called at least once before this method to determine the number of cells.

§Returns

A Result containing a Vec<bool> where true indicates the cell is currently balancing, or an Error. Returns Error::StatusError if get_status() was not called previously.

Source

pub fn get_errors(&mut self) -> Result<Vec<ErrorCode>, Error>

Retrieves a list of active error codes from the BMS.

§Returns

A Result containing a Vec<ErrorCode> of active errors or an Error. An empty vector means no errors are currently active.

Source

pub fn set_discharge_mosfet(&mut self, enable: bool) -> Result<(), Error>

Enables or disables the discharging MOSFET.

§Arguments
  • enable: Set to true to enable the discharging MOSFET, false to disable it.
§Returns

An empty Result indicating success or an Error.

Source

pub fn set_charge_mosfet(&mut self, enable: bool) -> Result<(), Error>

Enables or disables the charging MOSFET.

§Arguments
  • enable: Set to true to enable the charging MOSFET, false to disable it.
§Returns

An empty Result indicating success or an Error.

Source

pub fn set_soc(&mut self, soc_percent: f32) -> Result<(), Error>

Sets the State of Charge (SOC) percentage on the BMS.

§Arguments
  • soc_percent: The desired SOC percentage (0.0 to 100.0). Values outside this range will be clamped by the protocol.
§Returns

An empty Result indicating success or an Error.

Source

pub fn reset(&mut self) -> Result<(), Error>

Resets the BMS to its factory default settings.

Use with caution!

§Returns

An empty Result indicating success or an Error.

Trait Implementations§

Source§

impl Debug for DalyBMS

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.