SoftPlc

repository·master·Indexed 41 days ago

https://github.com/fbarresi/softplc

A software-based Programmable Logic Controller (PLC) for simulating PLC behavior. It implements the ISO-over-TCP protocol (RFC 1006) on port 102 and provides a Web API for managing DataBlocks.

Tokens
669
Snippets
2
Records
5
Agent score
50%

What's inside SoftPlc

  1. Understand the structure of datablocks.json for S7 server presets

    master

    The datablocks.json file is used to initialize a preset S7 server with specific data. When constructing or interpreting this file, note that all byte indices are 0-based. The file contains a sequence of data types and values used to simulate PLC memory blocks.

    Data layout in datablocks.json:

    • Bytes 0-15: An ASCII encoded String (e.g., "Hello, S7!" with trailing spaces).
    • Bytes 16-17: Max/min values of a signed byte (127 / -128).
    • Bytes 32-35: Max/min values of a signed short.
    • Bytes 48-55: Max/min values of a signed integer.
    • Bytes 64-79: Max/min values of a signed long.
    • Bytes 80-87: Max/min values of a float.
    • Bytes 96-111: Max/min values of a double.
    • Bytes 1024-2048: A continuous sequence of incrementing bytes.
  2. How SoftPlc works

    master

    SoftPlc is a software-based Programmable Logic Controller (PLC) controlled via a Web API. It provides two main interfaces:

    1. Simulated PLC: Listens on port 102 using the ISO-over-TCP protocol (RFC 1006). By default, it starts with no data. To start with a preset 2048-byte DataBlock, use the DATA_PATH environment variable.
    2. Web API: Listens at http://localhost:8080/ (with Swagger documentation available at the same endpoint). This API allows you to add, read, modify, and delete DataBlocks.
  3. Run SoftPlc from source code

    master

    To run SoftPlc from the source, you must restore, build, and ensure the required native library (e.g., snap7.dll) is copied to the output directory before execution.

    Note: The example below uses net8.0 for the execution path, ensure this matches your build output.

    cd SoftPlc
    dotnet restore
    dotnet build
    cp native\win\snap7.dll bin\x64\Debug\net6.0\snap7.dll
    dotnet bin\Debug\net8.0\SoftPlc.dll --plcPort=102 --urls="http://localhost:8080/"
  4. Use a PLC Client with SoftPlc

    master

    If you need to interact with SoftPlc from a C# application, it is recommended to use the following libraries:

    • Sharp7: A PLC client implementation.
    • Sharp7Reactive: A reactive version of the Sharp7 client.
  5. Run SoftPlc with Docker

    master

    You can run SoftPlc using a Docker image. Pull the appropriate tag for your platform (e.g., latest-linux or latest-win1809 for MOBY support) and bind the necessary ports.

    To start with preset data, add the environment variable -e DATA_PATH=/demodata to include a single 2048-byte DataBlock.

    docker run -p 8080:8080 -p 8443:443 -p 102:102 --name softplc fbarresi/softplc:latest-linux