# Siemens S7 Write

This function block writes data to a Siemens S7 PLC (Data Block). Use it to send numbers, booleans, strings and other supported types into a PLC address directly from your scenario.

## 📥 Inputs <a href="#inputs" id="inputs"></a>

`Enable` Provide TRUE to allow writing. If FALSE or empty no write will occur.

`Data` The value to write to the PLC. Accepts numeric, boolean, text or complex data depending on the selected `DB Data Type`.

`[Optional] Data on Stop` If provided, this value will be written once when the scenario or block is stopped/removed.

`S7 Client` Provide a connection reference from a connection block (for example `Siemens S7 Connect`) so the block can reach the PLC.

## 📤 Outputs <a href="#outputs" id="outputs"></a>

This function block does not produce outputs.

## 🕹️ Controls <a href="#controls" id="controls"></a>

`DB Number` Data Block number to write into on the PLC.

`DB Byte Address` Starting byte address inside the chosen DB where the value will be written.

`Bit Position` Use this only for boolean writes to select which bit inside the target byte should be set.

`DB Data Type` Choose the data type to write (examples: Boolean, Int, Real, String, Time, etc.). The choice controls how the input value is encoded on the PLC side.

`Connection Type` Choose `Async` or `Sync`. `Async` schedules the write to a background worker; `Sync` performs the write immediately.

## 🎯 Features <a href="#features" id="features"></a>

* Write a wide range of PLC data types including numerical types, booleans, and strings.
* Optional "write on stop" value that will be pushed to the PLC when the block or scenario is stopped.
* Two connection modes: synchronous immediate write and asynchronous background write to avoid blocking the main flow.
* Automatic reconnection support when the provided client address changes (useful when reorganizing connections).
* Simple UI controls for DB, byte address and bit position — no coding required.

## ⚙️ Running Mechanism <a href="#how-it-works" id="how-it-works"></a>

* When `Enable` is TRUE and a valid `S7 Client` is provided, the block attempts to write the `Data` to the selected DB and byte address using the chosen `DB Data Type`.
* If `Connection Type` is set to `Async`, the write is scheduled to run in a background worker so the main scenario can continue without delay.
* If `Connection Type` is set to `Sync`, the block performs the write as part of the current execution step.
* If a value is provided in `[Optional] Data on Stop`, that value will be written automatically when the block is stopped or removed.
* The block logs informative messages on success, warnings when the PLC/client is busy, and errors when writes fail or parameters are invalid.

## 📝 Usage Instructions <a href="#usage" id="usage"></a>

1. Add a connection block such as `Siemens S7 Connect` and configure the PLC address.
2. Feed that connection into the `S7 Client` input of this block.
3. Set the `DB Number`, `DB Byte Address`, and, if writing a boolean, the `Bit Position`.
4. Select the proper `DB Data Type` for the value you will write.
5. Choose `Connection Type`:
   * Use `Sync` for immediate writes or when few writes are performed.
   * Use `Async` to offload frequent or slow writes to a background worker.
6. Provide a value to the `Data` input (for manual input you can use blocks like `Number Input` or `String Input`).
7. Enable the block by providing TRUE to `Enable` to perform the write.
8. Optionally provide a value to `[Optional] Data on Stop` to be written when the block is stopped.

## 💡 Tips and Tricks <a href="#tips-and-tricks" id="tips-and-tricks"></a>

* Use `Siemens S7 Connect` together with this block to manage the PLC connection and provide the `S7 Client` input.
* After writing, verify values with `Siemens S7 Read` to confirm the expected DB contents.
* For manual triggers, combine with `Logic Input` or `Number Input` to control when writes occur.
* When writing textual values, feed text from `String Input` so encoding matches expectations.
* If you need to store values for later use or coordinate multiple blocks, consider combining with `Data Write Local` / `Data Read Local` or `Data Write Global` / `Data Read Global`.
* Use `Debug Input` to log or inspect values before sending them to the PLC.
* Prefer `Sync` mode when multiple blocks must write to the same exact bytes — it reduces race conditions. Use `Async` for higher throughput when writes are independent.

## 🛠️ Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* Connection errors or "cannot write" messages:
  * Confirm the `S7 Client` input is connected and the connection block is configured with the correct PLC address and active.
  * Try re-establishing the connection from the connection block.
* Wrong values seen on PLC:
  * Verify `DB Number`, `DB Byte Address` and `Bit Position` are correct for the target variable.
  * Ensure the selected `DB Data Type` matches the PLC variable type (e.g., writing a float with an integer type will produce incorrect values).
* String writes truncated or corrupted:
  * Check the PLC side expects a length byte or fixed-size buffer. Limit string length accordingly.
* Writes are too slow or block the scenario:
  * Switch `Connection Type` to `Async` so writes occur in the background.
* Multiple writers to same address cause conflicts:
  * Use `Sync` mode for blocks writing to the same DB/bytes, or centralize writes using a single dedicated writer block.

If problems persist, re-check address mapping on the PLC and validate with a read operation (for example using `Siemens S7 Read`) before relying on subsequent writes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.augelab.com/function-blocks/input-output/communication/siemens-s7-write.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
