public bool SyncRead(object[] values, int[] itemHandle) //同步读,读的结果存放在values中,读成功返回true,失败返回false { IntPtr pItemValues = IntPtr.Zero; IntPtr pErrors = IntPtr.Zero; bool isRead = false; try { if (values.Length != itemHandle.Length) { MessageBox.Show(string.Format("需要读出数据的个数与添加Item的数据说明长度不一致"), "读数据出错", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } IOPCSyncObj.Read(OPCDATASOURCE.OPC_DS_DEVICE, itemHandle.Length, itemHandle, out pItemValues, out pErrors); int[] errors = new int[itemHandle.Length]; Marshal.Copy(pErrors, errors, 0, itemHandle.Length); OPCITEMSTATE pItemState = new OPCITEMSTATE(); for (int i = 0; i < itemHandle.Length; i++) { if (errors[i] == 0) { pItemState = (OPCITEMSTATE)Marshal.PtrToStructure(pItemValues, typeof(OPCITEMSTATE)); values[i] = pItemState.vDataValue.ToString(); //pItemState中还包含质量和时间等信息,目前只使用了读取的数据值 // values[i] = String.Format("{0}", pItemState.vDataValue); //pItemState中还包含质量和时间等信息,目前只使用了读取的数据值 pItemValues = new IntPtr(pItemValues.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); isRead = true; } else { String pstrError; //需不需要释放? ServerObj.GetErrorString(errors[i], LOCALE_ID, out pstrError); MessageBox.Show(string.Format("读出第{0}个数据时出错:{1}", i, pstrError), "读数据出错", MessageBoxButtons.OK, MessageBoxIcon.Error); isRead = false; break; } } } catch (System.Exception error) { isRead = false; MessageBox.Show(error.Message, "Result-Read Items", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (pItemValues != IntPtr.Zero) { Marshal.FreeCoTaskMem(pItemValues); pItemValues = IntPtr.Zero; } if (pErrors != IntPtr.Zero) { Marshal.FreeCoTaskMem(pErrors); pErrors = IntPtr.Zero; } } return(isRead); }
//同步读数据 private void Btn_Read_Click(object sender, EventArgs e) { IntPtr pItemValues = IntPtr.Zero; IntPtr pErrors = IntPtr.Zero; try { IOPCSyncIO20Obj.Read(OPCDATASOURCE.OPC_DS_DEVICE, 2, ItemServerHandle, out pItemValues, out pErrors); int[] errors = new int[2]; Marshal.Copy(pErrors, errors, 0, 2); OPCITEMSTATE[] pItemState = new OPCITEMSTATE[2]; if (errors[0] == 0) { pItemState[0] = (OPCITEMSTATE)Marshal.PtrToStructure(pItemValues, typeof(OPCITEMSTATE)); pItemValues = new IntPtr(pItemValues.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); Txt_R1Value.Text = String.Format("{0}", pItemState[0].vDataValue); Txt_R1Quality.Text = GetQuality(pItemState[0].wQuality); DateTime dt = ToDateTime(pItemState[0].ftTimeStamp); Txt_R1TimeStamp.Text = dt.ToString(); } if (errors[1] == 0) { pItemState[1] = (OPCITEMSTATE)Marshal.PtrToStructure(pItemValues, typeof(OPCITEMSTATE)); pItemValues = new IntPtr(pItemValues.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); Txt_R2Value.Text = String.Format("{0}", pItemState[1].vDataValue); Txt_R2Quality.Text = GetQuality(pItemState[1].wQuality); DateTime dt = ToDateTime(pItemState[1].ftTimeStamp); Txt_R2TimeStamp.Text = dt.ToString(); } } catch (System.Exception error) { MessageBox.Show(error.Message, "Result-Read Items", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { //free the memory if (pItemValues != IntPtr.Zero) { Marshal.FreeCoTaskMem(pItemValues); pItemValues = IntPtr.Zero; } if (pErrors != IntPtr.Zero) { Marshal.FreeCoTaskMem(pErrors); pErrors = IntPtr.Zero; } } }