Python OpenOPC asyncRefresh错误

时间:2015-06-23 09:19:14

标签: python opc

我正在使用带有OpenOPC库的Python和来自OMRON的CX Server OPC 2.1来从PLC收集数据并每隔10秒将其保存到数据库中。

有一个定时器可以这样工作:

# This program is a timer that calls the other scripts

# Import libraries
import threading
import os    # To call the scripts

# Load the Mysql connection script
from loadMysql import *

# To choose the sample time for the readings
cursor.execute("SELECT * FROM sample_time WHERE Id=1 ")
for row in cursor:
    SampleTime = row[1]
print SampleTime

# Loads OPC connection script
from loadOPC import *
OPC_State = opc.ping()               # To check connection

# If the connection with the OPC server is OK
if OPC_State == 1:
    # Create an object called "timer"
    def timer():
        # Is called every desired seconds
        threading.Timer(SampleTime, timer).start()
        # Prints the time
        localtime = time.asctime(time.localtime(time.time()))
        print localtime
        print 'The OPC server is ' + str(OPC_State)
        # Calls the scripts to gather data
        os.system("SensorReading.py 1")
        os.system("SetpointReading.py 1")
        os.system("AlarmReading.py 1")
        os.system("EquipmentReading.py 1")
        os.system("RO_Calculus.py 1")
    # Start the timer again
    timer()
# If the connection with the OPC is wrong
else:
    print 'There is a problem with the OPC'

# Close opc connection
opc.close()

一个被调用的脚本(都是相似的)是:

# This program takes the id's and the PLC's adresses from the DB
# and gathers the data from SENSORS and saves it to the DB using the OPC server connection

# START STEPS:

# Loads the OPC connection script
from loadOPC import *
# Loads the Mysql connection script
from loadMysql import *

# MAIN PROGRAM STEPS:

# Query to the DB to read all the ID's linked to the sensors
cursor.execute("SELECT ID_Sensor FROM plc_positions WHERE ID_Sensor >0 ORDER BY ID_Sensor ASC")
# Saves all the ID's from the sensors into the id_sensor variable (all in order)
id_sensor = cursor.fetchall()

# Query the DB to read all the positions linked to the PLC
cursor.execute("SELECT Position FROM plc_positions WHERE ID_Sensor >0 ORDER BY ID_Sensor ASC")
# Saves the positions from the PLC adress taken from the DB
posicions_sensor = cursor.fetchall()
# Create a vector to save all the PLC's adress tags
tags_sensors = []
# Save all the PLC addresses tags into a vector to read it
for i in range(len(posicions_sensor)):
    tags_sensors.append(str(posicions_sensor[i][0]))

# Read the sensor data from the OPC and saves it into a sensor names "sensors"
opc.read(tags_sensors, group='GrupSensors', update=1)
sensors = opc.read(group='GrupSensors', timeout=15000)

# Loop that inserts every sensor readings with its id and value to the DB
for i in range(len(id_sensor)):
    sql3 = """INSERT INTO sensor_reading (ID_Sensor, Date, Value) VALUES (""" + str(id_sensor[i][0]) + """,localtime, """ + str(sensors[i][1]) + """)"""
    try:
        # Executes the query
        cursor.execute(sql3)
        # Commits the changes in the DB
        db.commit()
    except:
        # If there's an error does nothing
        db.rollback()

# EXIT STEPS:

# Deletes the cursor
cursor.close()
#Close the DB connection
db.close()
# Close the tags
opc.remove('GrupSensors')
# Close opc connection
#opc.close()

库是:loadOPC

# Connect to the OPC server
import OpenOPC                          # Imports the OpenOPC library
opc = OpenOPC.client()                  # Opens the client
server = 'OMRON.OpenDataServer.1'      # Choose the server
opc.connect(server)                     # Connect to server

loadMySQL:

# Import library to link Python to MYSQL
import MySQLdb
# Libraries for time
import time
# Create a variable for the time
localtime = time.asctime(time.localtime(time.time()))

# Connect to the DB ("host", "username", "password", "db_name")
host = 'localhost'                                          # Where the host is (local or internet)
username = 'XXXXX'                                       # user name to get into the DB
password = 'XXXXX'                                         # Password to get into the DB
db_name = 'colmatar'                                           # DB name
db = MySQLdb.connect(host, username, password, db_name)     # Connect and name it DB

# Create the object cursor to execute the SQL command
cursor = db.cursor()

我得到的问题是,有时候(有时它会在一周内运行良好,有时在30分钟后它会死亡)会出现错误:

http://i58.tinypic.com/1zx337o.jpg

我一直在检查,这是因为CX服务器OPC死了或发生了什么事情并且得到了这个......

它是否与代码有关(在每个脚本上打开和关闭OPC连接而不是每次轮询开始?)。

提前致谢。

0 个答案:

没有答案