snap7-rs -> rust-snap7
[!IMPORTANT]
[2024 Jun 25] crate name change to rust-snap7
This repository add utils module like in python-snap7 to handle data from PLC or vice verse.
Utils Example
use std::thread;
use std::time::Duration;
use rust_snap7::utils;
use rust_snap7::AreaCode;
use rust_snap7::MaskKind;
use rust_snap7::S7Client;
use rust_snap7::S7Server;
use rust_snap7::{InternalParam, InternalParamValue};
fn create_s7_server() -> Result<(), Box<dyn std::error::Error>> {
let server = S7Server::create();
let mut db_buff = [0u8; 1024];
server
.register_area(AreaCode::S7AreaDB, 1, &mut db_buff)
.expect("Failed to register area");
server
.set_mask(MaskKind::Event, 0x00020000 | 0x00040000)
.expect("Failed to set mask");
server
.set_events_callback(Some(move |_, p_event, _| {
if let Ok(text) = S7Server::event_text(p_event) {
println!("Server Event: {:?}", text);
}
}))
.expect("Failed to set events callback");
server.start().expect("Failed to start server");
std::thread::sleep(std::time::Duration::from_secs(10));
server.stop().expect("Failed to stop server");
Ok(())
}
fn connect_to_plc() -> Result<(), String> {
let client = S7Client::create();
client
.set_param(InternalParam::RemotePort, InternalParamValue::U16(102))
.expect("Failed to set remote port value");
match client.connect_to("127.0.0.1", 0, 1) {
Ok(_) => {
let mut buff = [0u8; 2];
match client.db_read(1, 20, 2, &mut buff) {
Ok(_) => {
let data = utils::getters::get_int(buff.to_vec().as_ref(), 0);
println!("Successfully read from DB1.W20: {}", data);
}
Err(e) => println!("Failed to read DB: {:?}", e),
}
client.disconnect().expect("Client disconnect failed");
}
Err(e) => println!("Connection to PLC failed: {:?}", e),
}
Ok(())
}
fn main() {
std::thread::spawn(|| {
if let Err(e) = create_s7_server() {
eprintln!("S7 Server Error: {}", e);
}
});
thread::sleep(Duration::from_millis(500));
connect_to_plc().expect("conenct fn error");
}
This is a Rust binding of the snap7 C++ library, linked statically to snap7 with no additional dependencies.
Warning: This library has not undergone any security clearance and is to be used at your own risk.
Note: This repository is based on the original snap7-rs, and was created to translate some stuff in the original repository to English and fix some compilation errors.
License
The source code and documentation for this project are under the Mulan Loose License (MulanPSL-2.0).
snap7 itself is licensed under the terms of the GNU Lesser General Public License (LGPL v3+).