# TwinCAT Write

This function block writes a value to a TwinCAT PLC variable using an existing PLC connection. Use it to push numeric, boolean or textual values from your scenario into the PLC and receive a simple success indicator.

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

* `Connection` — PLC connection object (provide from a communication setup block such as TwinCAT Connect).
* `Value` — The value to be written into the PLC variable (number, text, boolean, or structured value depending on your PLC variable).

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

* `Success` — Boolean indicating whether the write operation succeeded.

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

* `Variable Name` — Enter the exact PLC variable name/address you want to write to (as defined in your TwinCAT project).

## ⚙️ Running mechanism <a href="#running-mechanism" id="running-mechanism"></a>

When this block runs it:

1. Reads the provided `Connection` and `Value` inputs and the `Variable Name` control.
2. Validates that a variable name, a valid connection, and a value are present.
3. Attempts to write the provided value into the specified PLC variable via the supplied connection.
4. Emits `Success` as True when the write completes, otherwise False and logs an error message for troubleshooting.

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

* Simple single-variable write operation with a clear single boolean result.
* Input-flexible: accepts generic values so it can be used with numeric, textual and boolean data sources.
* Error reporting: informs you when connection, variable name or value is missing or when a write fails.

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

1. Add a PLC connection block such as TwinCAT Connect and configure it.
2. Feed the connection output into the `Connection` input of this block.
3. Provide a value to write using a suitable input block (for example a Number Input or String Input) into the `Value` input.
4. Enter the PLC variable name in the `Variable Name` control.
5. Run the scenario and check the `Success` output to confirm the write.

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

* Use `TwinCAT Connect` to create and supply the `Connection` input.
* Use `Number Input`, `String Input` or `Logic Input` to prepare the `Value` input depending on the variable type in the PLC.
* After writing, verify the result by reading the same variable with `TwinCAT Read`.
* If your value type is unclear, pass the data through `Data Type Converter` before writing to ensure type compatibility.
* Add a `Debug Input` or `Is None` check upstream to detect missing or invalid values and avoid failed writes.
* Log successful writes or failures using CSV Export or MQTT Publish for audit trails or remote monitoring.

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

* If `Success` is False: verify the `Variable Name` is exactly as defined in the PLC (including namespaces/paths).
* Ensure the provided `Connection` is active and correctly configured (check TwinCAT Connect status).
* Confirm the value type matches the PLC variable type; convert types upstream if necessary.
* Check logs shown by the block for descriptive error messages to guide corrective actions.


---

# 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/twincat-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.
