# TwinCAT Connect

This function block establishes a connection to a TwinCAT PLC so other blocks can read from or write to the PLC. Use it to create and maintain the PLC client connection and to report connection status to the rest of your scenario.

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

This function block does not have any input sockets.

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

`Connection` The active connection handle (use with downstream communication blocks).

`Success` Boolean indicating whether a successful connection to the PLC was established.

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

`AMS ID` Enter the PLC AMS identifier (address). This is typically provided by the PLC configuration or your system administrator.

`Port` Enter the port number for the TwinCAT connection. A default common port is prefilled (you can change it if needed).

`Timeout (ms)` Set the connection timeout in milliseconds. Use larger values for slower or remote networks.

Note: If the required TwinCAT support libraries are not present on the system, the block will show a message in its layout indicating that TwinCAT modules are not available and the block cannot connect until required components are installed.

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

* Persistent connection output for downstream blocks to reuse without re-initializing.
* Displays connection success as a boolean to enable conditional workflows.
* Simple, labeled controls to set AMS ID, port and timeout values.
* Gracefully reports missing system dependencies so you know when TwinCAT components are required.

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

* When the block runs it attempts to establish a PLC connection using the configured `AMS ID`, `Port` and `Timeout (ms)`.
* On success it provides the established `Connection` via its output and sets `Success` to true. On failure it provides a null/empty connection and sets `Success` to false.
* The connection output can be left connected to other communication blocks so they can use the same established link without re-creating connections repeatedly.
* The block also reports clear error messages in the UI if configuration is missing or system dependencies are not available.

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

1. Enter your PLC address into `AMS ID` and the target port into `Port`.
2. Adjust `Timeout (ms)` if your network is slow or unreliable.
3. Run the scenario; check the `Success` output to verify connection.
4. Feed the `Connection` output to blocks that perform PLC read/write operations.

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

* Use `TwinCAT Read` and `TwinCAT Write` blocks downstream of this block to perform read and write operations using the provided `Connection`.
* Combine `Logic Input` with the `Success` output to gate PLC operations only when the connection is active.
* Use `Debug Input` or a logging/display block to show connection status or errors during development.
* For saving or post-processing values read from the PLC, connect readers to `CSV Export` or `Data Write Global` blocks to persist results.
* If you need to trigger other actions when the connection appears or disappears, use logic blocks such as `Rising Edge` or `Set - Reset` with the `Success` output.

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

* If the block reports that TwinCAT modules are not available, ensure the TwinCAT runtime and required Python packages are installed on the machine, and restart the application.
* If `Success` remains false, check that the `AMS ID` and `Port` fields are correct and reachable from your network.
* Increase `Timeout (ms)` when connecting to remote PLCs or when experiencing intermittent network latency.
* If connection fails intermittently, use a small logic circuit (e.g., `Delay Step` + `Retry` logic with `Rising Edge`) to attempt reconnection only when needed.


---

# 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-connect.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.
