我想将Python Gekko应用程序连接到实时系统。对于控制器的每个“周期”,分三个步骤:
该过程的可视化显示在this video中或从此帧开始。
要连接到实时系统,客户端必须读取和写入值。用于连接到过程控制系统(例如Python中的分布式控制系统(DCS)或可编程逻辑控制器(PLC))的标准通信协议是什么?
答案 0 :(得分:1)
Python可以从MODBUS,OPC以及SQL或其他协议读取和写入值。这是example code,用于Modbus(pymodbus)和OPC(OpenOPC)。
Python中的OPC示例
# #######################################
# OPC write
# #######################################
try:
# OPC connection
import OpenOPC
opc=OpenOPC.client()
b=opc.connect('Kepware.KEPServerEX.V5')
#opc.connect('Kepware.KEPServerEX.V5','localhost')
Load1_avg = opcm[0][0]
Load2_avg = opcm[0][1]
Load3_avg = opcm[0][2]
Load4_avg = opcm[0][3]
Load1_max = opcm[1][0]
Load2_max = opcm[1][1]
Load3_max = opcm[1][2]
Load4_max = opcm[1][3]
Load1_min = opcm[2][0]
Load2_min = opcm[2][1]
Load3_min = opcm[2][2]
Load4_min = opcm[2][3]
Load_T12 = opcm[3][0]
Load_T21 = opcm[3][1]
Load_T32 = opcm[3][2]
Load_T41 = opcm[3][3]
opc.write( ('Channel2.Device1.T_12_Load_AVG',Load1_avg) )
opc.write( ('Channel2.Device1.T_21_Load_AVG',Load2_avg) )
opc.write( ('Channel2.Device1.T_32_Load_AVG',Load3_avg) )
opc.write( ('Channel2.Device1.T_41_Load_AVG',Load4_avg) )
opc.write( ('Channel2.Device1.T_12_Load_MAX',Load1_max) )
opc.write( ('Channel2.Device1.T_21_Load_MAX',Load2_max) )
opc.write( ('Channel2.Device1.T_32_Load_MAX',Load3_max) )
opc.write( ('Channel2.Device1.T_41_Load_MAX',Load4_max) )
opc.write( ('Channel2.Device1.T_12_Load_MIN',Load1_min) )
opc.write( ('Channel2.Device1.T_21_Load_MIN',Load2_min) )
opc.write( ('Channel2.Device1.T_32_Load_MIN',Load3_min) )
opc.write( ('Channel2.Device1.T_41_Load_MIN',Load4_min) )
opc.write( ('Channel2.Device1.T_12_Load_INST',Load_T12) )
opc.write( ('Channel2.Device1.T_21_Load_INST',Load_T21) )
opc.write( ('Channel2.Device1.T_32_Load_INST',Load_T32) )
opc.write( ('Channel2.Device1.T_41_Load_INST',Load_T41) )
opc.close()
except:
print 'OPC communication failed'
pass
Python中的MODBUS示例
# #######################################
# Modbus write
# #######################################
try:
# import the various server implementations
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder
# initiate client
##client = ModbusClient('192.168.0.1')
client = ModbusClient(host='localhost', port=502)
slave_address = 0
# AVG Registers Modbus 40001,3,5,7
# MAX Registers Modbus 40009,11,13,15
# MIN Registers Modbus 40017,19,21,23
# INST Registers Modbus 40025,27,29,31
# registers
reg = 0
# AVG, MAX, MIN, INST
for i in range(0,4):
# Channels
for j in range(0,4):
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_32bit_float(opcm[j][i])
payload = builder.build()
result = client.write_registers(int(reg), payload, skip_encode=True, unit=int(slave_address))
# two registers for floating point numbers
reg = reg + 2
client.close()
except:
print 'Modbus communication failed'
pass
除了MODBUS和OPC,公司还支持其他基于文本的文件传输和其他通信方式,尽管这些在石油和天然气行业中最常见,它们具有分布式控制系统(DCS)或可编程逻辑控制器( PLC),用于计划和数据获取(DAQ)。据我所知,工业应用中大约有140个Gekko / APMonitor实例。其中一些是listed here。