avatarFernando Souza

Summary

The web content provides a guide on how to connect and interact with OPC-UA (Unified Architecture) servers using the Go programming language, detailing the process of listing endpoints, browsing, reading, monitoring, and writing data.

Abstract

The article "How To Connect With OPC-UA Using Go" outlines the historical evolution of industrial data exchange protocols, emphasizing the transition from OPC Classic to the more versatile OPC UA. It explains the advantages of OPC UA, such as platform independence, security features, and a broader range of functionalities compared to its predecessors. The tutorial, which is based on Go version v1.15.3, demonstrates the practical use of the opcua Go package for connecting to an OPC-UA server. It covers essential operations including discovering available server endpoints, browsing the server's address space, reading data from tags, setting up monitored items for continuous data updates, and writing values to server nodes. The article also provides code examples and references to additional resources, making it a comprehensive starting point for developers looking to integrate OPC UA capabilities into their Go applications.

Opinions

  • The author acknowledges the complexity of industrial data exchange prior to standardized protocols and highlights the benefits of adopting OPC UA for cross-device communication.
  • There is an emphasis on the security and flexibility offered by OPC UA, which is particularly relevant in modern industrial networks that require robust communication protocols.
  • The tutorial is presented as a practical guide, suggesting that the Go programming language, combined with the opcua package, is a viable and efficient option for implementing OPC UA clients.
  • The article encourages readers to explore further by providing links to public OPC-UA servers and additional examples, indicating a community-driven approach to learning and development.
  • The author endorses an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), suggesting it as a valuable tool for readers interested in AI applications.

How To Connect With OPC-UA Using Go

And have access to a lot of devices.

Created with https://gopherize.me/.

Exchanging data has always been a challenge for the industry, with different options from devices (HMI, PLC) to protocols (Profibus, Modbus). If you wanted to connect various machines, you would either have to use the same manufacturer (therefore, the same protocol) or invest a lot of time to implement all the different protocols by yourself.

In the 1990s, a first attempt was made to create a standard, and the OPC (OLE for Process Control) was born. Known today as OPC Classic, it consists of three main specifications:

  • OPC DA (Data Access): defines an interface between client and server to exchange data.
  • OPC AE (Alarms & Events): defines the monitoring of variables, notifying clients with alarms and events.
  • OPC HDA (Historical Data Access): defines how the server stores the data in a form of database, and how to client can access it.

Although the protocol was accepted and implement by many manufactures, it was limited to only Windows systems.

In 2008, the OPC UA (Unified Architecture) was created. It unites all the services from the OPC Classic with more features:

  • Server discovery: check the availability of OPC Servers on the local network.
  • Subscriptions: monitor data and report when value changes.
  • Methods: clients can execute functions / programs based on methods defined on the server.
  • Platform independence: it can run on any hardware and any operating system.
  • Security: it is firewall-friendly while providing security controls, like session encryption, authentication, signing, and user control.

You can implement a client or server using different programming languages, and with Go is no different.

Let’s see how to use a Go client to connect with an OPC server.

Note: this tutorial used Go version v1.15.3.

Requirements

We are going to use the opcua package. You can install it using the command:

go get -u github.com/gopcua/opcua

Note: you will need an OPC-UA Server to be able to execute all the tests and codes shown here. You can check public online servers here.

List Endpoints

In OPC-UA, an endpoint is an access point with a specific set of capabilities that an user must know to be able to connect. A server can provide multiples endpoints, each containing information about:

  • URL (protocol and address)
  • Security Policy (algorithm and key length)
  • Security Mode (security level)
  • User Token Type (authentication supported by the server)

To list all available endpoints from a given server, we just need to use the function opcua.GetEndpoints, which will return a slice with all available endpoints.

Browse

Another function of OPC is the possibility to browse for all the variables available and get some information about them (like type, scale, min, max, etc).

You can use a combination of ReferencedNodes and Attributes to get information about the node itself and its children (if it has any).

Read

In order to read the value and attributes from a tag, we use the function ua.ReadRequest. You can read one or more nodes at the same request.

This function receives a struct with three parameters:

  • MaxAge: maximum age of the value to be read, in milliseconds.
  • TimestampsToReturn: which timestamps to be return in each request.
  • NodesToRead: a list of nodes and their attributes to read. A status code is returned, and you should check it to know if the request was successful.

Monitored Item

If you need to read continuously, you will need to request a read every period of time (also known as polling).

But OPC UA provides a better way to do it using a concept called Subscription. A client will subscribe to a group of nodes and let the server monitor these items. The server then notifies the client every time an attribute in the node changes.

A client can subscribe to three different types of change: when an attribute changes (like a value), when an event occurs or on aggregate values in a defined time interval.

To monitor some item, you have to create a monitor.NodeMonitor, informing the time interval you want to be notified, the callback function to be called, and which nodes you want to monitor.

The server will notifies the client at the time interval defined (in this example, each second). All the changes that occurred during this interval will be sent to the client.

Write

Some variables allow to external clients to write values directly to them. If this is the case, you can use the function ua.WriteRequest, passing a slice containing all the nodes you want to write, with their respective values.

Note: you can check if a variable is writable reading the attribute ua.AttributeIDAccessLevel.

If a node is not writable, an error StatusBadNotWritable will be returned.

Conclusion

The OPC-UA protocol is an attempt to make industrial components accessible from anywhere inside a network. It is a complete protocol, with different features that you can use in your application.

As it is a generic protocol, it has libraries in almost all programming languages, and is not different with Go.

We have seen how to use some basic commands and features of an OPC UA server. If you want to know more or need more cases, you should check here.

IoT
Opc
Tech
Programming
Go
Recommended from ReadMedium