# Siemens S7 Write

This function block is used to send values into a Siemens S7 PLC (DB area). It lets you pick the DB number, byte address, data type and optionally a bit position for boolean writes. You can choose immediate (synchronous) writes or background (asynchronous) writes and provide an optional value to be sent when the scenario stops.

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

`Enable` Boolean input that enables the block. When False, the block will not send data.

`Data` Generic input containing the value to write to the PLC (number, boolean, text, depending on the selected data type).

`[Optional] Data on Stop` Generic input to provide a value that will be written when the block stops or the scenario finishes.

`S7 Client` Generic input that accepts a PLC connection reference (use with `Siemens S7 Connect` or a shared client provider).

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

This block does not produce outputs.

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

`DB Number` DB number in the PLC where data will be written.

`DB Byte Address` Byte offset inside the DB where the write will start.

`Bit Position` Bit index inside the target byte (used only for boolean writes).

`DB Data Type` Dropdown to select the data type to write (examples: Boolean, Int, DInt, Real, String, Time, ...).

`Connection Type` Dropdown to choose between `Async` and `Sync` write modes.

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

* Background writing option: choose `Async` to queue writes and run them in the background for better throughput.
* Immediate writes: choose `Sync` to write immediately (recommended when multiple blocks write to the same bytes).
* Wide data type support: supports numeric types, strings, boolean writes (with bit selection), and time types.
* Optional stop-time write: specify a separate value to be written when the block is stopped or removed.
* Automatic client reconnection guidance: the block can be paired with a connection provider to manage PLC connections.

## ⚙️ Running mechanism (user-facing) <a href="#running-mechanism" id="running-mechanism"></a>

* Provide a valid `S7 Client` from a connection block such as `Siemens S7 Connect`.
* Send values into `Data` while `Enable` is True to perform writes.
* If `Connection Type` is set to `Async`, writes are queued and executed on a background worker so the rest of the flow continues without waiting.
* If `Connection Type` is set to `Sync`, writes happen immediately and the block waits until the write finishes. This is safer when multiple writers target the same bytes.
* If you provide `[Optional] Data on Stop`, that value will be written when the block is stopped or removed.

## 📝 How to use (simple) <a href="#usage" id="usage"></a>

1. Add a connection provider block like `Siemens S7 Connect` and connect its client output to `S7 Client`.
2. Enter the destination DB number in `DB Number` and the byte offset in `DB Byte Address`.
3. Select the correct `DB Data Type` for the value you intend to write. If writing a boolean, set `Bit Position`.
4. Provide the value to be written to the `Data` input (use `Number Input`, `String Input` or other producers).
5. Toggle `Enable` (or drive it with a trigger block such as `Logic Input` or `Rising Edge`) to perform the write.
6. Optionally set `[Optional] Data on Stop` if you need a final value to be written when stopping.

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

* For connection setup, pair with `Siemens S7 Connect` so the connection is shared and managed separately.
* Use `Siemens S7 Read` after this block to verify the written value (read-then-compare).
* Use `Logic Input` or `Rising Edge` to trigger single-shot writes when an event occurs instead of continuous writes.
* Use `Number Input` or `String Input` blocks to feed clean, user-editable values into `Data`.
* Choose `Sync` when multiple blocks may write to overlapping DB addresses to avoid conflicting updates. Use `Async` for higher throughput when writes target different areas.
* Combine with `Data Memory` to hold a value and only send when it changes (prevents unnecessary writes).
* Add a `Debug Input` or connect a logger (`CSV Export` or `Image Logger` where appropriate) to keep a write history for troubleshooting.
* If you want to publish write events externally, route the same value to `MQTT Publish` or `REST API - Post` blocks after a successful write.

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

* No connection / write fails: check that a valid `S7 Client` is connected (use `Siemens S7 Connect`).
* Wrong data type: ensure the selected `DB Data Type` matches the type of the `Data` you provide (e.g., do not feed text when a numeric type is selected).
* Boolean not changing: confirm the correct `Bit Position` is set and that the DB byte address is correct.
* Conflicting writes: if multiple blocks write to the same byte(s) and results are inconsistent, switch to `Sync` or consolidate writes into a single block.
* Need a guaranteed final write: provide the final value to `[Optional] Data on Stop` so it will be written when the block is removed or the scenario stops.

If you need example combinations, try: `Number Input` → `Data` and `Logic Input` → `Enable` for manual writes, or `Siemens S7 Read` → verify written values after using this block.
