Skip to main content

S7Client

Struct S7Client 

Source
pub struct S7Client<T: AsyncRead + AsyncWrite + Unpin + Send> { /* private fields */ }

Implementations§

Source§

impl<T: AsyncRead + AsyncWrite + Unpin + Send> S7Client<T>

Source

pub async fn from_transport(transport: T, params: ConnectParams) -> Result<Self>

Source

pub fn request_timeout(&self) -> Duration

Return the current request timeout.

Source

pub async fn set_request_timeout(&self, timeout: Duration)

Update the request timeout at runtime.

This affects subsequent recv_s7 calls made by this client instance.

Source

pub fn get_param(&self, name: &str) -> Result<Duration>

Read a client parameter by name.

Supported names: "request_timeout", "connect_timeout", "pdu_size".

Source

pub fn set_param(&mut self, name: &str, value: Duration) -> Result<()>

Set a client parameter at runtime.

Supported names: "request_timeout" (Duration).

Source

pub async fn db_read(&self, db: u16, start: u32, length: u16) -> Result<Bytes>

Source

pub async fn read_multi_vars( &self, items: &[MultiReadItem], ) -> Result<Vec<Bytes>>

Read multiple PLC regions in one or more S7 PDU exchanges.

Automatically batches items when the item count would exceed the Siemens hard limit of 20 per PDU, or when the encoded request or response would exceed the negotiated PDU size. Returns one Bytes per item in input order.

Unlike db_read, this accepts any Area and TransportSize.

Source

pub async fn write_multi_vars(&self, items: &[MultiWriteItem]) -> Result<()>

Write multiple PLC regions in one or more S7 PDU exchanges.

Automatically batches items when the count or encoded size would exceed the negotiated PDU size or the Siemens hard limit of 20 items per PDU. Returns Ok(()) only when all items are acknowledged with return code 0xFF.

Source

pub async fn db_write(&self, db: u16, start: u32, data: &[u8]) -> Result<()>

Source

pub async fn ab_read( &self, area: Area, db_number: u16, start: u32, length: u16, ) -> Result<Bytes>

Read from any PLC area using absolute addressing.

A convenience wrapper around read_multi_vars for a single area read.

Source

pub async fn ab_write( &self, area: Area, db_number: u16, start: u32, data: &[u8], ) -> Result<()>

Write to any PLC area using absolute addressing.

A convenience wrapper around write_multi_vars for a single area write.

Source

pub async fn read_szl(&self, szl_id: u16, szl_index: u16) -> Result<SzlResponse>

Source

pub async fn read_clock(&self) -> Result<PlcDateTime>

Source

pub async fn copy_ram_to_rom(&self) -> Result<()>

Copy RAM data to ROM (function 0x43).

Copies the CPU’s work memory to its load memory (retain on power-off).

Source

pub async fn compress(&self) -> Result<()>

Compress the PLC work memory (function 0x42).

Reorganises memory to eliminate fragmentation. The PLC must be in STOP mode before calling this.

Source

pub async fn plc_stop(&self) -> Result<()>

Stop the PLC (S7 function code 0x29).

Sends a Job request with no additional data. Returns Ok(()) when the PLC acknowledges the command, or an error if the PLC rejects it (e.g., password-protected or CPU in a non-stoppable state).

Source

pub async fn plc_hot_start(&self) -> Result<()>

Hot-start (warm restart) the PLC (S7 function code 0x28).

A warm restart retains the DB content and retentive memory.

Source

pub async fn plc_cold_start(&self) -> Result<()>

Cold-start (full restart) the PLC (S7 function code 0x2A).

A cold start clears all DBs and non-retentive memory.

Source

pub async fn get_plc_status(&self) -> Result<PlcStatus>

Read the current PLC status (S7 function code 0x31).

Returns one of [PlcStatus::Run], [PlcStatus::Stop], or [PlcStatus::Unknown].

Source

