Git Product home page Git Product logo

sunspec's Introduction

SunSpec Rust Implementation

Latest Version Unsafe forbidden

This Rust crate contains code for accessing SunSpec compliant devices in a safe and convenient way.

Highlights

  • Pure Rust library
  • No unsafe code
  • Panic free
  • All communication is done via tokio-modbus without wrapping it in some kind of abstraction layer.
  • Supports Modbus TCP and RTU (via tokio-modbus).
  • Implements "Device Information Model Discovery" as defined in the SunSpec specification.
  • Fully typed models generated from the JSON files contained in the SunSpec models repository
  • Fully typed enums
  • Fully typed bitfields
  • Fully documented. Even the generated models.
  • Reading of complete models in a single request.

Nested and repeating groups are not supported, yet.

Features

Feature Description Extra dependencies Default
tokio Enable tokio_modbus support tokio-modbus yes
serde Enable serde support serde, bitflags/serde yes

Examples

The examples directory in the code repository contains the unabridged code.

Example code for accessing data from a three phase inverter using the model 103

use std::{error::Error, net::SocketAddr, time::Duration};

use clap::Parser;
use itertools::Itertools;
use sunspec::tokio_modbus::{discover_models, read_model};
use tokio::time::sleep;
use tokio_modbus::{client::tcp::connect_slave, Slave};

#[derive(Parser)]
struct Args {
    addr: SocketAddr,
    device_id: u8,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let args = Args::parse();
    let mut ctx = connect_slave(args.addr, Slave(args.device_id)).await?;

    let models = discover_models(&mut ctx).await?.models;
    let m1 = read_model(&mut ctx, &models.m1).await?;

    println!("Manufacturer: {}", m1.mn);
    println!("Model: {}", m1.md);
    println!("Version: {}", m1.vr.as_deref().unwrap_or("(unspecified)"));
    println!("Serial Number: {}", m1.sn);

    println!(
        "Supported models: {}",
        models
            .supported_model_ids()
            .iter()
            .map(|id| id.to_string())
            .join(", ")
    );

    loop {
        let m103 = read_model(&mut ctx, &models.m103).await?;
        let w = m103.w as f32 * 10f32.powf(m103.w_sf.into());
        let wh = m103.wh as f32 * 10f32.powf(m103.wh_sf.into());
        println!("{:12.3} kWh {:9.3} kW", wh / 1000.0, w / 1000.0,);
        sleep(Duration::from_secs(1)).await;
    }
}

FAQ

How does this crate differ from crates like tokio-sunspec, sunspec-models, sunspec_rs?

  • This crate generates all code using Rust code via the official SunSpec models repository with a code generator that was written in Rust, too.

  • All generated models are plain Rust structs. A single Modbus call can return the complete data for a model rather than having to fetch points individually.

  • All public types are documented. Even the generated models.

License

Licensed under either of

at your option.

sunspec's People

Contributors

bikeshedder avatar jacobsvante avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

sunspec's Issues

Enable discovery to continue if reading a holding register hangs

Hi,

We are dealing with a device that claims to be Sunpec-compliant, but fails to respond to reads of holding register 0, making it impossible to use the standard discovery process as implemented in the sunspec module.

Some options to resolve the issue:

  1. Time out reads, so the process could proceed from 0 onwards
  2. Enable specifying the starting address for discovery

Serde support?

Would be nice to be able to serialize the models. E.g. for displaying purposes.

Add support for nested fields and repeating groups

I currently don't own any SunSpec compliant hardware that uses nested fields and/or repeating groups. I'd like to implement this feature to make this crate 100% SunSpec compliant.

If anyone could send me a dump of such data I'd be happy to implement this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.