Python OpenOPC asyncRefresh error

1.3k views Asked by At

I am using Python with the OpenOPC library and CX Server OPC 2.1 from OMRON to gather data from a PLC and save it to a DB every 10 seconds.

There is a timer that works like this:

# 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()

And a script that is called (all are similar) is:

# 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()

The libraries are: 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()

The problem I get is that sometimes (sometimes it works well for a week and sometimes after 30 minutes it dies) it gets the error:

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

I have been checking and this happens because CX server OPC dies or something happens to it and gets this...

Is it something related to the code (opening and closing the OPC connection at every script instead and the begining of each poll?).

Thanks in advance.

0

There are 0 answers