今天把我为libplctag写的rust封装plctag-rs发布到了github

plctag-rs

a rust wrapper of libplctag, with rust style APIs and useful extensions.

功能

  • 同步API
  • 异步API
  • Path Builder
  • UDT支持

怎么使用

  • 下载libplctag的二进制发布,并解压。
  • 设置环境变量LIBPLCTAG_PATH指向之前解压到的目录。
  • plctag添加到Cargo.toml
[dependencies]
plctag= { git="https://github.com/Joylei/plctag-rs.git"}

示例

同步读写

use plctag::{Accessor, 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!");

异步读写

use plctag::future::AsyncTag;
use tokio::runtime::Runtime;

let mut rt = Runtime::new()::unwrap();
rt.block_on(async move {
    // YOUR TAG DEFINITION
    let path="protocol=ab-eip&plc=controllogix&path=1,0&gateway=192.168.1.120&name=MyTag1&elem_count=1&elem_size=16";
    let tag = AsyncTag::new(path).await.unwrap();

    let offset = 0;
    let value:u16 = 100;
    //write tag
    tag.set_and_write(offset, value).await.unwrap();
    // read tag
    let value:u16 = tag.read_and_get(offset).await.unwrap();
    assert_eq!(value, 100);
});

UDT

use plctag::{Accessor, RawTag, Result, TagValue};

// define your UDT
#[derive(Default, Debug)]
struct MyUDT {
    v1:u16,
    v2:u16,
}
impl TagValue for MyUDT {
    fn get_value(&mut self, tag: &RawTag, offset: u32) -> Result<()>{
        self.v1.get_value(tag, offset)?;
        self.v2.get_value(tag, offset + 2)?;
        Ok(())
    }

    fn set_value(&self, tag: &RawTag, offset: u32) -> Result<()>{
        self.v1.set_value(tag, offset)?;
        self.v2.set_value(tag, offset + 2)?;
        Ok(())
    }
}

fn main(){
    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 = value.v1 + 10;
    tag.set_value(offset, value).unwrap();

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

More

后续可能添加Controllerscan模式来简化使用的难度。