Snapex7 Logo


Valiot Logo

Snapex7 is an Elixir wrapper for the Snap7 library. Snap7 i san open source, 32/64 bit, multi-platform Ethernet communication suite for interfacing natively with Siemens S7 PLCs.

Content

Installation

The package can be installed by adding snapex7 to your list of dependencies in mix.exs:

def deps do
  [
    {:snapex7, "~> 0.1.2"}
  ]
end

Fetch the dependency by running mix deps.get in your terminal.

Target Compatibility

Many hardware components equipped with an Ethernet port can communicate via the S7 protocol. Check compatibility with the component that you are using:

Here is a summary compatibility table from the documentation above:

Compatibility

Using Snapex7

Start a Snapex7.Client and connect it to the PLC's IP, where:

iex> {:ok, pid} = Snapex7.Client.start_link()
iex> Snapex7.Client.connect_to(pid, ip: "192.168.0.1", rack: 0, slot: 1)

Connection functions

iex> :ok = Snapex7.Client.connect_to(pid, ip: "192.168.0.1", rack: 0, slot: 1)

Data IO functions

  iex> {:ok, resp_binary} =
          Snapex7.Client.read_area(pid,
            area: :DB,
            word_len: :byte,
            start: 2,
            amount: 4,
            db_number: 1
          )
  iex> :ok =
          Snapex7.Client.write_area(pid,
            area: :DB,
            word_len: :byte,
            start: 2,
            amount: 4,
            db_number: 1,
            data: <<0x42, 0xCB, 0x00, 0x00>>
          )

Error format

When a response returns an error, it will have the following format:

iex> {:error, %{eiso: nil, es7: nil, etcp: 113}}

See Snap7-refman.pdf pg.252 for further error info.

Types

Arguments definition for each particular memory block:

  @block_types [
    OB: 0x38,  # Organization block
    DB: 0x41,  # Data block
    SDB: 0x42, # System data block
    FC: 0x43,  # Function
    SFC: 0x44, # System function
    FB: 0x45,  # Functional block
    SFB: 0x46  # System functional block
  ]

  @connection_types [
    PG: 0x01,      # Programming console
    OP: 0x02,      # Siemens HMI panel
    S7_basic: 0x03 # Generic data transfer connection
  ]

  @area_types [
    PE: 0x81, # Process Inputs
    PA: 0x82, # Process Outputs
    MK: 0x83, # Merkers
    DB: 0x84, # DB
    CT: 0x1C, # Counters
    TM: 0x1D  # Timers
  ]

  @word_types [
    bit: 0x01,
    byte: 0x02,
    word: 0x04,
    d_word: 0x06,
    real: 0x08,
    counter: 0x1C,
    timer: 0x1D
  ]

Further documentation and examples

Snapex7 has further client functions implementation which can be found at Snapex7 Hexdocs.

Contributing to this Repo

If you wish to clone this Repo use:

git clone --recursive git@github.com:valiot/snap7.git

License

See LICENSE.

TODO