I am using OPCLabs dll for subscribing to multiple items of a remote OPC DA server.When getting the first (initial) values from the server it is often the case that some of the values come with quality "bad" or "uncertain".(Especially when I subscribe to large amount of tags). Therefore in my code I am checking whether all the values are with quality "good" and just then I am accepting the values and use for further processing.
But the problem is: When all the initial values (first values after subscribing) come all with quality "good" (what is not always the case) there is no problem. But when I have let say totally 70 tags and in the first cycle only 45 tags are read with quality "good", then in the second reading cycle the reading is stopped after exactly 70-45=25 tags are totally read , all with quality "good" this time. ( I mean 70 minus amount of "good" quality tags of the previous cycle).
I am getting no errors and no exceptions after this. The code is just waiting until the total subscription time (in my case 10 seconds) is finished and then exiting. What could be the problem? Why is my code stopping to read the further values cyclic?
public string item_no = ""; // amount of items
public string[] item_array= new string[100];
public Int16 item_value_read_good = 0;
public Int16 item_value_read_total = 0;
static void Main(string[] args)
{
//some code for filling the item_array coming from the arguments;
item_no = args[2]; // amount of items coming from argument
subscribe_items(10.92.XXX.XXX)
}
public void subscribe_items(string ip)
{
item_value_array=new string[100];
dAItemGroupArguments.Clear();
item_value_read_good = 0;
item_value_read_total = 0;
all_items_read = false;
for (int u=0; u<100; u++)
{
item_value_array[u] = "";
}
for (int i = 0; i < Convert.ToInt16(item_no); i++)
{
dAItemGroupArguments.Add(new DAItemGroupArguments(ip, opcServer , item_array[i], 1000, null));
//Console.WriteLine("item-" + i+ "= " + item_array[i])
}
using (var client = new EasyDAClient())
{
client.ItemChanged += client_Main1_ItemChanged;
client.SubscribeMultipleItems(dAItemGroupArguments.ToArray());
//Console.WriteLine("Processing item changed events for 1 minute...");
Thread.Sleep(10 * 1000);
client.UnsubscribeAllItems();
Environment.Exit(0);
}
}
public void client_Main1_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
{
//Console.WriteLine("item changed: "+ e.Arguments.ItemDescriptor.ItemId);
try
{
if (e.Succeeded)
{
item_value = e.Vtq.ToString();
for (int j = 0; j < Convert.ToInt16(item_no); j++)
{
if (e.Arguments.ItemDescriptor.ItemId == item_array[j])
{
if (item_value.Contains("Good"))
{
item_value_array[j] = e.Vtq.Value.ToString();
item_value_read_good += 1;
}
item_value_read_total += 1;
using (StreamWriter sw = File.AppendText(@"D:\test\opcda_testlog.txt"))
{
sw.WriteLine("item value read total: " + item_value_read_total + " " + Convert.ToString(DateTime.Now));
sw.WriteLine("item value read good: " + item_value_read_good + " item no: " + item_no + " " + Convert.ToString(DateTime.Now));
}
}
}
if (item_value_read_good != Convert.ToInt16(item_no) && item_value_read_total == Convert.ToInt16(item_no))
{
item_value_read_good = 0;
item_value_read_total = 0;
array_item_to_web = "";
item_value_array_to_web = "";
item_value_array = new string[100];
//Console.WriteLine "not all items good read next cycle";
}
if (item_value_read_good == Convert.ToInt16(item_no) && item_value_read_total == Convert.ToInt16(item_no))
{
// all tags read with quality good, process the tags....
}
}
else
{
// Error handling
}
}
catch(Exception ex)
{
// Error Handling
}
}
As @ZbynekZ answered, I noticed that none of the tag values , that I subscribed, was changing. So there was no problem in the code. Sorry to everyone for taking time.