Struct S7Server

Source
pub struct S7Server { /* private fields */ }
Expand description

S7 服务端

§Examples

use snap7_rs::{AreaCode, InternalParam, InternalParamValue, S7Server, MaskKind};

// 创建 S7 服务端
let server = S7Server::create();

// 创建共享内存区
let mut db_buff = [0u8; 1024];

// 添加共享区块
assert!(server
    .register_area(AreaCode::S7AreaDB, 1, &mut db_buff)
    .is_ok());

// 过滤读和写
assert!(server
    .set_mask(MaskKind::Event, 0x00020000 | 0x00040000)
    .is_ok());

// 设置事件回调
assert!(server
    .set_events_callback(Some(move |_, p_event, _| {
        if let Ok(text) = S7Server::event_text(p_event) {
            println!("{:?}", text);
        }
    }))
    .is_ok());

// 启动服务
if let Err(e) = server.start() {
    dbg!(e);
}

// 处理逻辑
//loop {
   // ......
//}

// 关闭服务
assert!(server.stop().is_ok());

Implementations§

Source§

impl S7Server

Source

pub fn create() -> Self

创建一个 S7 服务端

Source

pub fn get_param( &self, param: InternalParam, value: &mut InternalParamValue, ) -> Result<()>

读取一个服务端对象的内部参数。

输入参数:

  • param: 内部参数类型
  • value: 内部参数值

返回值:

  • Ok: 设置成功
  • Err: 设置失败
Source

pub fn set_param( &self, param: InternalParam, value: InternalParamValue, ) -> Result<()>

设置服务端的内部参数。

输入参数:

  • param: 内部参数类型
  • value: 内部参数值

返回值:

  • Ok: 设置成功
  • Err: 设置失败
Source

pub fn start_to(&self, address: &str) -> Result<()>

启动服务端并将其绑定到指定的 IP 地址和 TCP 端口。

输入参数:

  • address: 服务器地址

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn start(&self) -> Result<()>

启动服务端并将其绑定到 start_to() 中指定的 IP 地址。

返回值:

  • Ok: 操作成功
  • Err: 操作失败

注:如果 start_to() 之前未被调用,则绑定 IP 到 0.0.0.0。

Source

pub fn stop(&self) -> Result<()>

停止服务端,优雅地断开所有客户端的连接,销毁所有的 S7 作业,并解除监听器套接字与地址的绑定。

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn register_area( &self, area_code: AreaCode, index: u16, buff: &mut [u8], ) -> Result<()>

共享一个内存区域,该内存块将被客户端看到。

输入参数:

  • area_code: 区块类型
  • index: 要分享的数据块(DB)编号。如果 area_code != S7AreaDB 则被忽略,值为 0。
  • buff: 要分享的内存缓冲区

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn unregister_area(&self, area_code: AreaCode, index: u16) -> Result<()>

解除先前 register_area() 共享的内存区域,该内存块将不再被客户端看到。

输入参数:

  • area_code: 区块类型
  • index: 要解除的数据块(DB)编号。如果 area_code != S7AreaDB 则被忽略,值为 0。

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn lock_area(&self, area_code: AreaCode, index: u16) -> Result<()>

锁定一个共享内存区域。

输入参数:

  • area_code: 区块类型
  • index: 要解除的数据块(DB)编号。如果 area_code != S7AreaDB 则被忽略,值为 0。

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn unlock_area(&self, area_code: AreaCode, index: u16) -> Result<()>

解锁先前锁定的共享内存区域。

输入参数:

  • area_code: 区块类型
  • index: 要解除的数据块(DB)编号。如果 area_code != S7AreaDB 则被忽略,值为 0。

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn set_events_callback<F>(&self, callback: Option<F>) -> Result<()>
where F: FnMut(*mut c_void, *mut TSrvEvent, c_int) + 'static,

设置服务器对象在创建事件时要调用的用户回调。

输入参数:

  • callback: 回调函数

返回值:

  • Ok: 操作成功
  • Err: 操作失败
§Examples
use std::sync::{Arc,Mutex};

let num = Arc::new(Mutex::new(32));
let num_clone = te.clone();
server.set_events_callback(Some(move |_, p_event, _| {
    let mut num = num_clone.lock().unwrap();
    *num += 100;
    println!("num: {}", num);
    if let Some(text) = S7Server::event_text(p_event) {
        println!("{:?}", text);
    }
})).unwrap();
println!("num:{}", num.lock().unwrap());
Source

