Below is an example of a custom implementation of a simple SCADA program executed, as is commonly said, “on the knee”.
The task is to monitor the diesel generator. It is necessary to keep a log with indication of hourly parameters, such as: fuel level, oil pressure, temperature, battery charge, current, voltage, operating hours.
The generator was located at a distance of half a kilometer from the office and with time these walkings began to strain, especially in bad weather.
')
As it turned out, there is a controller installed on the generator that supports the Modbus RTU communication protocol, which means that you can lay a twisted pair cable and connect via RS-485.
After studying the address table, we decided to make a simple program ourselves.
The result is a ScadaPy.
Modbus TCPInterface sharing. First we connect the modbus library.
import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus_tcp as modbus_tcp
Create an object link to where you are connecting and specify:
host = "IP address of the device with which we are connecting"
port = "port of the device to which we are connecting"
master = modbus_tcp.TcpMaster(host=slaveIP, port=int(slavePort)) master.set_timeout(1.0)
Now we try to get data from the device, in this case starting from the address of register 0, we get 10 registers of discrete signals (TS).
getDI=master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 10)
For other types of registers it is necessary to specify other names.
master.execute(1,st.READ_COILS, 0, 10) master.execute(1,cst.READ_INPUT_REGISTERS, 100, 3) master.execute(1,cst.READ_HOLDING_REGISTERS, 100, 12)
Now if you do:
print getDi
We will receive an array of data from the device from address 0 to address 9.
(0,1,0,1,0,0,0,0,0)
If something like this appears, it means the device is in touch. Data acquisition from other types of registers is similar.
Formation of the program windowWe connect the library.
from Tkinter import *
Create an object link (window).
root = Tk()
Set the window background image.
im = PhotoImage(file=backGroundPath)
Create a canvas object.
canv = Canvas(root,width=1900,height=950,bg="black",bd=0, highlightthickness=0, relief='ridge')
We place in the window.
canv.place(x=0, y=25)
We display the background.
canv.create_image(1, 1,anchor=NW, image=im)
Run the loop.
root.mainloop()
Polling functionIn order to continuously poll devices using the modbusTCP protocol, there are methods after and mainloop in tkinter, but first you need to create a procedure jobModbusTCP.
def jobModbusTCP(): getDI=master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 10) if(int(getDI[0]) == 1): canv.itemconfig(diFig1,fill='red') if(int(getDI[0]) == 0): canv.itemconfig(diFig1,fill='green') if(int(getDI[1]) == 1): canv.itemconfig(diFig2,fill='red') if(int(getDI[1]) == 0): canv.itemconfig(diFig2,fill='green') root.after(1000, jobModbusTCP)
Program codeBelow is the program code that displays the state of the registers [0] and [1], if the logical state of the register is 0, the square on the canvas will be green, if the logical state is 1 - red.
from Tkinter import * import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus_tcp as modbus_tcp import math def jobModbusTCP(): getDI=master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 10) if(int(getDI[0]) == 1): canv.itemconfig(diFig1,fill='red') if(int(getDI[0]) == 0): canv.itemconfig(diFig1,fill='green') if(int(getDI[1]) == 1): canv.itemconfig(diFig2,fill='red') if(int(getDI[1]) == 0): canv.itemconfig(diFig2,fill='green') root.after(1000, jobModbusTCP) master = modbus_tcp.TcpMaster(host='192.168.0.1', port=502) master.set_timeout(1.0) root = Tk() im = PhotoImage(file='bg.gif') canv = Canvas(root,width=1900,height=950,bg="black",bd=0, highlightthickness=0, relief='ridge') canv.place(x=0, y=25) canv.create_image(1, 1,anchor=NW, image=im) diFig1=canv.create_rectangle(10,10,30,30,fill='gray', outline='black') diFig2=canv.create_oval(50,50,80,80,fill='gray', outline='black') root.after(1, jobModbusTCP) root.mainloop()
Now once a second the program will send a request to the device and display the answer on the mnemonic scheme.
More examples can be found
here .