birkenfeld / ads-rs

Rust crate to access PLCs via the Beckhoff ADS protocol
https://crates.io/crates/ads
Apache License 2.0
40 stars 8 forks source link

Notifications for named symbols possible? #23

Closed Mebus closed 1 month ago

Mebus commented 5 months ago

Hallo,

is it possible to add notifications for named symbols, so I don't have to know the index group and index offset?

Mebus

birkenfeld commented 5 months ago

Hm, good question. At least only the way with group/offset is documented on the Beckhoff pages, but knowing Beckhoff, there is some undocumented access to do it.

Mebus commented 5 months ago

Where can I get the group / offset from? The tmc file?

Mebus

keenanjohnson commented 1 month ago

Hey @Mebus there isn't a great example, but you need to do a couple of things:

  1. First lookup the group / offset using the symbol::get_location method:
  2. Then you can subscribe to a notifcation for the named symbol

It's not one function but you can do as I did and easily write a little wrapper function that will do both for you.

Get Location

let symbol_location = match ads::symbol::get_location(dev, "MAIN.BOOL2") {
    Ok(location) => location,
    Err(e) => {
        eprintln!("Failed to get location: {}", e);
        return;
    }
};

symbol_location will then be a tuple like (0xF030, 0x2107) where the first term is the gorup and the second is the offset.

You can then pass those to the add_notification function: https://github.com/birkenfeld/ads-rs/blob/7d4ca8a81f86e2fac14432c39df9ec20d352ab99/src/client.rs#L786