pub fn set_rw_area_callback<F>(&self, callback: Option<F>) -> Result<()>

设置服务端对象在客户请求读/写时要调用的用户回调。。

输入参数:

  • callback: 回调函数

返回值:

  • Ok: 操作成功
  • Err: 操作失败
§Examples
use std::ffi::*;

server.set_rw_area_callback(Some(
    move |usr_ptr, sender, operation, ps7tag: PS7Tag, p_usr_data: *mut c_void| {
        unsafe {
            let pbuff = p_usr_data as *mut u8;
            if operation == 0 {
                println!("读请求!");
            } else {
                println!("写请求!");
            }
            let p7 = *ps7tag;
            match p7.Area {
                0x81 => println!("Area: PE"),
                0x82 => println!("Area: PA"),
                0x83 => println!("Area: MK"),
                0x1c => println!("Area: CT"),
                0x1d => println!("Area: TM"),
                0x84 => println!("Area: DB{}", p7.DBNumber as i32),
                _ => println!("未定义的 Area"),
            }
            println!("Strat: {}", p7.Start as i32);
            println!("Size: {}", p7.Size as i32);
            if operation == 1 {
                let buff = std::slice::from_raw_parts(pbuff, p7.Size as usize);
                println!("pUsrData: {:#x?}", buff);
            } else {
                //
            }
        }
    }
)).unwrap();
Source

pub fn set_read_events_callback<F>(&self, callback: Option<F>) -> Result<()>
where F: FnMut(*mut c_void, *mut TSrvEvent, c_int) + 'static,

设置服务端对象在创建读取事件时要调用的用户回调。

输入参数:

  • callback: 回调函数

返回值:

  • Ok: 操作成功
  • Err: 操作失败
§Examples
server.set_read_events_callback(Some(|ptr, p_event, size| {
    println!("ptr: {:?}, size: {}", ptr, size);
    if let Some(text) = S7Server::event_text(p_event) {
        println!("{:?}", text);
    }
})).unwrap();
Source

pub fn get_mask(&self, mask_kind: MaskKind, mask: &mut u32) -> Result<()>

读取指定的过滤器掩码。

输入参数:

  • mask_kind: 掩码类型
  • mask: 掩码值

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn set_mask(&self, mask_kind: MaskKind, mask: u32) -> Result<()>

写入指定的过滤掩码。

输入参数:

  • mask_kind: 掩码类型
  • mask: 掩码值

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn pick_event( &self, event: &mut TSrvEvent, evt_ready: &mut i32, ) -> Result<()>

从事件队列中提取一个事件(如果有的话)。

输入参数:

  • event: 事件变量
  • evt_ready: 提取是否成功,返回 1 代表提取成功,0 代表无事件。

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn clear_events(&self) -> bool

清空事件队列。

返回值:

  • true: 操作成功
  • false: 操作失败
Source

pub fn get_status( &self, server_status: &mut i32, cpu_status: &mut i32, client_count: &mut i32, ) -> Result<()>

读取服务器状态、虚拟 CPU 状态和连接的客户端数量。

输入参数:

  • server_status: 服务端状态
    • 0: 服务停止
    • 1: 服务运行
    • 2: 服务错误
  • cpu_status: CPU 状态
    • 0x00: 状态未知
    • 0x08: CPU Run
    • 0x04: CPU Stop
  • client_count: 客户端连接数

返回值:

  • Ok: 操作成功
  • Err: 操作失败

注:CPU 的状态可以由客户端调用相关的 S7 控制功能(冷启动/热启动/停止)或以编程方式,在服务器端调用函数 set_cpu_status() 来改变。

Source

pub fn set_cpu_status(&self, cpu_status: i32) -> Result<()>

设置虚拟 CPU 状态。

输入参数:

  • cpu_status: CPU 状态
    • 0x00: 状态未知
    • 0x08: CPU Run
    • 0x04: CPU Stop

返回值:

  • Ok: 操作成功
  • Err: 操作失败
Source

pub fn error_text(error: i32) -> String

返回一个给定错误的文本解释。

输入参数:

  • error: 错误代码
Source

pub fn event_text(event: *mut TSrvEvent) -> Result<String>

返回一个给定事件的文本解释。

输入参数:

  • event: 事件

返回值:

  • Ok: 操作成功
  • Err: 操作失败

Trait Implementations§

Source§

impl Default for S7Server

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for S7Server

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.