1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// plctag-rs
//
// a rust wrapper of libplctag, with rust style APIs and useful extensions.
// Copyright: 2022, Joylei <leingliu@gmail.com>
// License: MIT
/*!
# plctag-rs
a rust wrapper of [libplctag](https://github.com/libplctag/libplctag), with rust style APIs and useful extensions.
[](https://crates.io/crates/plctag)
[](https://docs.rs/plctag)
[](https://github.com/joylei/plctag-rs/actions?query=workflow%3A%22build%22)
[](https://github.com/joylei/plctag-rs/blob/master/LICENSE)
## How to use
Add `plctag` to your Cargo.toml
```toml
[dependencies]
plctag= "0.4"
```
## crates
- [plctag](https://crates.io/crates/plctag) reexports everything from below crates.
- [plctag-core](https://crates.io/crates/plctag-core) a rust wrapper of [libplctag](https://github.com/libplctag/libplctag), with rust style APIs and useful extensions.
- [plctag-async](https://crates.io/crates/plctag-async) async wrapper.
- [plctag-log](https://crates.io/crates/plctag-log) log adapter for `libplctag`
- [plctag-derive](https://crates.io/crates/plctag-derive) macros for `plctag`
- [plctag-sys](https://crates.io/crates/plctag-sys) native libplctag binding
## Examples
### read/write tag
```rust,no_run
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
```rust,ignore
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
```rust,no_run
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
```rust,no_run
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`
```rust,no_run
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](https://github.com/Joylei/plctag-rs/tree/master/crates/sys#build) to setup build environment.
### Static build
Please refer to [Static build](https://github.com/Joylei/plctag-rs/tree/master/crates/sys#Static%20build)
## License
MIT
*/
pub use *;
pub use ;
pub use plctag_log as log;
pub use plctag_async as futures;
pub use AsyncTag;