could you help me with the connection to Geoscada database from python?
I want to create a python script that can read and write values to variables inside the Geoscada database (local geoscada server). I think I can use python library pyodbc, but I cannot find documentation or guide on how to do it. Is it possible? if yes, how to do it? And if not, is there any solution how to manipulate DB from python (or from another language)??
Thank you very much!
You're on the right track with accessing it via ODBC when using python. I've written a few things against it for extracting history and doing data frames and such in the past and it works really well. Below is a sample on how to connect, run a query, and iterate through it. This should be enough to get you started.
import pyodbc #connect to the DSN cnxn = pyodbc.connect('DSN=DSNNAMEHERE;UID=USERNAMEHERE;PWD=PASSWORDHERE') cursor = cnxn.cursor() #execute the query. the same approach would be used for writing updates to update values. cursor.execute("SELECT TOP(10) Id,FullName FROM CDBPOINT ORDER BY FullName ASC") #build the record set points = cursor.fetchall() #loop the recordset for point in points: #do something here #data can be accessed as follows pId = point.Id pFullName = point.FullName print(str(pId) + ';' + pFullName)
There is the ODBC way as mentioned previously... I haven't tried this from Python, but I'm sure there would be some way to use the OLE / COM interface also (possibly even the .NET interface.. assuming there is some Python library to deal with managed .NET APIs).
On the about to be moved TProjects space there is a collection of (short) documents around using the Automation Interface from various languages. Perl isn't Python of course... but they possibly have similar traps around threading model, and needing to go via OLE to obtain an object reference... so it might be helpful (or not)
The best way to connect is to use the pythonnet library to connect to the Geo SCADA .Net client.
This is (was when I last looked) available for python up to v3.8).
# Import .Net runtime support - needs "pip install pythonnet" import clr # Get Geo SCADA Library (could use the namespace if the dll is on PATH) CS = clr.AddReference( "c:\Program Files\Schneider Electric\ClearSCADA\ClearSCADA.Client.dll" ) import ClearScada.Client as CSClient # Create node and connect, then log in. (Could read net parameters from SYSTEMS.XML) node = CSClient.ServerNode( CSClient.ConnectionType.Standard, "127.0.0.1", 5481 ) connection = CSClient.Simple.Connection( "Utility" ) connection.Connect( node ) connection.LogOn( "AdminExample", "AdminExample" ) # instance a template P = connection.GetObject("Test") #parent T = connection.GetObject("Test.Test Bulk Data.Ten Points") # A template I = P.CreateInstance(T, "New Instance") # Create an instance # set string point value P = connection.GetObject("Test.New String Point") # A string point P.InvokeMethod("CurrentValue", "fred" ) # get object children FieldDevice = connection.GetObject("Test") Children = FieldDevice.GetChildren("CDBPoint","") for value in Children: print(value.FullName) # set an object's Geo Location FieldDevice = connection.GetObject("SELogger.2820015789 DLLTE4-SA.Logger" ) GeoAgg = FieldDevice.Aggregates["GISLocationSource"]; print( GeoAgg.Enabled); GeoAgg.ClassName = "CGISLocationSrcDynamic" print( GeoAgg.ClassName); GeoAgg["Latitude"] = 3 GeoAgg["Longitude"] = 4 # Find and set internal point values in history pointObject = connection.GetObject("Example Projects.Oil and Gas.Transportation.Graphics.End Station.Valve 3.Position Control" ) for i in range(1,100,30): pointObject.InvokeMethod("CurrentValue", i ) print( "Point set to: " + str(pointObject.GetProperty("CurrentValue" ) ) ) # Find a historic point pointObject2 = connection.GetObject("Example Projects.Oil and Gas.Transportation.Inflow Computer.GasFlow" ) from System import DateTime # To support .Net date/time # Historic arguments are start, end, index(=0), maxrecords, forwards=true, reason="All" hisStart = DateTime( 2021,1,19,0,0,0 ) hisEnd = DateTime( 2021,1,20,0,0,0 ) hisArgs = [ hisStart, hisEnd, 0, 100, True, "All" ] # Call methods to get values and times. Could also read quality, or use .ProcessedValue to get fixed interval data hisValues = pointObject2.InvokeMethod("Historic.RawValues", hisArgs ) hisQualities = pointObject2.InvokeMethod("Historic.RawQualities", hisArgs ) hisTimeStamps = pointObject2.InvokeMethod("Historic.RawTimestamps", hisArgs ) for i in range( hisTimeStamps.Length): print( hisTimeStamps[i], hisValues[i], hisQualities[i] )
Please mark as a solution!
Discuss challenges in energy and automation with 30,000+ experts and peers.
Find answers in 10,000+ support articles to help solve your product and business challenges.
Find peer based solutions to your questions. Provide answers for fellow community members!