Exemple #1
0
        //同步写数据
        private void Btn_Write_Click(object sender, EventArgs e)
        {
            IntPtr pErrors = IntPtr.Zero;

            object[] values = new object[2];
            values[0] = Txt_W1.Text;
            values[1] = Txt_W2.Text;
            try
            {
                IOPCSyncIO20Obj.Write(2, ItemServerHandle, values, out pErrors);
                int[] errors = new int[2];
                Marshal.Copy(pErrors, errors, 0, 2);
                String pstrError;
                String pstrError1;
                ServerObj.GetErrorString(errors[0], LOCALE_ID, out pstrError);
                ServerObj.GetErrorString(errors[1], LOCALE_ID, out pstrError1);
            }
            catch (System.Exception error)
            {
                MessageBox.Show(error.Message, "Result-WriteItem", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (pErrors != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pErrors);
                    pErrors = IntPtr.Zero;;
                }
            }
        }
Exemple #2
0
        public bool SyncWrite(object[] values, int[] itemHandle) //由编程人员保证,所写数据和添加Item的数据说明相对应
        {
            IntPtr pErrors  = IntPtr.Zero;
            bool   isWrited = false;

            try
            {
                if (values.Length != itemHandle.Length)
                {
                    MessageBox.Show(string.Format("写入数据的个数与添加Item的数据说明长度不一致"), "写数据出错",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
                IOPCSyncObj.Write(values.Length, itemHandle, values, out pErrors);
                int[] errors = new int[values.Length];
                Marshal.Copy(pErrors, errors, 0, values.Length);
                for (int i = 0; i < values.Length; i++)
                {
                    if (errors[i] != 0)   //写数据不成功
                    {
                        String pstrError; //需不需要释放?
                        ServerObj.GetErrorString(errors[i], LOCALE_ID, out pstrError);
                        MessageBox.Show(string.Format("写入第{0}个数据时出错:{1}", i, pstrError), "写数据出错",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        isWrited = false;
                        break;
                    }
                    else
                    {
                        isWrited = true;
                    }
                }
            }
            catch (System.Exception error)
            {
                isWrited = false;
                MessageBox.Show(error.Message, "写数据出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (pErrors != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pErrors);
                    pErrors = IntPtr.Zero;
                }
            }

            return(isWrited);
        }
Exemple #3
0
        /* 读取所有设备的数据,由上层应用调用,传递全局DeviceData[]引用 */
        /* 这个函数必须在ConnectOPCServerOfBCNetS7函数执行成功后才能调用 */
        /// <summary>
        /// 读取标签值
        /// </summary>
        /// <param name="value"></param>
        public static void ReadRunTimeData(ref Int32 value,
                                           ref Int32 qua,
                                           ref Int32 reserved,
                                           ref string time,
                                           ref string type,
                                           ref string hclient,
                                           ref string hashcode)
        {
            if (connectedOK == false)
            {
                return;
            }
            if (InitReqIOInterfaces() == false)
            {
                return;
            }
            IntPtr pItemValues = IntPtr.Zero;                                        /* 标签值指针 */
            IntPtr pErrors     = IntPtr.Zero;                                        /* 错误值指针 */

            int[]        errors = new int[1];                                        /* 保存所有错误 */
            OPCITEMSTATE pItemState;                                                 /* 标签状态 */

            for (int GroupIndex = 0; GroupIndex < 1; GroupIndex++)                   /* 读取每个组的所有标签值 */
            {
                if (pIOPCSyncIO[GroupIndex] != null)                                 /* OPC同步接口有效 */
                {
                    try
                    {
                        for (int i = 0; i < 1; i++)                                  /* 获得该组的所有标签的服务器句柄 */
                        {
                            tItemSvrHandleArray[i] = ItemSvrHandleArray[GroupIndex, i];
                        }
                        pIOPCSyncIO[GroupIndex].Read(OPCDATASOURCE.OPC_DS_DEVICE, 1, tItemSvrHandleArray, out pItemValues, out pErrors); /* 读取标签值 */
                        IntPtr pos = pItemValues;                                                                                        /* 定位ItemValues的指针 */
                        Marshal.Copy(pErrors, errors, 0, 1);                                                                             /* pErrors->errors */
                        if (pErrors != IntPtr.Zero)                                                                                      /* 释放pErrors的内存块 */
                        {
                            Marshal.FreeCoTaskMem(pErrors);
                            pErrors = IntPtr.Zero;
                        }
                        Boolean CheckAllBadQuality = true;                           /* 检查所有标签是否为BAD的标记 */
                        for (int i = 0; i < 1; i++)                                  /* 从pItemState中获得每个标签的值 */
                        {
                            if (errors[i] == 0)                                      /* 如果无错误 */
                            {
                                if (i != 0)                                          /* 如果非第一个标签则指针移动一个OPCITEMSTATE */
                                {
                                    pos = new IntPtr(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE)));
                                }
                                pItemState         = (OPCITEMSTATE)Marshal.PtrToStructure(pos, typeof(OPCITEMSTATE));           /* pos->OPCITEMSTATE */
                                CheckAllBadQuality = CheckAllBadQuality && (pItemState.wQuality != Qualities.OPC_QUALITY_GOOD); /* 如果所有标签质量都为非GOOD则为true */
                                /*测试标签的其他字段具体的值*/
                                if (pItemState.vDataValue == null)
                                {
                                    value = 0;
                                }
                                else
                                {
                                    value = Int32.Parse(String.Format("{0}", pItemState.vDataValue));
                                }
                                qua      = Int32.Parse(String.Format("{0}", pItemState.wQuality));
                                reserved = Int32.Parse(String.Format("{0}", pItemState.wReserved));
                                time     = String.Format("{0}", pItemState.ftTimeStamp);
                                type     = pItemState.GetType().ToString();
                                hclient  = String.Format("{0}", pItemState.hClient);
                                hashcode = pItemState.GetHashCode().ToString();
                            }
                            else
                            {
                                String strResult = "";
                                Serverobj.GetErrorString(errors[i], LOCALE_ID, out strResult);
                                // OutputExceptionToDatabase(.......)                                                   /* 将异常信息输出到数据库 */
                            }
                        }

                        /* 回收非托管资源 */
                        if (pItemValues != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(pItemValues);
                            pItemValues = IntPtr.Zero;
                        }
                    }
                    catch (System.Exception error)                                                        /* IOPCSyncIO::Read函数执行异常 */
                    {
                        MessageBox.Show(error.Message);                                                   /* 将异常信息输出到数据库 */
                    }
                    finally                                                                               /* 回收非托管资源 */
                    {
                        if (pItemValues != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(pItemValues);
                            pItemValues = IntPtr.Zero;
                        }
                    }
                }
            }
            if (pItemValues != IntPtr.Zero)                                                                             /* 回收非托管资源 */
            {
                Marshal.FreeCoTaskMem(pItemValues);
                pItemValues = IntPtr.Zero;
            }
        }