Client

Snap7 client used for connection to a siemens7 server.

class snap7.client.Client[source]

A snap7 client

ab_read(start: int, size: int) → bytearray[source]

This is a lean function of Cli_ReadArea() to read PLC process outputs.

ab_write(start: int, data: bytearray) → int[source]

This is a lean function of Cli_WriteArea() to write PLC process outputs

as_ab_read(start: int, size: int, data) → int[source]

This is the asynchronous counterpart of client.ab_read().

as_ab_write(start: int, data: bytearray) → int[source]

This is the asynchronous counterpart of Cli_ABWrite.

as_compress(time: int) → int[source]

This is the asynchronous counterpart of client.compress().

as_db_get(db_number: int, _buffer, size) → bytearray[source]

This is the asynchronous counterpart of Cli_DBGet.

as_download(data: bytearray, block_num: int) → int[source]

Downloads a DB data into the AG asynchronously. A whole block (including header and footer) must be available into the user buffer.

Parameters:
  • block_num – New Block number (or -1)
  • data – the user buffer
as_read_area(area: str, dbnumber: int, start: int, size: int, wordlen: int, pusrdata) → int[source]

This is the main function to read data from a PLC. With it you can read DB, Inputs, Outputs, Merkers, Timers and Counters.

Parameters:
  • area – chosen memory_area
  • dbnumber – The DB number, only used when area= S7AreaDB
  • start – offset to start writing
  • size – number of units to read
  • pusrdata
  • wordlen
as_write_area(area: str, dbnumber: int, start: int, size: int, wordlen: int, pusrdata) → int[source]

This is the main function to write data into a PLC. It’s the complementary function of Cli_ReadArea(), the parameters and their meanings are the same. The only difference is that the data is transferred from the buffer pointed by pUsrData into PLC.

Parameters:
  • area – chosen memory_area
  • dbnumber – The DB number, only used when area= S7AreaDB
  • start – offset to start writing
check_as_completion(p_value) → int[source]

Method to check Status of an async request. Result contains if the check was successful, not the data value itself :param p_value: Pointer where result of this check shall be written. :return: 0 - Job is done successfully :return: 1 - Job is either pending or contains s7errors

create()[source]

create a SNAP7 client.

db_get(db_number: int) → bytearray[source]

Uploads a DB from AG.

db_read(db_number: int, start: int, size: int) → bytearray[source]

This is a lean function of Cli_ReadArea() to read PLC DB.

Returns:user buffer.
delete(block_type: str, block_num: int) → int[source]

Deletes a block

Parameters:
  • block_type – Type of block
  • block_num – Bloc number
destroy() → Optional[int][source]

destroy a client.

full_upload(_type: str, block_num: int) → Tuple[bytearray, int][source]

Uploads a full block body from AG. The whole block (including header and footer) is copied into the user buffer.

Parameters:block_num – Number of Block
get_block_info(blocktype: str, db_number: int) → snap7.types.TS7BlockInfo[source]

Returns the block information for the specified block.

get_connected() → bool[source]

Returns the connection status

Returns:a boolean that indicates if connected.
get_cpu_info() → snap7.types.S7CpuInfo[source]

Retrieves CPU info from client

get_cpu_state() → str[source]

Retrieves CPU state from client

get_param(number: int) → int[source]

Reads an internal Client object parameter.

get_pdu_length() → int[source]

Returns info about the PDU length.

get_plc_datetime() → datetime.datetime[source]

Get date and time from PLC.

Returns:date and time as datetime
list_blocks() → snap7.types.BlocksList[source]

Returns the AG blocks amount divided by type.

Returns:a snap7.types.BlocksList object.
list_blocks_of_type(blocktype, size: int) → Union[int, _ctypes.Array][source]

This function returns the AG list of a specified block type.

plc_cold_start() → int[source]

cold starts a client

plc_hot_start() → int[source]

hot starts a client

plc_stop() → int[source]

stops a client

read_area(area: str, dbnumber: int, start: int, size: int) → bytearray[source]

This is the main function to read data from a PLC. With it you can read DB, Inputs, Outputs, Merkers, Timers and Counters.

Parameters:
  • dbnumber – The DB number, only used when area= S7AreaDB
  • start – offset to start writing
  • size – number of units to read
read_multi_vars(items) → Tuple[int, snap7.types.S7DataItem][source]

This function read multiple variables from the PLC.

Parameters:items – list of S7DataItem objects
Returns:a tuple with the return code and a list of data items
set_connection_params(address: str, local_tsap: int, remote_tsap: int)[source]

Sets internally (IP, LocalTSAP, RemoteTSAP) Coordinates. This function must be called just before Cli_Connect().

Parameters:
  • address – PLC/Equipment IPV4 Address, for example “192.168.1.12”
  • local_tsap – Local TSAP (PC TSAP)
  • remote_tsap – Remote TSAP (PLC TSAP)
set_connection_type(connection_type: int)[source]

Sets the connection resource type, i.e the way in which the Clients connects to a PLC.

Parameters:connection_type – 1 for PG, 2 for OP, 3 to 10 for S7 Basic
upload(block_num: int) → bytearray[source]

Uploads a block body from AG

Parameters:block_num – bytearray
wait_as_completion(timeout: int) → int[source]

Snap7 Cli_WaitAsCompletion representative. :param timeout: ms to wait for async job :return: Result of request in int format

snap7.client.error_wrap(func)[source]

Parses a s7 error code returned the decorated function.