Crate plctag

Source
Expand description

§plctag-rs

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

crates.io docs build license

§How to use

Add plctag to your Cargo.toml

[dependencies]
plctag= "0.4"

§crates

§Examples

§read/write tag

use plctag::{Encode, Decode, RawTag, ValueExt};
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

read/write UDT

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

// 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!");

Note: Do not perform expensive operations when you derives Decode or Encode.

§Async

use plctag::futures::{Error, AsyncTag};
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 mut tag = AsyncTag::create(path).await?;
    let offset = 0;
    let value: u16 = tag.read_value(offset).await?;
    println!("tag value: {}", value);

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

§Path Builder

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());

§Logging adapter for 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`

§Build

Please refer to How to build to setup build environment.

§Static build

Please refer to Static build

§License

MIT

Modules§

builder
builders for tag path and tag
ffi
reexports ffi Apis
futures
plctag-async
log
plctag-log

Structs§

AsyncTag
tag entry, represents a tag in PLC controller
RawTag
wrapper of tag model based on libplctag

Enums§

Status
plc tag error code representations

Traits§

Decode
this trait abstracts tag value. you can use the trait to implement your UDT.
Encode
see Decode
ValueExt
generic value getter/setter

Type Aliases§

Result
plctag result

Derive Macros§

Decode
the macro derives plctag_core::Decode for you automatically.
Encode
the macro derives plctag_core::Encode for you automatically.