📜 ⬆️ ⬇️

ScadaPy application modbus protocol

Continuing articles here, here, here and here. , I want to give examples of simple use of python scripts from the ScadaPy series, both in the field of home automation and in manufacturing plants.


1. ADAM modules from Advantech and ScadaPy.


A simple example of using python to poll Advantech Series 4000 modules.


To read data and transfer control commands using its own protocol DCON. Once upon a time, a program from this manufacturer was distributed and, if I'm not mistaken, Adam View or GeniDAQ was called. We once started with her, it was very interesting. To date, I got a few modules that are still fully functional.


image

The list of commands for the survey is given in the documentation for each module. You can read here .


The interface rs485 is used for polling. In the presence was only the input model output 4050, so the processing wrote for this model. Input lines 7 digits, output lines 8 digits.


To read the data, send the command ' $ 016 \ r ' to the port

$ - control character
01 - device address
6 - team
serialPort.write ('$ 016 \ r')
dataIn = serialPort.read (8)
serialPort.flushInput ()


In response, 8 bytes of the form ! 007F00 will be received. Information bytes 3 and 4 report the status of digital inputs DI, and bytes 1 and 2 report the status of digital outputs DO. Below is the decryption code.


if(dataIn[0]=='!'): byteDI = str(bin(int('0x'+dataIn[3]+dataIn[4],16))[2:] ).zfill(7) byteCoil = str(bin(int('0x'+dataIn[1]+dataIn[2],16))[2:] ).zfill(8) 

Further, the state of the digital inputs of the ADAM-4050 are located in the slave part of the program in the DISCRETE_INPUTS registers, and the state of the digital outputs is transferred to the COILS registers.
')
image

Library put here on github.com


2. ScadaPy and the master_ping.py module.


It is used to detect the loss of communication with subordinate servers on remote objects. At regular intervals, an ICMP packet is sent to a remote server, and if there is no connection, it is displayed on a mnemonic. Of course, it is advisable to add some kind of audible alarm with handshake.


image

Library put here on github.com


3. ScadaPy and Smartgen generator module.


I wrote about this earlier here.

As a variant of the implementation of the program for monitoring the error of the generator and its current state. Of all the important parameters of all this is the level of fuel, load currents and engine temperature. As a rule, these parameters are checked regularly during generator operation.


image

The console version is possible, I like it more


image

Library put here on github.com


4. ScadaPy and master_http module.


I will describe a recent implementation example.

An I-8831 device is installed on the site, it works using the Modbus TCP protocol. It has 32 discrete inputs. It is necessary to control the state of the "blinkers" of the cells of the outgoing feeders of the substation. There are no strict requirements on the real-time mode, as far away in the mountains it is enough to receive information once every 15 minutes.


image

Implemented using Modbus_tk libraries and requests. The principle is simple, data is received via the Modbus TCP protocol and sent to the server via http using the GET method. Everything is extremely simple.


 import requests.packages.urllib3 resp = requests.get('http://myserver.ru/alive.php?v='+val+'&d='+d+'&r='+reg+'&s='+st+'&dv='+device, timeout=5 ) 

It can be implemented with the addition of SSL, as well as a login and password.

 from requests.auth import HTTPBasicAuth import requests.packages.urllib3 requests.packages.urllib3.disable_warnings() #       resp = requests.get('https://myserver.ru/alive.php?v='+val+'&d='+d+'&r='+reg+'&s='+st+'&dv='+device, timeout=3, verify=False ,auth=HTTPBasicAuth(login, password)) # verify=False –     # login, password –     

On the server side, you can limit yourself to a single PHP script with the function of saving to the database. As an example, I have indicated saving to a file.

 $val=$_GET['v']; $ind=$_GET['d']; $reg=$_GET['r']; $state=$_GET['s']; $device=$_GET['dv']; $dt=Date("dmY"); $now = DateTime::createFromFormat('U.u', microtime(true)); $tm = $now->format("mdY H:i:su"); $getData=$tm.' '.$state.' '.$device.' '.$reg.' '.$val; $fp=fopen('./alive.log','a+'); if($fp) { fputs($fp,$getData."\n"); } fclose($fp); 

Library put here on github.com

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


All Articles