#plc #ethernet #modbus #automation #hardware #api-bindings #extension

plctag

libplctag 的 Rust 封装,提供了 Rust 风格的 API 和有用的扩展

6 个版本 (3 个重大更改)

0.4.0 2023年1月14日
0.3.0 2022年2月25日
0.2.2 2022年10月16日
0.2.1 2021年10月15日
0.1.0 2021年9月14日

硬件支持 中排名第 376

Download history 15/week @ 2024-03-08 36/week @ 2024-03-15 13/week @ 2024-03-22 13/week @ 2024-03-29 27/week @ 2024-04-05 11/week @ 2024-04-12 46/week @ 2024-04-19 10/week @ 2024-04-26 17/week @ 2024-05-03 19/week @ 2024-05-10 25/week @ 2024-05-17 70/week @ 2024-05-24 40/week @ 2024-05-31 5/week @ 2024-06-07 7/week @ 2024-06-21

每月下载量 84

MIT 许可

79KB
1K SLoC

plctag-rs

libplctag 的 Rust 封装,提供了 Rust 风格的 API 和有用的扩展。

crates.io docs build license

如何使用

plctag 添加到您的 Cargo.toml 文件中

[dependencies]
plctag= "0.3"

crates

示例

读取/写入标签

use plctag::{Encode, Decode, RawTag};
let timeout = 100;//ms
let path="protocol=ab-eip&plc=controllogix&path=1,0&gateway=192.168.1.120&name=MyTag1&elem_count=1&elem_size=16";// YOUR TAG DEFINITION
let tag = RawTag::new(path, timeout).unwrap();

//read tag
let status = tag.read(timeout);
assert!(status.is_ok());
let offset = 0;
let value:u16 = tag.get_value(offset).unwrap();
println!("tag value: {}", value);

let value = value + 10;
tag.set_value(offset, value).unwrap();

//write tag
let status = tag.write(timeout);
assert!(status.is_ok());
println!("write done!");

UDT

读取/写入 UDT

use plctag::{Decode, Encode, RawTag, Result};

// define your UDT
#[derive(Default, Debug, Decode, Encode)]
struct MyUDT {
    #[tag(offset = 0)]
    v1: u16,
    #[tag(offset = 2)]
    v2: u16,
}


let timeout = 100; //ms
                    // YOUR TAG DEFINITION
let path = "protocol=ab-eip&plc=controllogix&path=1,0&gateway=192.168.1.120&name=MyTag2&elem_count=2&elem_size=16";
let tag = RawTag::new(path, timeout).unwrap();

//read tag
let status = tag.read(timeout);
assert!(status.is_ok());
let offset = 0;
let mut value: MyUDT = tag.get_value(offset).unwrap();
println!("tag value: {:?}", value);

value.v1 += 10;
tag.set_value(offset, value).unwrap();

//write tag
let status = tag.write(timeout);
assert!(status.is_ok());
println!("write done!");

注意:当您使用 DecodeEncode 进行派生时,不要执行昂贵的操作。

异步

use plctag::futures::{AsyncTag, Error};

use tokio::runtime;

let rt = runtime::Runtime::new().unwrap();
let res: Result<_, Error> = rt.block_on(async {
    let path="protocol=ab-eip&plc=controllogix&path=1,0&gateway=192.168.1.120&name=MyTag1&elem_count=1&elem_size=16"; // YOUR TAG DEFINITION
    let tag = AsyncTag::create(path).await?;
    let tag_ref = tag.get().await?;
    let offset = 0;
    let value: u16 = tag_ref.read_value(offset).await?;
    println!("tag value: {}", value);

    let value = value + 10;
    tag_ref.write_value(offset, value).await?;
    Ok(())
});
res.unwrap();

路径构建器

use plctag::builder::*;
use plctag::RawTag;

let timeout = 100;
let path = PathBuilder::default()
    .protocol(Protocol::EIP)
    .gateway("192.168.1.120")
    .plc(PlcKind::ControlLogix)
    .name("MyTag1")
    .element_size(16)
    .element_count(1)
    .path("1,0")
    .read_cache_ms(0)
    .build()
    .unwrap();
let tag = RawTag::new(path, timeout).unwrap();
let status = tag.status();
assert!(status.is_ok());

libplctag 的日志适配器

use plctag::log::log_adapt;
use plctag::log::set_debug_level;
use plctag::log::DebugLevel;

log_adapt(); //register logger
set_debug_level(DebugLevel::Info); // set debug level

// now, you can receive log messages by any of logging implementations of crate `log`

构建

请参阅 如何构建 以设置构建环境。

基准测试

cargo bench

图表和保存的数据存储在 target/criterion/$BENCHMARK_NAME/ 目录下

基准测试结果

处理器:Intel(R) Core(TM) i7-9700KF CPU @ 3.60GHz 3.60 GHz 内存:64.0 GB

sync read               time:   [5.8721 ms 5.9099 ms 5.9585 ms]
                        change: [+0.0881% +0.7138% +1.4994%] (p = 0.06 > 0.05)
                        No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
  2 (2.00%) low severe
  1 (1.00%) low mild

async read              time:   [8.5650 ms 8.6148 ms 8.6786 ms]
                        change: [-1.1842% -0.2853% +0.7350%] (p = 0.55 > 0.05)
                        No change in performance detected.
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low severe


async batch-20 read     time:   [18.205 ms 18.431 ms 18.676 ms]
                        change: [-2.0615% -0.1733% +1.5987%] (p = 0.86 > 0.05)
                        No change in performance detected.
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) high mild
  1 (1.00%) high severe

许可

MIT

依赖项

~2.9–5.5MB
~111K SLoC