* This blog follows from – Leveraging the Cloud for WinCC OA. Please check it out for an introduction to using the Cloud capabilities for WinCC OA.

** The tutorial in this blog is only meant as an introduction to using Websockets in WinCC OA and does not cover all function types. Please refer to documentation for additional details or reach out to SAM IT Industrial Automation team for further assistance by calling +1-919-800-0044 or e-mail us at [email protected]

Starting from WinCC OA version 3.15, the provided http server supports Websockets. WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. Websockets do not contain overhead data like that in HTTP and hence, require significantly less bandwidth. Moreover, once a connection is established, the server can send live updates and notifications to the client (in this case an Internet browser) without waiting for a client request. Similarly, the client can send requests to the server which will be queued until they can be served.

Let us look at some advantages to using websockets over the OLE DB Provider as mentioned in this post:

  • It can not only access archived values in HDB, but also real time data and alerts.
  • Data can be read as well as edited.
  • It can run simply using a CTRL Manager.
  • No limitations to accessing data from distributed systems.
  • No compatibility issues for Linux/Windows, neither for different versions (32-bit/64-bit).
  • Only requirement is that the client browser should support Websocket protocol.

Now I will describe how to get started with Websockets. To use Websocket, first create a control file and follow the steps below:

Step 1 – Using httpConnect() with the Websocket flag

Import the “CtrlHTTP” library and define httpServer() and httpConnect() functions within main body as follows:

#uses “CtrlHTTP”
void main()
{
httpServer(false, 8080);
//Activates an HTTP Server listening on port 8080. False means Authentication is not used.

httpConnect("websocket", "/websocketurl", "_websocket_");
//Here “_websocket_” is the flag. This flag defines the “/websocketurl” url as a websocket.
//The first argument “websocket” registers a function called websocket() as web resource.
}

We will define the Websocket function next.

Step 2 – Define the function websocket()

When a web browser sends a request to the WinCC OA http server, the server starts the callback process within a thread and passes a mapping. Mappings are simply associative arrays, or dictionaries. Hence define the function as follows:

void websocket(mapping map)
{
mixed any;
//Here, any is a variable that we will use next to read data from the Websocket Request.
}

Step 3 – Add the httpReadWebSocket() function

This httpReadWebSocket() function is a waiting Control function which waits for a Websocket message from the client. It returns 0 if a message(or many messages) has been sent and is ready to read. If the socket is closed on the client side, this function returns 1.

So now, let’s define the logic to read a request:

while ( httpReadWebSocket(map["idx"], any) == 0 )
//Here, map[“idx”] references the Internal File Descriptor of the Websocket connection.
//The second argument any, gets written by the message that is received.
{
DebugTN("Received Message", any);
//Read and display the message to Log Viewer
}

*Here, note that the received message is stored in a mixed data type ‘any’. This is because websockets support only text or binary messages, and they can be passed in an anytype/mixed variable.

Step 4 – Building Logic to respond to requests

If you are using javascript to send requests to the WinCC OA http server, it is advisable to send the messages in a JSON format converted to string. To convert this JSON data to a string, you can use the function JSON.stringify(JSON_Object). The reason for using JSON format is that data parsing becomes very easy. Like you would expect, WinCC OA has a script “json.ctl” in its library that we can leverage to parse and understand the received message.

As an example, let’s say you define a request in the following format using javascript, and send it to the http server as shown below:

websocket_object = new WebSocket("ws://" + window.location.host + "/websocketurl");
//Here websocketurl is the URL used to initiate a Websocket connection in localhost.
var req_msg = {
type: “dpGetPeriod”,
dpe: “System1:Site.Site1.Total_Energy”,
T1: “2018.02.17 10:30:05.000000000”,
T1: “2018.02.18 11:30:05.000000000”
};
websocket_object.send(JSON.stringify(req_msg))
//Here, websocket_object is a js variable of the type Websocket, that communicates with the
//Server.

After the message is read on the server side, this message can be converted to json format and can be parsed as follows:

mixed any;
anytype json_data;
while ( httpReadWebSocket(map["idx"], any) == 0 )
{
DebugTN("Received Message", any);

json_data = json_strToVal(any);
if (json_data[“type”] == “dpGetPeriod”) {
custom_function(map[“idx”], json_data);
}
}

Here, you can create a custom_function() to execute the actual dpGetPeriod() function as well as add any extra logic, however you like.

Step 5 – Send Response using httpWriteWebSocket()

Based on the variables mentioned above, your write function should be like follows:

int custom_function(int idx, const mapping &json_data)
{
mapping response_data;

//Code containing backend logic

response_data[“type”] = json_data[“type”];
response_data[“dpe”] = json_data[“dpe”];
response_data[“values”] = values_from_logic;
response_data[“times”] = times_from_logic;
//Variables values_from_logic and times_from_logic are derived from the
//actual code you have to implement using dpGetPeriod()

return httpWriteWebSocket(idx, jsonEncode(response_data));
}

After sending the response, the variable response_data can now be parsed accordingly on the client side web browser using Javascript or any other relevant tool.

That’s it! This small tutorial should help you get started on how to use Websockets in WinCC OA. Sending and receiving messages on the client side can be easily done using Javascript, and have not been mentioned much here. You can also use Python to send and receive Websocket messages. Whatever methods you use, once you are able to get the underlying data, you can connect to a Dashboard directly and display real time values, or you can push them to a database for data analytics.

For completion, here’s the complete Pseudo code:

#uses “CtrlHTTP”
#uses “json.ctl”
void main()
{
httpServer(false, 8080);

httpConnect("websocket", "/websocketurl", "_websocket_");
}