pub async fn get_order_code(&self) -> Result<OrderCode>

Read the PLC order code (SZL ID 0x0011).

The order code is a 20-character ASCII string (e.g. "6ES7 317-2EK14-0AB0").

Source

pub async fn get_cpu_info(&self) -> Result<CpuInfo>

Read detailed CPU information (SZL ID 0x001C).

Returns module type, serial number, plant identification, copyright and module name fields pre-parsed from the SZL response.

Source

pub async fn get_cp_info(&self) -> Result<CpInfo>

Read communication processor information (SZL ID 0x0131).

Returns maximum PDU length, connection count, and baud rates.

Source

pub async fn read_module_list(&self) -> Result<Vec<ModuleEntry>>

Read the rack module list (SZL ID 0x00A0).

Each entry is a 2-byte module type identifier.

Source

pub async fn list_blocks(&self) -> Result<BlockList>

List all blocks in the PLC grouped by type (SZL 0x0130).

Returns a [BlockList] with the total block count and per-type entries.

Source

pub async fn get_ag_block_info( &self, block_type: u8, block_number: u16, ) -> Result<BlockInfo>

Get detailed information about a block stored on the PLC.

block_type should be one of the BlockType discriminant values (e.g. 0x41 for DB, 0x38 for OB).

Source

pub async fn get_pg_block_info( &self, block_type: u8, block_number: u16, ) -> Result<BlockInfo>

Get detailed block information from the PG perspective.

Same fields as get_ag_block_info but the information is from the programming-device viewpoint.

Source

pub async fn set_session_password(&self, password: &str) -> Result<()>

Set a session password for protected PLC access.

The password is obfuscated using the S7 nibble-swap + XOR-0x55 algorithm and sent as a Job PDU with function code 0x12. Passwords longer than 8 bytes are truncated.

Source

pub async fn clear_session_password(&self) -> Result<()>

Clear the session password on the PLC (function code 0x11).

Source

pub async fn get_protection(&self) -> Result<Protection>

Read the current protection level (SZL ID 0x0032, index 0x0004).

Returns the protection scheme identifiers and level; password_set is true when the PLC reports a non-empty password.

Source

pub async fn delete_block( &self, block_type: u8, block_number: u16, ) -> Result<()>

Delete a block from the PLC (S7 function code 0x1F).

Source

pub async fn upload(&self, block_type: u8, block_number: u16) -> Result<Vec<u8>>

Upload a PLC block via S7 PI-Upload (function 0x1D).

Returns the raw block bytes in Diagra format (20-byte header + payload). Use [BlockData::from_bytes] to parse the result.

Source

pub async fn db_get(&self, db_number: u16) -> Result<Vec<u8>>

Upload a DB block (convenience wrapper around upload).

Source

pub async fn download( &self, block_type: u8, block_number: u16, data: &[u8], ) -> Result<()>

Download a block to the PLC (S7 function 0x1E).

data should be in Diagra format (20-byte header + payload, as returned by upload or built via [BlockData::to_bytes]).

Source

pub async fn db_fill(&self, db_number: u16, value: u8) -> Result<()>

Fill a DB with a constant byte value.

Uses get_ag_block_info to determine the DB size, then writes every byte to value.

Source§

impl S7Client<TcpTransport>

Source

pub async fn connect(addr: SocketAddr, params: ConnectParams) -> Result<Self>

Source§

impl S7Client<UdpTransport>

Source

pub async fn connect_udp( addr: SocketAddr, params: ConnectParams, ) -> Result<Self>

Connect to a PLC using UDP transport.

Auto Trait Implementations§

§

impl<T> !Freeze for S7Client<T>

§

impl<T> !RefUnwindSafe for S7Client<T>

§

impl<T> Send for S7Client<T>

§

impl<T> Sync for S7Client<T>

§

impl<T> Unpin for S7Client<T>

§

impl<T> UnsafeUnpin for S7Client<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for S7Client<T>
where T: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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.