[英]BrowseTags method (Collector object) | VBA vs C#
For a while i've been trying to make a standalone program in c#, which uses the BrowseTags function from iHistorian_SDK. 一段时间以来,我一直在尝试使用c#制作一个独立程序,该程序使用iHistorian_SDK中的BrowseTags函数。 (iFix 5.8 and Historian 7.0)
(iFix 5.8和Historian 7.0)
First I made this function in VBA where it works great, but due to VBA being single threaded, I want to moive it out of VBA. 首先,我在VBA上使该功能很好用,但是由于VBA是单线程的,因此我想将其从VBA中删除。
My VBA code that works today: 我今天可以使用的VBA代码:
Public connectedServer As Object
Public myServerManager As Object
Private Sub TestBrowseFunction()
Call BrowseTagsFromHistorianCollector("SVNF-IFIX-HIS01", "SVNF-IFIX-SCA01_iFIX")
End Sub
Public Function BrowseTagsFromHistorianCollector(ByVal HistServer As String, ByVal HistCollector As String, Optional AdditionsOnly As Boolean = False, Optional SourceFilter As String = "*", Optional DescriptionFilter As String = "*")
On Error Resume Next
Dim MyTags As Variant
Dim Tag As Variant
Set connectedServer = Nothing
Set MyServerManager = CreateObject("iHistorian_SDK.ServerManager")
DoEvents
'Make sure Historian is installed correctly'
If MyServerManager Is Nothing Then
Err.Raise 0, , "Error Creating iHistorian Server Manager - please check to see if Historain Client is correctly installed on your system", vbOKOnly, "test"
Exit Function
End If
'Create iHistorian server object'
Set connectedServer = CreateObject("iHistorian_SDK.Server")
'Check to see if the connection is established, else connect.'
If CheckConnection = False Then connectedServer.Connect (HistServer)
If CheckConnection = True Then
'Browse the collector for tags.'
Set MyTags = connectedServer.collectors.Item(HistCollector).BrowseTags(AdditionsOnly, SourceFilter, DescriptionFilter)
'Loop all the tags from the collector'
For Each Tag In MyTags.Item
'INSERT CODE TO DO FOR EACH TAG HERE!'
Debug.Print Tag.tagName
Next
End If
End Function
' make sure that we are connected to a server'
Public Function CheckConnection() As Boolean
On Error GoTo errc
If connectedServer Is Nothing Then
CheckConnection = False
Exit Function
End If
If Not connectedServer.Connected Then
CheckConnection = False
Exit Function
End If
If connectedServer.ServerTime < CDate("1/1/1970") Then
CheckConnection = False
Exit Function
End If
CheckConnection = True
Exit Function
errc:
CheckConnection = False
End Function
This works great. 这很好。 But in my attempt to convert the same function over to C# i keep getting errors.
但是,在尝试将相同的函数转换为C#的过程中,我不断遇到错误。
First i connect to my historian server, which is pretty painless. 首先,我连接到我的历史服务器,这非常轻松。
tsStatus.Text = "Connecting to " + HistServer;
try
{
connectedServer = new iHistorian_SDK.Server();
connectedServer.Connect(HistServer);
tsStatus.Text = "Connected to " + HistServer;
}
catch (Exception ex)
{
Debug.Print("Server connection threw exception: " + ex);
tsStatus.Text = "Failed connecting to " + HistServer;
}
My status label before i try to connect: 我尝试连接之前的状态标签:
My status label after i try to connect: 我尝试连接后的状态标签:
After the connection is established, I would like to be able to do something like what i've done in VBA. 建立连接后,我希望能够像在VBA中一样进行操作。
Set MyTags = connectedServer.collectors.Item(HistCollector).BrowseTags(AdditionsOnly, SourceFilter, DescriptionFilter)
My c# attempt goes as follows 我的C#尝试如下
iHistorian_SDK.TagRecordset MyTags;
MyTags = new iHistorian_SDK.TagRecordset();
MyTags = connectedServer.Collectors.Item("SVNF-IFIX-SCA01_iFIX").BrowseTags(false, "*", "*");
Does anyone know how I can come around this, or if it's even possible in C# to browse tags with the same methode of the collector object. 有谁知道我该如何解决这个问题,或者是否有可能在C#中使用与收集器对象相同的方法浏览标签。
I've seen this video a few times so I would assume it's possible, they just skip the code where they actually browse tags. 我已经看过几次这个视频,所以我认为这是可能的,他们只是跳过他们实际浏览标签的代码。
Thanks in advance /T 预先感谢/ T
The parenthesis in VBA is an indexer. VBA中的括号是一个索引器。 Try to replace
.Collectors.Item.("...")
with .Collectors.Item.["..."]
尝试将
.Collectors.Item.("...")
替换为.Collectors.Item.["..."]
If you check the source code for the video link you provided (Ie The client SDK sample), they aren't using the collectors to query the tags. 如果您检查提供的视频链接的源代码(即客户端SDK示例),则它们不会使用收集器来查询标签。
cmdBrowseTags_Click is using the ITags "Query" method to 'Browse Tags'. cmdBrowseTags_Click使用ITags的“查询”方法来“浏览标签”。
Here is the GE provided help doco example included in "iHistClientAccessAPI.chm": 这是“ iHistClientAccessAPI.chm”中包含的GE提供的帮助doco示例:
TagQueryParams query = new TagQueryParams();
List<Tag> list = new List<Tag>(), temp = null;
query.Criteria.TagnameMask = "*";
// simple query
connection.ITags.Query(ref query, out list);
// paged query
list.Clear();
query.PageSize = 100; // return at most 100 results per request
while (connection.ITags.Query(ref query, out temp))
list.AddRange(temp);
list.AddRange(temp);
I prefer something like this (includes the server connection for completeness): 我喜欢这样的东西(包括服务器连接以确保完整性):
ServerConnection serverConnection = new ServerConnection(
new ConnectionProperties
{
ServerHostName = "MyHistorianHostName",
Username = "MyUserName",
Password = "MyPassword",
ServerCertificateValidationMode = CertificateValidationMode.None
});
serverConnection.Connect();
if (serverConnection.IsConnected())
{
List<Tag> tagList = new List<Tag>();
TagQueryParams tagQueryParams = new TagQueryParams
{
Criteria = new TagCriteria { TagnameMask = "*" }, // Wilcard, get all tags.
Categories = Tag.Categories.All, // Change this to Basic fo mimimal info.
PageSize = 100 // The batch size of the while loop below..
};
bool isQuery = true;
while (isQuery)
{
isQuery = serverConnection.ITags.Query(ref tagQueryParams, out var tags);
tagList.AddRange(tags);
}
// At this point, tagList contains a list of all tags that matched your wildcard filter.
// To filter to a specific collector, you could then do something like:
var collectorTagList = tagList.Where(t => t.CollectorName == "MyCollector");
}
Goodluck :) 祝好运 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.