### Complete SoftPlc Integration Example Source: https://context7.com/fbarresi/softplc/llms.txt A comprehensive bash script demonstrating the full workflow of creating, writing to, reading from, and cleaning up datablocks using the SoftPlc API. ```bash #!/bin/bash # Complete SoftPlc integration test API_URL="http://localhost:8080" # 1. Create datablocks for different data types echo "Creating datablocks..." curl -X POST "$API_URL/api/datablocks?id=1&size=1024" # Process data curl -X POST "$API_URL/api/datablocks?id=10&size=256" # Sensor readings curl -X POST "$API_URL/api/datablocks?id=20&size=128" # Control flags # 2. List all created datablocks echo "Listing datablocks..." curl -s "$API_URL/api/datablocks" | jq '.' # 3. Write test data to datablocks echo "Writing data..." # Write process values (simulating temperatures: 25.5°C, 30.0°C) curl -X PUT "$API_URL/api/datablocks/1" \ -H "Content-Type: application/json" \ -d '[0, 0, 0, 25, 128, 0, 0, 30, 0]' # Write sensor readings curl -X PUT "$API_URL/api/datablocks/10" \ -H "Content-Type: application/json" \ -d '[255, 128, 64, 32, 16, 8, 4, 2, 1]' # 4. Read back and verify echo "Verifying data..." curl -s "$API_URL/api/datablocks/1" | jq '.data' # 5. Clean up test datablocks echo "Cleaning up..." curl -X DELETE "$API_URL/api/datablocks/10" curl -X DELETE "$API_URL/api/datablocks/20" # 6. Connect S7 client to port 102 for ISO-over-TCP communication # Use Sharp7 or other S7 client library to read/write datablocks ``` -------------------------------- ### Demo Datablock JSON Structure Source: https://context7.com/fbarresi/softplc/llms.txt Example structure for a datablock configuration file, including ID, size, and base64 encoded data. ```json // datablocks.json structure { "1": { "Data": "SGVsbG8sIFM3ISAgICAgIH+A...", "Id": 1, "Size": 2048 } } ``` -------------------------------- ### Retrieve Datablocks via API Source: https://context7.com/fbarresi/softplc/llms.txt Use the GET endpoint to list all registered datablocks or fetch a specific one by ID. ```bash # List all datablocks curl -X GET "http://localhost:8080/api/datablocks" \ -H "Accept: application/json" ``` ```bash # Get datablock with ID 1 curl -X GET "http://localhost:8080/api/datablocks/1" \ -H "Accept: application/json" ``` -------------------------------- ### GET /api/datablocks/{id} Source: https://context7.com/fbarresi/softplc/llms.txt Retrieves a specific datablock by its ID. Returns the complete datablock description including the binary data content as a Base64-encoded byte array. Returns 404 if the datablock does not exist. ```APIDOC ## GET /api/datablocks/{id} ### Description Retrieves a specific datablock by its ID. Returns the complete datablock description including the binary data content as a Base64-encoded byte array. Returns 404 if the datablock does not exist. ### Method GET ### Endpoint /api/datablocks/{id} ### Parameters #### Path Parameters - **id** (integer) - Required - The ID of the datablock to retrieve. #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **data** (string) - Base64-encoded byte array representing the datablock content. - **id** (integer) - The unique identifier of the datablock. - **size** (integer) - The allocated size of the datablock in bytes. #### Response Example ```json { "data": "SGVsbG8sIFM3ISAgICAgIH+A...", "id": 1, "size": 2048 } ``` #### Error Response (404) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.5", "title": "DB not found", "status": 404, "detail": "DB 99 not found." } ``` ``` -------------------------------- ### GET /api/datablocks Source: https://context7.com/fbarresi/softplc/llms.txt Retrieves information about all datablocks currently registered in the PLC server. Returns an array of datablock descriptions including their IDs, sizes, and binary data content. ```APIDOC ## GET /api/datablocks ### Description Retrieves information about all datablocks currently registered in the PLC server. Returns an array of datablock descriptions including their IDs, sizes, and binary data content. ### Method GET ### Endpoint /api/datablocks ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **data** (string) - Base64-encoded byte array representing the datablock content. - **id** (integer) - The unique identifier of the datablock. - **size** (integer) - The allocated size of the datablock in bytes. #### Response Example ```json [ { "data": "SGVsbG8sIFM3IQ==", "id": 1, "size": 2048 }, { "data": "AAAAAAAAAA==", "id": 10, "size": 256 } ] ``` ``` -------------------------------- ### Build and Run SoftPlc from Source Source: https://github.com/fbarresi/softplc/blob/master/README.md Steps to build the SoftPlc project from source code and run it. Ensure the native library (e.g., snap7.dll) is copied to the correct output directory. ```shell 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/" ``` -------------------------------- ### Build SoftPlc from Source Source: https://context7.com/fbarresi/softplc/llms.txt Steps to compile the application and configure the native Snap7 library for the target platform. ```bash cd SoftPlc dotnet restore dotnet build # Copy appropriate native library for your platform cp native/win/snap7.dll bin/Debug/net8.0/snap7.dll # Windows cp native/linux/libsnap7.so bin/Debug/net8.0/ # Linux cp native/arm/libsnap7.so bin/Debug/net8.0/ # ARM # Run with custom PLC port and API URL dotnet bin/Debug/net8.0/SoftPlc.dll --plcPort=102 --urls="http://localhost:8080/" ``` -------------------------------- ### Configure SoftPlc with Environment Variables Source: https://context7.com/fbarresi/softplc/llms.txt Demonstrates how to configure SoftPlc using environment variables, such as setting the data path for datablock configurations. ```bash # DATA_PATH: Directory for loading/saving datablock configurations # Default: Application directory docker run -e DATA_PATH=/custom/data -v /host/data:/custom/data fbarresi/softplc:latest-linux ``` ```bash # Command line arguments for source builds dotnet SoftPlc.dll --plcPort=102 --urls="http://0.0.0.0:8080/" ``` -------------------------------- ### Create Datablocks via API Source: https://context7.com/fbarresi/softplc/llms.txt Use the POST endpoint to initialize a new datablock with a specific ID and size. ```bash # Create a new datablock with ID 10 and size 256 bytes curl -X POST "http://localhost:8080/api/datablocks?id=10&size=256" # Create a larger datablock for complex data structures curl -X POST "http://localhost:8080/api/datablocks?id=100&size=4096" ``` -------------------------------- ### Deploy SoftPlc with Docker Source: https://context7.com/fbarresi/softplc/llms.txt Commands to run SoftPlc as a Docker container, including options for different platforms and preset demo data. ```bash # Run SoftPlc with Linux image docker run -p 8080:8080 -p 8443:443 -p 102:102 --name softplc fbarresi/softplc:latest-linux # Run with preset demo data (single 2048-byte datablock) docker run -p 8080:8080 -p 102:102 -e DATA_PATH=/demodata --name softplc fbarresi/softplc:latest-linux # Run with Windows container docker run -p 8080:8080 -p 102:102 --name softplc fbarresi/softplc:latest-win1809 ``` -------------------------------- ### Run SoftPlc using Docker Source: https://github.com/fbarresi/softplc/blob/master/README.md Instructions to run SoftPlc as a Docker container. This method allows for easy deployment and includes options for pre-loaded data. ```docker docker run -p 8080:8080 -p 8443:443 -p 102:102 --name softplc fbarresi/softplc:latest-linux ``` -------------------------------- ### POST /api/datablocks Source: https://context7.com/fbarresi/softplc/llms.txt Creates a new datablock with the specified ID and size. The datablock is initialized with zeros and registered with the PLC server for ISO-over-TCP access. Returns 409 Conflict if a datablock with the same ID already exists. ```APIDOC ## POST /api/datablocks ### Description Creates a new datablock with the specified ID and size. The datablock is initialized with zeros and registered with the PLC server for ISO-over-TCP access. Returns 409 Conflict if a datablock with the same ID already exists. ### Method POST ### Endpoint /api/datablocks ### Parameters #### Query Parameters - **id** (integer) - Required - The ID for the new datablock. - **size** (integer) - Required - The size of the new datablock in bytes. #### Request Body None ### Response #### Success Response (201) No content is returned on successful creation. #### Error Response (409) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example (409 Conflict) ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.10", "title": "DB already exists", "status": 409, "detail": "DB 10 already exists." } ``` #### Error Response (400) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example (400 Bad Request) ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1", "title": "Invalid DB size", "status": 400, "detail": "DB size must be > 0 but was 0." } ``` ``` -------------------------------- ### Demo Data Byte Layout Description Source: https://context7.com/fbarresi/softplc/llms.txt Describes the byte layout of a demo datablock, detailing the data types and their positions. ```text // Demo data byte layout (0-based indices): // Bytes 0-15: ASCII string "Hello, S7!" with trailing spaces // Bytes 16-17: Signed byte max/min (127/-128) // Bytes 32-35: Signed short max/min // Bytes 48-55: Signed integer max/min // Bytes 64-79: Signed long max/min // Bytes 80-87: Float max/min // Bytes 96-111: Double max/min // Bytes 1024-2047: Sequential bytes 0-255 (repeated) ``` -------------------------------- ### DatablockDescription C# Model Source: https://context7.com/fbarresi/softplc/llms.txt Defines the C# class representing a PLC datablock, including its binary data, ID, and size. ```csharp public class DatablockDescription { public byte[] Data; // Binary content of the datablock public int Id { get; } // Unique identifier for the datablock public int Size { get; } // Allocated size in bytes } ``` ```json // JSON representation in API responses: // { // "data": "base64-encoded-bytes", // "id": 1, // "size": 2048 // } ``` -------------------------------- ### Update Datablock Content via API Source: https://context7.com/fbarresi/softplc/llms.txt Use the PUT endpoint to write a JSON byte array to an existing datablock. ```bash # Update datablock content with byte array (JSON format) curl -X PUT "http://localhost:8080/api/datablocks/1" \ -H "Content-Type: application/json" \ -d '[72, 101, 108, 108, 111, 44, 32, 83, 55, 33]' # Write specific values to simulate sensor data curl -X PUT "http://localhost:8080/api/datablocks/10" \ -H "Content-Type: application/json" \ -d '[0, 100, 0, 50, 255, 0, 128, 64]' ``` -------------------------------- ### PUT /api/datablocks/{id} Source: https://context7.com/fbarresi/softplc/llms.txt Updates the data content of an existing datablock. Accepts a JSON byte array in the request body. The provided data must not exceed the datablock's allocated size. Returns 404 if the datablock does not exist. ```APIDOC ## PUT /api/datablocks/{id} ### Description Updates the data content of an existing datablock. Accepts a JSON byte array in the request body. The provided data must not exceed the datablock's allocated size. Returns 404 if the datablock does not exist. ### Method PUT ### Endpoint /api/datablocks/{id} ### Parameters #### Path Parameters - **id** (integer) - Required - The ID of the datablock to update. #### Query Parameters None #### Request Body - **data** (array of integers) - Required - A JSON array representing the byte data to write to the datablock. ### Response #### Success Response (204) No content is returned on successful update. #### Error Response (404) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example (404 Not Found) ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.5", "title": "DB not found", "status": 404, "detail": "DB 99 not found." } ``` #### Error Response (400) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example (400 Bad Request) ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1", "title": "Data exceeds DB length", "status": 400, "detail": "Data with 500 bytes exceeds length of DB 1 of 256 bytes." } ``` ``` -------------------------------- ### DELETE /api/datablocks/{id} Source: https://context7.com/fbarresi/softplc/llms.txt Deletes a specific datablock by its ID. Returns 404 if the datablock does not exist. ```APIDOC ## DELETE /api/datablocks/{id} ### Description Deletes a specific datablock by its ID. Returns 404 if the datablock does not exist. ### Method DELETE ### Endpoint /api/datablocks/{id} ### Parameters #### Path Parameters - **id** (integer) - Required - The ID of the datablock to delete. #### Query Parameters None #### Request Body None ### Response #### Success Response (204) No content is returned on successful deletion. #### Error Response (404) - **type** (string) - URI that identifies the error type. - **title** (string) - A short, human-readable summary of the error. - **status** (integer) - The HTTP status code. - **detail** (string) - A human-readable explanation specific to this occurrence of the problem. #### Error Response Example (404 Not Found) ```json { "type": "https://tools.ietf.org/html/rfc9110#section-15.5.5", "title": "DB not found", "status": 404, "detail": "DB 99 not found." } ``` ``` -------------------------------- ### Delete Datablock API Request Source: https://context7.com/fbarresi/softplc/llms.txt Use this endpoint to remove a datablock from the PLC server. Returns 404 if the datablock is not found. ```bash # Delete datablock with ID 10 curl -X DELETE "http://localhost:8080/api/datablocks/10" ``` ```bash # Verify deletion by listing all datablocks curl -X GET "http://localhost:8080/api/datablocks" ``` ```json # Error: Deleting non-existent datablock (HTTP 404) # { # "type": "https://tools.ietf.org/html/rfc9110#section-15.5.5", # "title": "DB not found", # "status": 404, # "detail": "DB 99 not found." # } ``` -------------------------------- ### DELETE /api/datablocks/{id} Source: https://context7.com/fbarresi/softplc/llms.txt Deletes a specific datablock from the PLC server and unregisters it from the ISO-over-TCP communication area. ```APIDOC ## DELETE /api/datablocks/{id} ### Description Deletes a datablock from the PLC server and unregisters it from the ISO-over-TCP communication area. Returns 404 if the datablock does not exist. ### Method DELETE ### Endpoint /api/datablocks/{id} ### Parameters #### Path Parameters - **id** (integer) - Required - The unique identifier of the datablock to delete. ### Response #### Success Response (200) - **status** (integer) - Indicates successful deletion. #### Error Response (404) - **type** (string) - RFC 9110 reference. - **title** (string) - Error title (e.g., "DB not found"). - **status** (integer) - 404. - **detail** (string) - Descriptive error message. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.