10个版本
0.1.9 | 2019年10月20日 |
---|---|
0.1.8 | 2019年10月16日 |
#422 in 硬件支持
每月52次下载
61KB
1K SLoC
s7
一个简单的库,可用于与西门子S7系列PLC设备通信
此crate为西门子s7系列设备提供通信工具。到目前为止,仅在真实硬件上测试了PG.db_read
和PG.db_write
。由于目前此crate不稳定,因此不提供任何保证。
示例
extern crate s7;
use s7::{client::Client, field::Bool, field::Fields, field::Float, tcp, transport::Connection};
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
fn main() {
let addr = Ipv4Addr::new(127, 0, 0, 1);
let mut opts = tcp::Options::new(IpAddr::from(addr), 5, 5, Connection::PG);
opts.read_timeout = Duration::from_secs(2);
opts.write_timeout = Duration::from_secs(2);
let t = tcp::Transport::connect(opts).unwrap();
let mut cl = Client::new(t).unwrap();
let buffer = &mut vec![0u8; Bool::size() as usize];
let db = 888;
// the offset in the PLC is represented by a float
// the difit on the left is the index within the block
// the digit after the decimal point is only important for the `Bool` to be able to change the relevant bit
// we don't need after
let mut offset = 8.4;
// Since this is a boolean field, we are going to get back 1 byte
cl.ag_read(db, offset as i32, Bool::size(), buffer).unwrap();
// field mod provides types to handle the data from the PLC
// create a bool field from the byte we got
let mut lights = Bool::new(db, offset, buffer.to_vec()).unwrap();
// the bit in the byte is set without changing any of the other bits
lights.set_value(!lights.value()); // toggle the light switch
offset = 12.0;
let mut cooling_buffer = vec![0u8; Float::size() as usize];
cl.ag_read(db, offset as i32, Float::size(), cooling_buffer.as_mut())
.unwrap();
let mut cooling = Float::new(db, offset, cooling_buffer).unwrap();
cooling.set_value(121.3);
let fields: Fields = vec![Box::new(lights), Box::new(cooling)];
// save back the changed values
for field in fields.iter() {
cl.ag_write(
field.data_block(),
field.offset(),
field.to_bytes().len() as i32,
field.to_bytes().as_mut(),
)
.unwrap();
}
}
许可
版权所有2019年Petar Dambovaliev。保留所有权利。本软件可在BSD许可的条款下修改和分发。有关详细信息,请参阅LICENSE文件。
依赖项
~120KB