TwinCAT HMI

Connecting Bechoff PLC via Websocket with realvirtual.io

This interface is only included in realvirtual.io Professional.

Additionally the interface depends on BestHTTP V3 which needs to be purchased separately on the Unity Asset Store

https://assetstore.unity.com/packages/tools/network/best-http-267636

This interface is beside TwinCAT ADS another interface for Beckhoff PLCs. It requires HMI licenses on the TwinCAT PLC. The advantage of this interface is that it does not depend on an installation on the device on which the simulation model is running. Communication takes place exclusively via WebSockets. This interface works on any type of target platform and also works within WebGL builds.

In contrary TwinCAT ADS requires ADS installed on the machine and is by this limited to Windows System.

The interface can be added to a realvirtual.io scene by selecting game4automation > Add Object > Interface > TwinCATHMI.

Prerequisites

  • TE2000 license is needed for properties (HMI) configuration.

  • TE2000 Version 1.12.758.8 and above

  • You need on your PLC TF2000 licenses

  • REALVIRTUAL_BESTHTTP3 needs to be added to your Unity Project Project Settings > Player > Script Compilation

  • Newtonsoft.Json needs to be available and installed. In latest standard Unity releases Newtonsoft is already included. For enabling Newtonsoft please also add REALVIRTUAL_JSON to your Scripting Define Symbols Project Settings > Player > Script Compilation. For more infomation about how to install and get please check section Newtonsoft JSON

Annotations in the PLC code

Within the TwinCAT PLC source all symbols needs to be annotated with { attribute 'TcHmiSymbol.AddSymbol' }, if they should be available for the TwinCATHMI interface.

Interface Properties

IsConnected (Readonly)

True, if the interface is successfully connected to the PLC.

Server Adress

The adress of the Server (Notation must be ws://IP:Port

Status (Readonly)

The current Status of the Websocket, "Open" if connection is active.

IsConnecting (Readonly)

True during reconnection to Server.

DebugMode

If set to true additional messages are printed out to the Unity console

Intervall Time

The Intervaltime in Milliseconds. All update messages are collected to one message inside the PLC within the intervall time.

Subscribe Outputs

If set to true, the outputs are subscribed and only changed outputs are send within the intervall time.

Poll Outputs

If set to true all outputs are send wthin the intervall time.

Symbol Table

It is possible to import all symbold from an external CSV file. The table must look like this:

Security

If you want to use security features and login into the PLC with domain, user and password you need to set this to true and define the user and the password.

EventSignalRead

An Event which is fired when a Signal is read from the interface.

Complex data types

Only primitive datatypes like floats, ints, bools are transformed automatically to the realvirtual.io PLCInput and PLCOutput Variables.

Reading primitives within complex data

For reading complex datatypes you can access the primitive data within the complex datatype by adressing them like this:

Reading complex data with C# scripts

For subscribing a complex datatype you should create a variable of Type PLCOutputText. Within this text you will get the complex data as a Json. You can now use within a C# script JSON deserialization to transfer this JSON to a C# data type.

Writing complex data with C# scripts

For writing complex data you can call the public Method WriteSymbol

public void WriteSymbol (string symbol, object signal)

This will serialize the given object into a JSON and send it to the PLC. It is important that the C# data structure is identical to the data structure within the PLC.

Writing complex data with write acknowledgement

Usually, when using WriteSymbol for direct writing symbols into the PLC, the function is non blocking. You don't get any value back and you won't be automatically informed if the symbol is not written. In very complex and big data structures writing can take some time.

If you want to be sure that the data is written there is another write command:

public int WriteSymbolControlled (string symbol, object signal)

This function is giving back an id as an INT

Before calling WriteSymbolControlled you should subscribe to this event, which will be called as soon as a reply is send from the PLC.

EventSignalRead.AddListener(OnSymbolWritten);

The code for your event should look somehow like this. You need now to check if the method was exactly called for your WriteSymbolControlled id (which was given back in the call) and if there was no error (error == false). You can also look in detail into the reply which is given you as a full JSON string in reply.

   private void OnSymbolWritten(int id, bool error, string reply)
        {
            // Here check if error is false and the id is the same as given back in WriteSymbolControlled
        }

© 2022 in2Sight GmbH https://realvirtual.io - All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including printing, saving, photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

Last updated