OPC客户端写入项目

时间:2015-03-30 12:25:23

标签: c# opc

我正在制作一个OPC客户端,它使用OPC Foundation API DA 3.0并使用Schneider OFS服务器。 我能够从服务器中读取组中的项目,并且我还能够将短值写入项目。

我在将字符串值写入项目并将浮点值写入项目时遇到困难。

项目的价值不会发生变化。

浮动项命名为"" DevExample_1!OUTPUT_REAL_VALUE"在服务器上。

        string[] groupitems = { "Test_device!INPUT_INT_VALUE", "Test_device!OUTPUT_INT_VALUE", "DevExample_1!INPUT_STRING_VALUE","DevExample_1!OUPUT_STRING_VALUE","DevExample_1!OUTPUT_REAL_VALUE"};
        Opc.Da.Subscription group = CreateGroup(server, "GroupTest", groupitems);

        WriteGroupItemValue(group, "DevExample_1!OUTPUT_REAL_VALUE", 1.5e-02);

    public static Opc.Da.Subscription CreateGroup(Opc.Da.Server server, string groupName, string[] groupitems)
    {
        Opc.Da.SubscriptionState groupState;
        Opc.Da.Subscription groupReadWrite;

        groupState = new Opc.Da.SubscriptionState(); // subscr state
        groupState.Name = groupName;
        groupState.UpdateRate = 1000;// this is the time between every reads from OPC server
        groupState.Active = true;//this must be true if you the group has to read value
        groupReadWrite = (Opc.Da.Subscription)server.CreateSubscription(groupState); // add subscr for groupReadWrite

        itemsLength = groupitems.Length; // determine amount of items

        items = new Opc.Da.Item[itemsLength]; // items array of DA type

        for (int i = 0; i < items.Length; i++)
        {
            items[i] = new Opc.Da.Item(); // create new DA item for every index
            items[i].ItemName = groupitems[i];  
        }
        items = groupReadWrite.AddItems(items);
        return groupReadWrite;
    }

       public static void WriteGroupItemValue(Opc.Da.Subscription group, string itemName, object itemValue){

        Opc.Da.Item[] itemToAddWrite = new Opc.Da.Item[1]; /
        itemToAddWrite[0] = new Opc.Da.Item();
        itemToAddWrite[0].ItemName = itemName;

        Opc.Da.ItemValue[] writeValues = new Opc.Da.ItemValue[1];
        writeValues[0] = new Opc.Da.ItemValue(itemToAddWrite[0]);

        writeValues[0].Value = itemValue; // set value

        writeValues[0].ServerHandle = group.Items[0].ServerHandle;

        group.Write(writeValues); // write the value of the items
    }

0 个答案:

没有答案