void websocket(mapping map)
{
mixed any;
anytype json_data;

while ( httpReadWebSocket(map["idx"], any) == 0 )
{
DebugTN("Received Message", any);

json_data = json_strToVal(any);
if (json_data[“type”] == “dpGetPeriod”) {
custom_function(map[“idx”], json_data);
}
}
}

int custom_function(int idx, const mapping &json_data)
{
mapping response_data;

//Code containing backend logic

response_data[“type”] = json_data[“type”];
response_data[“dpe”] = json_data[“dpe”];
response_data[“values”] = values_from_logic;
response_data[“times”] = times_from_logic;
//Variables values_from_logic and times_from_logic are derived from the
//actual code you have to implement using dpGetPeriod()

return httpWriteWebSocket(idx, jsonEncode(response_data));
}

Should you need help implementing any of these for your environment, feel free to reach out to the Industrial Automation experts at SAM IT Solutions, we are just a phone call away. Call +1-919-800-0044 or e-mail us at [email protected]

Control Infotech, our Industrial Automation partner applies substation automation domain expertise in the realm of utility grid-tie solar generation plants. Complete solutions from Grid-tie engineering, protection & Control panel build, relay programming and PV plant asset monitoring are among the solutions they offer. The SCADA system is technologically the most advanced. It offers user friendly features on a non-proprietary commercially available platform. Customers benefit from a stable and powerful monitoring and control platform that can be seamlessly expanded and deployed on a Cloud platform.

Suyash Kanungo, BTech, MS
Computer Engineer
SAM Analytic Solutions


*This blog follows from – Leveraging the Cloud for WinCC OA. Please check it out for an introduction to using the Cloud capabilities for WinCC OA.

In the last blog, we explained about the advantages of using platforms like Elastic Stack, Web Frameworks, Python etc. with WinCC OA. In this blog, we discuss how to actually access data from WinCC OA to use with an analytics or Big Data platform or database of your choice. We will focus on the data archived in History DB (HDB), and how to pull it using OLE DB Provider.

OLE DB Provider has been provided by WINCC OA. OLE DB is a Microsoft specification for accessing data on different computers. It is based on Microsoft’s COM technology and is the successor to the older and limited ODBC technology. While ODBC uses static APIs for data access and is limited to SQL, OLE DB uses ADO (ActiveX Data Objects) to provide a quick and easy facility for programming applications.

OLE DB Provider is supposed to give access to the underlying HDB. It uses its own SQL queries to get Data Points. The examples are provided in the Help Documentation provided along with the WinCC OA software.

Let’s check out the requirements and limitations for using OLE DB with WinCC OA:

  1. WinCC OA version 2.12.1 or higher should be installed.
  2. It can access only archived Values and Alerts that exist in the History DB.
  3. Data can only be read, not edited.
  4. No direct support for Windows Excel 2000 or earlier versions.
  5. The Data Manager must be running when OLE DB Provider is started. If the Data Manager is stopped, queries using OLE DB are no longer possible.
  6. For distributed Systems, each WinCC OA instance should be running its own OLE DB Driver to provide access to external applications.
  7. Under Windows 64 bit, only single Server Systems are allowed as compared to Distributed systems having multiple Clients.
  8. OLE DB provider is a 32 bit driver, so it might not interact too well with 64-bit applications.

Based on these points, if your requirements are not met, you can refer to – Accessing HDB via Websocket using C# API. If you can work with these requirement, read on.

Let’s get started on how to set up OLE DB Access. The following steps can be found in the Help documentation provided in the WinCC OA software too:

Step 1 – Add WinCCOAoledb manager to the WinCC OA Project

Go to your project directory and navigate to the config folder within it. Open the file called progs and add the following line to the end:

windows/WCCOAoledb | manual |      30 |        2 |        2 |

 

*Make sure there are no blanks in the end.

Step 2 – Register the OLE DB drivers and executable

Open a command prompt as Administrator, and navigate to the WinCC_OA_installation_directory/bin/windows directory. Here, run these 3 commands:

WCCOAoledb.exe /regserver

regsvr32 WCCOAOleDbExeps.dll

regsvr32 WCCOAoledb.dll

 

Step 3 – Start WCCOAoledb Manager from the console

Now, when you start/restart your project, you’ll see the OLE DB Manager in the console.You can start it directly from the console.


Click for larger image
We have been able to get access using MS Excel as well as a Python Script with WIN32COM library. To find instructions on how to access HDB data using MS Excel, please refer to the help documentation provided with WinCC OA. Moreover, MS Access will also be able to access the underlying Data. An example output using python is shown below:


Click for larger image
To use OLE DB Provider with Python, a 32 bit version of python should be installed, and you should download the win32com library to use the communication client. The win32com client is distributed as a part of pywin32 library, which you can download from here.

If you have any questions, or need help implementing any of the Industrial Automation tools mentioned in this and other Blog posts, please feel free to contact us with any questions at +1-919-800-0044 or e-mail us at [email protected]

Control Infotech, our Industrial Automation partner applies substation automation domain expertise in the realm of utility grid-tie solar generation plants. Complete solutions from Grid-tie engineering, protection & Control panel build, relay programming and PV plant asset monitoring are among the solutions they offer. The SCADA system is technologically the most advanced. It offers user friendly features on a non-proprietary commercially available platform. Customers benefit from a stable and powerful monitoring and control platform that can be seamlessly expanded and deployed on a Cloud platform.

Suyash Kanungo, BTech, MS
Computer Engineer
SAM IT Solutions