⬆️ ⬇️

ScadaPy - using OPC UA

In the previous few articles, I have described the possibilities of using the modbus protocol to create my own Scada system based on python. This time I would like to share the experience of building a system for polling slave devices using OCR technology.

The disadvantages of OPC servers are that they can only be used in the Microsoft Windows operating systems (as a rule, they are paid), and you could forget about devices using Linux OS.



But over time, the OPC Unified Architecture specification (the OPC Unified Architecture) was created, which made it possible to use this data transfer technology on other operating systems other than Windows. This also applies to embedded systems where full-fledged Linux can be launched.



Read more here.



For example, you can run several different OPC UA servers on the Raspberry Pi single-board computer to poll terminal devices, meters, sensors, etc., while the system will work quite stably.

')

image



Installing Libraries



Xubuntu 17.04 Desktop and Windows 8.1 are used to work with OPC UA and modbus servers. On Xubuntu 17.04, Python 2.7 and Python 3.5 are already installed by default. Choosing Python 3.5.



If the necessary packages have not been added to the computer after installing the operating system, then you need to run:



sudo apt-get install python3-pip sudo pip3 install pytz PyQt5 sudo apt-get install python3-opcua, libxml2-dev, python3-lxml 


We solve the problem of dependencies:



 sudo apt-get –f install 


After we put the necessary libraries:



 sudo pip3 install requests pyserial 


For windows, you can install via pip3.exe, the library and examples are located here.



To start the server, the library must be imported:



 import sys from opcua import ua, Server 


Now we create an OPC UA server.



 server = Server() #  IP    ,    server.set_endpoint("opc.tcp://0.0.0.0:4840/") #   server.set_server_name("Server") #     #      ,          server.load_certificate("server_cert.der") server.load_private_key("server_private_key.pem") #    uri = "http://server" idx = server.register_namespace(uri) #         objects = server.get_objects_node() #      Object_1 =objects.add_object(idx,'MyFirstObject) Object_2 =objects.add_object(idx,'MySecondObject) Object_3 =objects.add_object(idx,'MyThirdObject) #   Discret_1 = Object_1.add_variable(idx,'Discret_1',[0,0,0,0,0,0,0,0]) Discret_2 = Object_2.add_variable(idx,'Discret_2',[0,0,0,0,0,0,0,0]) Analog_3 = Object_3.add_variable(idx,'Analog_3',[10,20,30,40,50]) #  server.start() 


That's all the python code to run OPC UA. As it turned out nothing complicated, and if you now connect to the running server using UA Expert , you can see a hierarchical list of our objects and variables with values.



To modify the values ​​of variables, use the set_value function of the type:



 Discret_1.set_value([1,1,1,1,1,1,1,1]) 


Of course, this is a very primitive example, but the OPC UA library contains many more features that can be found here .



The only thing that could not be figured out is how to set the login and password on the server, something like through politics, I think I will solve this problem later.



Server configurator



In continuation of the above, the task arose of quickly configuring each newly created server.



For this purpose, the Server Configurator was written in the PyQt5 library.



image



Principle of operation:



- database is created on sqlite3

- tables are formed for the slave and master parts of the server.

- the tables are filled with the necessary parameters.

- formed startup script.



The main idea is that servers should work equally in Windows and Linux.

Download here



Directory structure:

srvconf.py - Server Configurator program

db - the srvDb.db database file is located.

img - .png files for buttons

source - server template files





scr - script files to start the server.



For Windows, a file of type start_XX.bat will be created, for Linux, a file of type start_XX.sh, where XX is the server sequence number in the servers table.



Contents of the start_XX.bat file:



 rem     'ScadaPy   v.3.11' rem   'Windows opcua_mercury230 ' rem Slave  '192.168.0.103' rem Slave  '4840' rem  master 'master_mercury230' rem  slave 'opcUA' rem  tty 'com6' rem  tty '9600' start c:\Python35\python.exe F:\scadapy\config\source\opcua_master_mercury230.py 68 F:\scadapy\config\db\srvDb.db 


Contents of the start_XX.sh file for Linux:

 #     'ScadaPy   v.3.09' #   'Modbus TCP' # Slave  '192.168.0.101' # Slave  '504' #  master 'master_modbusTCP' #  slave 'modbusTCP' #  tty '/dev/ttyUSB0' #  tty '9600' /home/jack/config/source/modbustcp_master_tcp.py 67 /home/jack/config/db/srvDb.db 


Linux startup parameters use xfce4-terminal, since I work in Xubuntu 17.04,

but you can specify a different type of launch, like gnome-terminal.



 xfce4-terminal --command='sudo /home/jack/config/scr/start_67.sh' 


In the examples, the parameters for filling the tables are quite clearly described.



It is noteworthy that 4 servers were simultaneously launched on raspberry Pi - mobus_ping, opcua_http, opcua_mercury230 on / dev / ttyUSB0 and modbus_dcon on / dev / ttyUSB1, while it worked quite stably and without failures.



The control is currently implemented in part and only in master_dcon, therefore only tele-alarm and tele-measurements are used. In the future, I think to add and remote control.

Source: https://habr.com/ru/post/341728/



All Articles