В этом примере показано, как считать исторические данные с сервера OPC UA. В частности, в этом примере считываются данные с сервера симуляции OPC UA Prosys 4.0.0 или более поздней версии.
Чтобы запустить этот пример в своем Сеансе работы с MATLAB, вам нужно будет установить Prosys OPC UA Симуляции Server. Для получения дополнительной информации ознакомьтесь с разделом «Начало работы» документации OPC Toolbox.
Объекты клиента создаются с помощью результатов запроса к локальной службе обнаружения с помощью opcuaserverinfo
, или непосредственно используя имя хоста и номер порта сервера, к которому вы подключаетесь. В этом случае подключитесь непосредственно к серверу OPC UA на порте 53530.
uaClient = opcua('localhost',53530);
connect(uaClient);
uaClient.Status
ans = 'Connected'
Prosys OPC UA Simulation Server обеспечивает имитированные сигналы для узлов в ветви «Simulation». По умолчанию Simulation Server обновляет значения каждую секунду. Определите эти узлы с помощью opcuanode
функция.
simNodeIds = {'Random'; 'Triangle'; 'Sinusoid'}; simNodes = opcuanode(3,simNodeIds,uaClient)
simNodes = 1×3 OPC UA Node array: index Name NsInd Identifier NodeType ----- -------- ----- ---------- -------- 1 Random 3 Random Variable 2 Triangle 3 Triangle Variable 3 Sinusoid 3 Sinusoid Variable
Используйте readHistory
функция для чтения истории узла. Необходимо пройти временную область значений, в котором можно считать исторические данные. Для сервера Prosys прочитайте последние 30 секунд данных.
dataSample = readHistory(uaClient,simNodes,datetime('now')-seconds(30),datetime('now'))
dataSample = 1-by-3 OPC UA Data object array: Timestamp Random Triangle Sinusoid ----------------------- -------------------------- -------------------------- -------------------------- 2019-12-20 01:18:14.000 1.402465 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:18:15.000 1.044139 [Good (Raw)] 0.000000 [Good (Raw)] 0.000000 [Good (Raw)] 2019-12-20 01:18:16.000 -1.857952 [Good (Raw)] -0.266667 [Good (Raw)] -0.415823 [Good (Raw)] 2019-12-20 01:18:17.000 1.783723 [Good (Raw)] -0.533333 [Good (Raw)] -0.813473 [Good (Raw)] 2019-12-20 01:18:18.000 -1.095435 [Good (Raw)] -0.800000 [Good (Raw)] -1.175570 [Good (Raw)] 2019-12-20 01:18:19.000 -1.178567 [Good (Raw)] -1.066667 [Good (Raw)] -1.486290 [Good (Raw)] 2019-12-20 01:18:20.000 -1.548359 [Good (Raw)] -1.333333 [Good (Raw)] -1.732051 [Good (Raw)] 2019-12-20 01:18:21.000 -0.438983 [Good (Raw)] -1.600000 [Good (Raw)] -1.902113 [Good (Raw)] 2019-12-20 01:18:22.000 -0.785842 [Good (Raw)] -1.866667 [Good (Raw)] -1.989044 [Good (Raw)] 2019-12-20 01:18:23.000 1.419149 [Good (Raw)] -1.866667 [Good (Raw)] -1.989044 [Good (Raw)] 2019-12-20 01:18:24.000 1.049357 [Good (Raw)] -1.600000 [Good (Raw)] -1.902113 [Good (Raw)] 2019-12-20 01:18:25.000 -1.932999 [Good (Raw)] -1.333333 [Good (Raw)] -1.732051 [Good (Raw)] 2019-12-20 01:18:26.000 1.720142 [Good (Raw)] -1.066667 [Good (Raw)] -1.486290 [Good (Raw)] 2019-12-20 01:18:27.000 -1.170482 [Good (Raw)] -0.800000 [Good (Raw)] -1.175571 [Good (Raw)] 2019-12-20 01:18:28.000 -1.540274 [Good (Raw)] -0.533333 [Good (Raw)] -0.813473 [Good (Raw)] 2019-12-20 01:18:29.000 -0.430899 [Good (Raw)] -0.266667 [Good (Raw)] -0.415823 [Good (Raw)] 2019-12-20 01:18:30.000 -0.869489 [Good (Raw)] -0.000000 [Good (Raw)] -0.000000 [Good (Raw)] 2019-12-20 01:18:31.000 -1.630916 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:18:32.000 1.999292 [Good (Raw)] 0.533333 [Good (Raw)] 0.813473 [Good (Raw)] 2019-12-20 01:18:33.000 -0.891333 [Good (Raw)] 0.800000 [Good (Raw)] 1.175570 [Good (Raw)] 2019-12-20 01:18:34.000 -1.238192 [Good (Raw)] 1.066667 [Good (Raw)] 1.486290 [Good (Raw)] 2019-12-20 01:18:35.000 -0.220548 [Good (Raw)] 1.333333 [Good (Raw)] 1.732051 [Good (Raw)] 2019-12-20 01:18:36.000 -0.590339 [Good (Raw)] 1.600000 [Good (Raw)] 1.902113 [Good (Raw)] 2019-12-20 01:18:37.000 0.519036 [Good (Raw)] 1.866667 [Good (Raw)] 1.989044 [Good (Raw)] 2019-12-20 01:18:38.000 0.172177 [Good (Raw)] 1.866667 [Good (Raw)] 1.989044 [Good (Raw)] 2019-12-20 01:18:39.000 -0.589250 [Good (Raw)] 1.600000 [Good (Raw)] 1.902113 [Good (Raw)] 2019-12-20 01:18:40.000 -0.959042 [Good (Raw)] 1.333333 [Good (Raw)] 1.732051 [Good (Raw)] 2019-12-20 01:18:41.000 0.425527 [Good (Raw)] 1.066667 [Good (Raw)] 1.486290 [Good (Raw)] 2019-12-20 01:18:42.000 0.078668 [Good (Raw)] 0.800000 [Good (Raw)] 1.175571 [Good (Raw)] 2019-12-20 01:18:43.000 1.188043 [Good (Raw)] 0.533333 [Good (Raw)] 0.813473 [Good (Raw)]
Можно попросить сервер получить данные в определенное время. Если сервер не имеет заархивированного значения за это конкретное время, возвращается интерполированное (или экстраполированное) значение. Используйте readAtTime
функция для извлечения данных каждую минуту в течение последних 10 минут.
timesToReturn = datetime('now')-minutes(10):minutes(1):datetime('now'); dataRegular = readAtTime(uaClient,simNodes,timesToReturn)
dataRegular = 1-by-3 OPC UA Data object array: Timestamp Random Triangle Sinusoid ----------------------- -------------------------- -------------------------- -------------------------- 2019-12-20 01:08:44.000 -0.083361 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:09:44.000 0.043744 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:10:44.000 1.199272 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:11:44.000 1.259184 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:12:44.000 0.193783 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:13:44.000 -1.585967 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:14:44.000 1.073438 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:15:44.000 0.099768 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:16:44.000 -1.368735 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:17:44.000 1.791922 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)] 2019-12-20 01:18:44.000 0.818252 [Good (Raw)] 0.266667 [Good (Raw)] 0.415823 [Good (Raw)]
OPC UA Servers обеспечивает совокупные функции для возврата предварительно обработанных данных клиентам. Это наиболее полезно, когда необходимо запросить данные в течение большого периода времени.
Запросите AggregateFunctions
свойство подключенного клиента, чтобы узнать, какие совокупные функции поддерживает сервер.
uaClient.AggregateFunctions
ans = 14×1 cell array {'Interpolative' } {'Average' } {'Minimum' } {'Maximum' } {'MinimumActualTime'} {'MaximumActualTime'} {'Range' } {'Count' } {'Start' } {'End' } {'Delta' } {'WorstQuality' } {'StartBound' } {'EndBound' }
Считайте среднее значение для каждого 30 второго периода за последние 10 минуты.
dataAverage = readProcessed(uaClient,simNodes,'Average',seconds(30),datetime('now')-minutes(10),datetime('now'))
dataAverage = 1-by-3 OPC UA Data object array: Timestamp Random Triangle Sinusoid ----------------------- --------------------------------- --------------------------------- --------------------------------- 2019-12-20 01:08:44.000 -0.008396 [Good (Calculated)] -0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:09:14.000 0.071422 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:09:44.000 0.034084 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:10:14.000 0.190256 [Good (Calculated)] -0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:10:44.000 0.088148 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:11:14.000 0.065122 [Good (Calculated)] 0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:11:44.000 -0.057444 [Good (Calculated)] 0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:12:14.000 -0.047782 [Good (Calculated)] 0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:12:44.000 0.253328 [Good (Calculated)] 0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:13:14.000 -0.018746 [Good (Calculated)] 0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:13:44.000 0.103775 [Good (Calculated)] 0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:14:14.000 0.010857 [Good (Calculated)] 0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:14:44.000 -0.370672 [Good (Calculated)] 0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:15:14.000 -0.198687 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:15:44.000 -0.025481 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:16:14.000 0.067565 [Good (Calculated)] -0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:16:44.000 0.085904 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:17:14.000 0.018061 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)] 2019-12-20 01:17:44.000 -0.033414 [Good (Calculated)] -0.000000 [Good (Calculated)] 0.000000 [Good (Calculated)] 2019-12-20 01:18:14.000 -0.205573 [Good (Calculated)] -0.000000 [Good (Calculated)] -0.000000 [Good (Calculated)]
Считайте Среднее значение для каждой половины второго периода за последние 5 секунд. Обратите внимание, что качество данных включает Хорошее качество и Плохое качество, где нет данных, доступных для выполнения вычисления.
dataMixedQuality = readProcessed(uaClient,simNodes,'Average',seconds(0.5),datetime('now')-seconds(5),datetime('now'))
dataMixedQuality = 1-by-3 OPC UA Data object array: Timestamp Random Triangle Sinusoid ----------------------- --------------------------------- --------------------------------- --------------------------------- 2019-12-20 01:18:39.000 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 2019-12-20 01:18:39.500 -0.589250 [Good (Calculated)] 1.600000 [Good (Calculated)] 1.902113 [Good (Calculated)] 2019-12-20 01:18:40.000 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 2019-12-20 01:18:40.500 -0.959042 [Good (Calculated)] 1.333333 [Good (Calculated)] 1.732051 [Good (Calculated)] 2019-12-20 01:18:41.000 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 2019-12-20 01:18:41.500 0.425527 [Good (Calculated)] 1.066667 [Good (Calculated)] 1.486290 [Good (Calculated)] 2019-12-20 01:18:42.000 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 2019-12-20 01:18:42.500 0.078668 [Good (Calculated)] 0.800000 [Good (Calculated)] 1.175571 [Good (Calculated)] 2019-12-20 01:18:43.000 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 0.000000 [Bad:NoData (Raw)] 2019-12-20 01:18:43.500 1.188043 [Good (Calculated)] 0.533333 [Good (Calculated)] 0.813473 [Good (Calculated)]
Фильтрация качества данных для возврата только хороших данных.
dataGood = filterByQuality(dataMixedQuality,'good')
dataGood = 1-by-3 OPC UA Data object array: Timestamp Random Triangle Sinusoid ----------------------- --------------------------------- --------------------------------- --------------------------------- 2019-12-20 01:18:39.500 -0.589250 [Good (Calculated)] 1.600000 [Good (Calculated)] 1.902113 [Good (Calculated)] 2019-12-20 01:18:40.500 -0.959042 [Good (Calculated)] 1.333333 [Good (Calculated)] 1.732051 [Good (Calculated)] 2019-12-20 01:18:41.500 0.425527 [Good (Calculated)] 1.066667 [Good (Calculated)] 1.486290 [Good (Calculated)] 2019-12-20 01:18:42.500 0.078668 [Good (Calculated)] 0.800000 [Good (Calculated)] 1.175571 [Good (Calculated)] 2019-12-20 01:18:43.500 1.188043 [Good (Calculated)] 0.533333 [Good (Calculated)] 0.813473 [Good (Calculated)]
После завершения связи с сервером отключите клиент от сервера. Это также выполняется автоматически, когда переменная клиента выходит из возможностей в MATLAB ®.
disconnect(uaClient);