Skip to content

Latest commit

History

36 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

s7

A simple library that can be used to communicate with Siemens S7 family PLC devices

This crate provides communication tools for Siemens s7 family devices So far only PG.db_read and PG.db_write have been tested on actual hardware The crate is unstable as of now and provides no guarantees

examples

externcrate s7;use s7::{client::Client, field::Bool, field::Fields, field::Float, tcp, transport::Connection};use std::net::{IpAddr,Ipv4Addr};use std::time::Duration;fnmain(){let addr = Ipv4Addr::new(127,0,0,1);letmut 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();letmut cl = Client::new(t).unwrap();let buffer = &mutvec![0u8;Bool::size()asusize];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 afterletmut offset = 8.4;// Since this is a boolean field, we are going to get back 1 byte
cl.ag_read(db, offset asi32,Bool::size(), buffer).unwrap();// field mod provides types to handle the data from the PLC// create a bool field from the byte we gotletmut 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;letmut cooling_buffer = vec![0u8;Float::size()asusize];
cl.ag_read(db, offset asi32,Float::size(), cooling_buffer.as_mut()).unwrap();letmut 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 valuesfor field in fields.iter(){
cl.ag_write(
field.data_block(),
field.offset(),
field.to_bytes().len()asi32,
field.to_bytes().as_mut(),).unwrap();}}

License

Copyright 2019 Petar Dambovaliev. All rights reserved. This software may be modified and distributed under the terms of the BSD license. See the LICENSE file for details.

About

A simple library that can be used to communicate with Siemens S7 family PLC devices

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages