Skip to content

Commit

Permalink
WiP groups
Browse files Browse the repository at this point in the history
  • Loading branch information
bikeshedder committed Oct 24, 2024
1 parent f05e506 commit da4ee7c
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 12 deletions.
35 changes: 32 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ members = [
"sunspec-gen",
"examples/readme",
"examples/model103",
"examples/model712",
]
3 changes: 2 additions & 1 deletion examples/model103/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.then(|| Duration::from_secs_f32(args.read_timeout)),
..Default::default()
},
).await?;
)
.await?;

let m1: Model1 = client.read_model().await?;

Expand Down
14 changes: 14 additions & 0 deletions examples/model712/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sunspec-example-model712"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1.33.0", features = ["rt-multi-thread", "macros", "time"] }
tokio-modbus = { version = "0.15", features = ["tcp"] }
sunspec = { path = "../../" }
clap = { version = "4.4.7", features = ["derive"] }
itertools = "0.13.0"
26 changes: 26 additions & 0 deletions examples/model712/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Model 103 example

This code connects to a device that supports sunspec via modbus TCP and
outputs the contents of model 1 and then proceeds reading model 103
(three phase inverter) outputting the value "W / W_SF" and "WH / WH_SF"
every second.

Usage example:

```
$ cargo run 192.168.178.38:1502 1
```

Example output:

```
Manufacturer: SolarEdge
Model: SE25K-RW00IBNM4
Version: 0004.0018.0518
Serial Number: -redacted-
Supported models: 1, 103
157.185 kWh 2.292 kW
157.185 kWh 2.269 kW
157.186 kWh 2.270 kW
...
```
68 changes: 68 additions & 0 deletions examples/model712/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::{error::Error, net::SocketAddr, time::Duration};

use clap::Parser;
use itertools::Itertools;
use sunspec::{
client::{AsyncClient, Config}, models::{model1::Model1, model712::Model712}, DEFAULT_DISCOVERY_ADDRESSES
};
use tokio_modbus::{client::tcp::connect_slave, Slave};

#[derive(Parser)]
struct Args {
addr: SocketAddr,
device_id: u8,
#[arg(
long,
short='d',
help = "Discovery addresses",
name = "ADDRESS",
default_values_t = DEFAULT_DISCOVERY_ADDRESSES
)]
discovery_addresses: Vec<u16>,
#[arg(
long,
short = 't',
help = "Read timeout in seconds",
name = "SECONDS",
default_value_t = 1.0
)]
read_timeout: f32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

let mut client = AsyncClient::new(
connect_slave(args.addr, Slave(args.device_id)).await?,
Config {
discovery_addresses: args.discovery_addresses,
read_timeout: (args.read_timeout != 0.0)
.then(|| Duration::from_secs_f32(args.read_timeout)),
..Default::default()
},
)
.await?;

let m1: Model1 = client.read_model().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: {}",
client
.models
.supported_model_ids()
.iter()
.map(|id| id.to_string())
.join(", ")
);

let m712: Model712 = client.read_model().await?;
println!("{:?}", m712);

Ok(())
}
3 changes: 2 additions & 1 deletion examples/readme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut client = AsyncClient::new(
connect_slave(args.addr, Slave(args.device_id)).await?,
Config::default(),
).await?;
)
.await?;

let m1: Model1 = client.read_model().await?;

Expand Down
3 changes: 2 additions & 1 deletion src/client/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl<C: AsyncModbusClient> AsyncClient<C> {
&mut client,
&config.discovery_addresses,
config.read_timeout,
).await?;
)
.await?;
Ok(Self {
client,
config,
Expand Down
2 changes: 2 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{DecodeError, Models};
pub trait Model: Sized {
/// Model ID
const ID: u16;
/// Model length
const LEN: u16;
/// Parse model points from a given u16 slice
fn from_data(data: &[u16]) -> Result<Self, DecodeError>;
/// Get model address from discovered models struct
Expand Down
Loading

0 comments on commit da4ee7c

Please sign in to comment.