www.gusucode.com > opc 案例源码 matlab代码程序 > opc/demoUA_historicalData.m
%% Read Historical OPC UA Server Data % This example shows you how to read historical data from an OPC UA server. Specifically, this example reads data from % the OPC Foundation Quickstart Historical Access Server. % % *PREREQUISITES:* % % * <docid:opc_examples.example-ex55000998> % Copyright 2015-2016 The MathWorks, Inc. %% Create a Client and Connect to the Server % You create client objects using the results of a query to the Local Discovery Service using |opcuaserverinfo|, or % directly using the host name and port number of the server you are connecting to. In this case, find the server with a % name containing 'Quickstart Historical'. serverList = opcuaserverinfo('localhost'); historicalServer = findDescription(serverList,'Quickstart Historical'); uaClient = opcua(historicalServer); connect(uaClient); uaClient.Status %% Define Nodes to Read Historical Data % The Quickstart Historical Access Server simulates some historical data for nodes in the namespace. Two sets of data % are provided: Sample data, which is a small data set, and Dynamic data, which varies in size depending on when the % server was started. Define these nodes using the |opcuanode| function. sampleNodeIds = {'1:Quickstarts.HistoricalAccessServer.Data.Sample.Double.txt'; '1:Quickstarts.HistoricalAccessServer.Data.Sample.Float.txt'; '1:Quickstarts.HistoricalAccessServer.Data.Sample.Int32.txt'}; sampleNodes = opcuanode(2,sampleNodeIds,uaClient) dynamicNodeIds = strrep(sampleNodeIds,'Sample','Dynamic'); dynamicNodes = opcuanode(2,dynamicNodeIds,uaClient) %% (Server-Specific) Discover When the Server Was Launched % The Quickstart Historical Access server creates a simulated archive of data from the beginning of the hour that the % server was started to the start time of the server (for the Dynamic nodes). To provide reasonable time ranges over % which to retrieve data, query the server for the start time. % % All servers must publish a ServerStatus node which includes information on the start time of the server. You can read % this value directly from the OPC server, using the |getServerStatus| function. Calculate the start and end times of the % history stored by the server. serverStatus = getServerStatus(uaClient) serverStartTime = serverStatus.StartTime; startHistory = dateshift(serverStartTime,'start','hour') endHistory = serverStartTime %% Read Historical Data from Nodes % Use the |readHistorical| function to read the history of a node. You must pass a time range in which to read % historical data. For the Quickstart server sample data, read the first 2 minutes of data. dataSample = readHistory(uaClient,sampleNodes,startHistory,startHistory+minutes(2)) %% Read Historical Data at Specific Times % You can ask the servfer to retrieve data at specific times. If the server does not have an archived value for that % specific time, an interpolated (or extrapolated) value is returned. Use the |readAtTime| function to retrieve data % each 5 minutes for two hours. timesToReturn = startHistory:minutes(5):endHistory; dataRegular = readAtTime(uaClient,dynamicNodes,timesToReturn) %% Read Processed Data from the Server % OPC UA Servers provide aggregate functions for returning preprocessed data to clients. This is most useful when you % need to query data over a large period of time. % % Query the |AggregateFunctions| property of a connected client to find out what aggregate functions the server supports. uaClient.AggregateFunctions %% % Read the Average value for each 10 second period over 2 minutes. Note how the quality of the data includes Good quality, % Bad quality (where there is no data available to perform the calculation), and Uncertain quality (where insufficient % data is available to classify it as Good). dataAverage = readProcessed(uaClient,dynamicNodes,'Average',seconds(10),startHistory,startHistory+minutes(2)) %% % Filter the quality of the data to return only the Good data. dataGood = filterByQuality(dataAverage,'good') %% Disconnect from Server % When you have finished communicating with the server, disconnect the client from the server. This is also % automatically performed when the client variable goes out of scope in MATLAB(R). disconnect(uaClient);