简体   繁体   English

从OPC服务器读取C#项

[英]Reading in C# items from an OPC Server

I'm reading OPC items from the OPC server KepServer V5 every 10 seconds to a file, but it's reading every 10 seconds twice!!. 我每10秒从OPC服务器KepServer V5读取OPC项到一个文件,但每10秒读取两次。 (i don't want to read 2x every 10 seconds, but only 1 time!) (我不想每10秒读取2倍,而只读取1次!)

Here is my code. 这是我的代码。

OPCController class: OPCController类:

    public void AddItems(string plcPath)
    {

        if (server.IsConnected || group.Active)
        {
            try
            {
                Opc.Da.Item[] items = new Opc.Da.Item[3];
                items[0] = new Opc.Da.Item();
                items[0].ItemName = “PLC1.Value1″;
                items[1] = new Opc.Da.Item();
                items[1].ItemName = “PLC1.Value2″;
                items[2] = new Opc.Da.Item();
                items[2].ItemName = “PLC1.Value3″;

                group.AddItems(items);
            }
            catch (Exception e)
            {
                MessageBox.Show("Tags not read.");
            }
        }

    }

    public void ReadItems()
    {
        Opc.IRequest req;
        group.Read(group.Items, 123, new Opc.Da.ReadCompleteEventHandler(ReadCompleteCallback), out req);
        Console.ReadLine();
    }


static void ReadCompleteCallback( object clientHandle, Opc.Da.    ItemValueResult[] results)
{
Console.WriteLine( "Read complete" );
DateTime dataTimeNow = DateTime.Now;
foreach (Opc.Da.ItemValueResult readResult in results)
{
Console.WriteLine( "\Timestamp{0}\tValue:{1}" , (dataTimeNow.ToString("")), readResult.Value);
}
Console.WriteLine();
}

Forms class: 表格类:

private void startRead_btn_Click(object sender, EventArgs e)
    {
            int setTimerInterval = (int)this.numSetTimer.Value;
            timer1.Enabled = true;
            timer1.Interval = (setTimerInterval) * (1000);
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
            startRead_btn.Enabled = false;
            endRead_btn.Enabled = true;
    }

 private void timer1_Tick(object sender, EventArgs e)
    {
        opcController.ReadItems();
    }

Output file: 输出文件:

TimeStamp: 3/2/2015 3:10:15 PM Value: 12159
TimeStamp: 3/2/2015 3:10:15 PM Value: 12162

TimeStamp: 3/2/2015 3:10:25 PM Value: 12168
TimeStamp: 3/2/2015 3:10:25 PM Value: 12169

TimeStamp: 3/2/2015 3:10:35 PM Value: 12177
TimeStamp: 3/2/2015 3:10:35 PM Value: 12178

You have literally added the same groups twice: 您实际上已经两次添加了相同的组:

try
{
    //...

    items = group.AddItems(items);     //you add them HERE

    group.AddItems(items);             //and HERE
}

By adding each object twice, they will be read twice from the server in a single refresh cycle. 通过将每个对象添加两次,可以在一个刷新周期内从服务器读取两次。

Remove either line, and it should only read once. 删除任一行,它应该只能读取一次。

You are only getting the read results for 2 items! 您仅获得2个项目的读取结果! Your console is writing each item it reads and it is only reading 2 are you sure the code you have posted is the code that generated the output ? 您的控制台正在写入它读取的每个项目,并且仅读取2。您确定所发布的代码是生成输出的代码吗?

Place the ReadResult.ItemName in your console.Writeline method as another string output - and you will see that is the case. 将ReadResult.ItemName放在console.Writeline方法中,作为另一个字符串输出-您将看到这种情况。

foreach (Opc.Da.ItemValueResult readResult in results)
{
Console.WriteLine( "\Timestamp{0}\tValue:{1}" , (dataTimeNow.ToString("")), readResult.Value);
}

Also on a side note: 另请注意:

try{ ...

   group.AddItems(items);
            }
            catch (Exception e)
            {
                MessageBox.Show("Tags not read."); 
}

You are only adding items to the group here there is no READ method. 您仅将项目添加到组中,这里没有READ方法。 the error should be something else. 错误应该是其他原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM