COPC DLL : use _datChange prevent null data from OPC server

At start of program, COPC DLL reading OPC data. Some OPC tags may not ready to read. If you reading those tags, it could be null value and may be the error message in the program. Then COPC DLL has event handler ‘_datChange(int tagIndex)’ to prevent this.

The following example get OPC data only for ready tags that have data moving.

private void Copc1_datChange(int tagIndex)
        {
            switch ( tagIndex)
            {
                case 0:
                    label1.Text = copc1.tgVal(0).ToString();
                    break;
                case 1:
                    label2.Text = copc1.tgVal(1).ToString();
                    break;
                case 2:
                    label3.Text = copc1.tgVal(2).ToString();
                    break;
            }
                

            
        }

You can also use Timer_Tick instead of _datChange event. But please make sure to check null data like the following example. We store OPC data into array before use in several place of program to prevent data bottle neck. You may want to use global array instead of local array in the example as well.

private void timer1_Tick(object sender, EventArgs e)
        {
            dynamic[] myvar;

            try
            {

                for (int i = 0; i <= 2; i++)
                {
                    myvar[i] = copc1.tgVal(i);
                }


            }
            catch (Exception err)
            {
            }

            if (myvar[0] is object) { label1.Text = myvar[0].ToString(); }
            if (myvar[1] is object) { label2.Text = myvar[1].ToString(); }
            if (myvar[2] is object) { label3.Text = myvar[2].ToString(); }


        }
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s