public List <Result> Write(string[] tag, object[] values) { List <Result> results = new List <Result>(); if (tag.Length != values.Length) { throw new Exception("item和values个数不一致"); } List <OpcDa.ItemValue> _values = new List <OpcDa.ItemValue>(); for (int i = 0; i < tag.Length; i++) { var itmVal = new OpcDa.ItemValue { ItemName = tag[i], Value = values[i] }; } var writresut = _server.Write(_values.ToArray()); foreach (IdentifiedResult r in writresut) { results.Add(new Result { Succeed = r.ResultID == ResultID.S_OK }); } return(results); }
private void OperationWriteOPC() { /* * Opc.Da.Item[] OPC_ItemID = new Opc.Da.Item[3]; * OPC_ItemID[0] = new Opc.Da.Item(); * OPC_ItemID[0].ItemName = BrakePressTag_OPC + "HMI_Operation_One_PB.VALUE"; * OPC_ItemID[1] = new Opc.Da.Item(); * OPC_ItemID[1].ItemName = BrakePressTag_OPC + "HMI_Operation_Two_PB.VALUE"; * OPC_ItemID[2] = new Opc.Da.Item(); * OPC_ItemID[2].ItemName = BrakePressTag_OPC + "HMI_Operation_Three_PB.VALUE"; * OPC_ItemID = OperationSelection_Write.AddItems(OPC_ItemID); */ Opc.Da.Item[] OPC_ItemID = new Opc.Da.Item[3]; OPC_ItemID[0] = new Opc.Da.Item(); OPC_ItemID[0].ItemName = BrakePressTag_OPC + "HMI_Operation_One_PB"; OPC_ItemID[1] = new Opc.Da.Item(); OPC_ItemID[1].ItemName = BrakePressTag_OPC + "HMI_Operation_Two_PB"; OPC_ItemID[2] = new Opc.Da.Item(); OPC_ItemID[2].ItemName = BrakePressTag_OPC + "HMI_Operation_Three_PB"; OPC_ItemID = OperationSelection_Write.AddItems(OPC_ItemID); Opc.Da.ItemValue[] ItemID_OPCValue = new Opc.Da.ItemValue[3]; ItemID_OPCValue[0] = new Opc.Da.ItemValue(); ItemID_OPCValue[1] = new Opc.Da.ItemValue(); ItemID_OPCValue[2] = new Opc.Da.ItemValue(); switch (SelectedOperation) { case 1: // Operation #1 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 1; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 0; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 0; break; case 2: // Operation #2 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 0; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 1; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 0; break; case 3: // Operation #3 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 0; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 0; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 1; break; } Opc.IRequest OPCRequest; OperationSelection_Write.Write(ItemID_OPCValue, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); }
/// <summary> /// 写入整组的数据 /// </summary> /// <param name="index"></param> /// <param name="itemNames"></param> /// <param name="values"></param> public void writeOpc(int index, string[] itemNames, string[] values) { if (itemNames.Length != values.Length) { throw new Exception("变量数组和赋值数组必须长度相同"); } Opc.Da.ItemValue[] opcItemValues = new Opc.Da.ItemValue[itemNames.Length]; int m = 0; foreach (Opc.Da.Item opcItem in opcServer.Subscriptions[index].Items) { for (int i = 0; i < itemNames.Length; i++) { if (opcItem.ItemName.Equals(itemNames[i])) { opcItemValues[m] = new Opc.Da.ItemValue(opcItem); opcItemValues[m].Value = values[i]; m++; break; } } } if (opcItemValues == null) { throw new Exception("该组中没有相应的变量!"); } opcServer.Write(opcItemValues); }
/// <summary> /// Writes the value of the specified item property. /// </summary> public Opc.IdentifiedResult Write( string itemID, PropertyID propertyID, Opc.Da.ItemValue value) { return(null); }
private void WriteComponentThree_OPC() { Opc.Da.Item[] OPC_CompThreeWrite = new Opc.Da.Item[4]; OPC_CompThreeWrite[0] = new Opc.Da.Item(); OPC_CompThreeWrite[0].ItemName = SpotWeld_Tag_Name + "HMI_Operation_One_PB"; OPC_CompThreeWrite[1] = new Opc.Da.Item(); OPC_CompThreeWrite[1].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Two_PB"; OPC_CompThreeWrite[2] = new Opc.Da.Item(); OPC_CompThreeWrite[2].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Three_PB"; OPC_CompThreeWrite[3] = new Opc.Da.Item(); OPC_CompThreeWrite[3].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Four_PB"; OPC_CompThreeWrite = GroupWriteCompThree.AddItems(OPC_CompThreeWrite); Opc.Da.ItemValue[] WriteCompThreeValue = new Opc.Da.ItemValue[4]; WriteCompThreeValue[0] = new Opc.Da.ItemValue(); WriteCompThreeValue[0].ServerHandle = GroupWriteCompThree.Items[0].ServerHandle; WriteCompThreeValue[0].Value = 0; WriteCompThreeValue[1] = new Opc.Da.ItemValue(); WriteCompThreeValue[1].ServerHandle = GroupWriteCompThree.Items[1].ServerHandle; WriteCompThreeValue[1].Value = 0; WriteCompThreeValue[2] = new Opc.Da.ItemValue(); WriteCompThreeValue[2].ServerHandle = GroupWriteCompThree.Items[2].ServerHandle; WriteCompThreeValue[2].Value = 1; WriteCompThreeValue[3] = new Opc.Da.ItemValue(); WriteCompThreeValue[3].ServerHandle = GroupWriteCompThree.Items[3].ServerHandle; WriteCompThreeValue[3].Value = 0; Opc.IRequest req; GroupWriteCompThree.Write(WriteCompThreeValue, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out req); }
public Opc.IdentifiedResult[] WriteTags(Opc.Da.ItemValue[] items) { PLCFactory factory = new PLCFactory(opcURL); Opc.Da.ItemValue[] responseIDTags = new Opc.Da.ItemValue[1]; var results = factory.WriteTags(items); return results; }
/// <summary> /// Write a value on the specified opc tag /// </summary> /// <typeparam name="T">The type of tag to write on</typeparam> /// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name. /// E.g: the tag `foo.bar` writes on the tag `bar` on the folder `foo`</param> /// <param name="item"></param> public void Write <T>(string tag, T item) { var itmVal = new OpcDa.ItemValue { ItemName = tag, Value = item }; var result = _server.Write(new[] { itmVal })[0]; CheckResult(result, tag); }
/// <summary> /// Writes the value of the specified item. /// </summary> public Opc.IdentifiedResult Write(string locale, Opc.Da.ItemValue value) { // write value. IdentifiedResult result = m_cache.Write(m_itemID, locale, value); if (result == null) { return(new IdentifiedResult(m_itemID, ResultID.E_FAIL)); } return(result); }
// Write The Component Value From Barcode Scanner private void ScanComponents_OPC() { Opc.Da.Item[] OPC_ScanComponent = new Opc.Da.Item[1]; OPC_ScanComponent[0] = new Opc.Da.Item(); OPC_ScanComponent[0].ItemName = Spotweld_Tag_Name + "DM8050_READ_RESULTS.DATA"; OPC_ScanComponent = ScanNewPart_Write.AddItems(OPC_ScanComponent); Opc.Da.ItemValue[] OPC_ScanComponent_Value = new Opc.Da.ItemValue[1]; OPC_ScanComponent_Value[0] = new Opc.Da.ItemValue(); OPC_ScanComponent_Value[0].ServerHandle = ScanNewPart_Write.Items[0].ServerHandle; OPC_ScanComponent_Value[0].Value = ScannerString; Opc.IRequest OPCRequest; ScanNewPart_Write.Write(OPC_ScanComponent_Value, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); }
private void SendItemValueRequest(Opc.Da.ItemValue item) { // Генерация URL в котором производится сохранение параметра string url = "http://" + config.amicumIp; if (config.amicumPort != "") { url += ":" + config.amicumPort; } url += "/opc/handle-opc-data"; Console.WriteLine(url); // Отправка данных на контроллер OPC WebRequest request = WebRequest.Create(url); request.Method = "POST"; // Данные для отправки string data = "itemName=" + item.ItemName; data += "&itemValue=" + item.Value.ToString(); data += "&dateTime=" + item.Timestamp.ToString("yyyy-MM-dd HH:mm:ss") + "." + item.Timestamp.Millisecond; //data += "&dateTime=" + (item.Timestamp.Date + item.Timestamp.TimeOfDay) + "." + item.Timestamp.Millisecond; data += "&connectString=" + serverHandle.Name; Console.WriteLine(data); // Преобразуем данные в массив байтов byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data); // Устанавливаем тип содержимого - параметр ContentType request.ContentType = "application/x-www-form-urlencoded"; // Устанавливаем заголовок Content-Length запроса - свойство ContentLength request.ContentLength = byteArray.Length; // Записываем данные в поток запроса using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } // Получение ответа от сервера WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine(reader.ReadToEnd()); } } response.Close(); }
// Turn on Reset Input private void ConfirmWeldReset_On_OPC(object sender, EventArgs e) { Opc.Da.Item[] OPC_Reset_On = new Opc.Da.Item[1]; OPC_Reset_On[0] = new Opc.Da.Item(); OPC_Reset_On[0].ItemName = SpotWeld_Tag_Name + "HMI_PB_Reset_Part_Weld_Count"; OPC_Reset_On = ResetWeldCount_Write.AddItems(OPC_Reset_On); Opc.Da.ItemValue[] OPC_ResetValue_On = new Opc.Da.ItemValue[1]; OPC_ResetValue_On[0] = new Opc.Da.ItemValue(); OPC_ResetValue_On[0].ServerHandle = ResetWeldCount_Write.Items[0].ServerHandle; OPC_ResetValue_On[0].Value = 1; Opc.IRequest OPCRequest; ResetWeldCount_Write.Write(OPC_ResetValue_On, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); //ResetOff_Timer.Start(); // Start a Timer to turn off Input }
// Turn off Reset Input private void ConfirmWeldReset_Off_OPC() { Opc.Da.Item[] OPC_Reset_Off = new Opc.Da.Item[1]; OPC_Reset_Off[0] = new Opc.Da.Item(); OPC_Reset_Off[0].ItemName = SpotWeld_Tag_Name + "HMI_PB_Reset_Part_Weld_Count"; OPC_Reset_Off = ResetWeldCount_Off_Write.AddItems(OPC_Reset_Off); Opc.Da.ItemValue[] OPC_ResetValue_Off = new Opc.Da.ItemValue[1]; OPC_ResetValue_Off[0] = new Opc.Da.ItemValue(); OPC_ResetValue_Off[0].ServerHandle = ResetWeldCount_Write.Items[0].ServerHandle; OPC_ResetValue_Off[0].Value = 0; Opc.IRequest OPCRequest; ResetWeldCount_Off_Write.Write(OPC_ResetValue_Off, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); }
/********************************************************************************************************************* * * Buttons Region End * *********************************************************************************************************************/ #endregion /******************************************************************************************************************** * * OPC Region Start * -- Total: * ********************************************************************************************************************/ #region // Write Both On and Off Using OPC_TeachSensorValue private void TeachSensor_OPC() { Opc.Da.Item[] OPC_SensorWrite = new Opc.Da.Item[1]; OPC_SensorWrite[0] = new Opc.Da.Item(); OPC_SensorWrite[0].ItemName = SpotWeld_Tag_Name + "HMI_PB_TEACH_SENSOR"; OPC_WriteSensor.Add(OPC_SensorWrite[0]); TeachSensor_Write.AddItems(OPC_WriteSensor.ToArray()); Opc.Da.ItemValue[] WriteValue = new Opc.Da.ItemValue[1]; WriteValue[0] = new Opc.Da.ItemValue(); WriteValue[0].ServerHandle = TeachSensor_Write.Items[0].ServerHandle; WriteValue[0].Value = OPC_TeachSensorValue; Opc.IRequest req; TeachSensor_Write.Write(WriteValue, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out req); }
private bool WriteString(string tag, string value) { //Create the item to write (if the group doesn't have it, we need to insert it) Item[] itemToAdd = new Item[1]; itemToAdd[0] = new Item(); itemToAdd[0].ItemName = tag; //create the item that contains the value to write ItemValue[] writeValues = new Opc.Da.ItemValue[1]; writeValues[0] = new ItemValue(itemToAdd[0]); //make a scan of group to see if it already contains the item bool itemFound = false; foreach (Item item in _view.groupWrite.Items) { if (item.ItemName == itemToAdd[0].ItemName) { // if it find the item i set the new value writeValues[0].ServerHandle = item.ServerHandle; itemFound = true; } } if (!itemFound) { //if it doesn't find it, we add it _view.groupWrite.AddItems(itemToAdd); writeValues[0].ServerHandle = _view.groupWrite.Items[_view.groupWrite.Items.Length - 1].ServerHandle; } //set the value to write writeValues[0].Value = value; //write try { _view.groupWrite.Write(writeValues); return(true); } catch { return(false); } }
/// <summary> /// Writes the value of the specified item property. /// </summary> public Opc.IdentifiedResult Write( string itemID, PropertyID propertyID, Opc.Da.ItemValue value) { lock (this) { // lookup item. DeviceItem item = (DeviceItem)m_items[itemID]; if (item == null) { return(new IdentifiedResult(itemID, ResultID.Da.E_UNKNOWN_ITEM_NAME)); } // write value. return(item.Write(propertyID, value)); } }
// Turn off Scan New Part Input // Toggle Scan Hardware Input private void SetHardware_OPC() { Opc.Da.Item[] OPC_SetHardware = new Opc.Da.Item[2]; OPC_SetHardware[0] = new Opc.Da.Item(); OPC_SetHardware[0].ItemName = Spotweld_Tag_Name + "HMI_PB_SCAN_NEW_PART"; OPC_SetHardware[1] = new Opc.Da.Item(); OPC_SetHardware[1].ItemName = Spotweld_Tag_Name + "HMI_PB_SCAN_HARDWARE"; OPC_SetHardware = Hardware_Write.AddItems(OPC_SetHardware); Opc.Da.ItemValue[] OPC_SetHardware_Value = new Opc.Da.ItemValue[2]; OPC_SetHardware_Value[0] = new Opc.Da.ItemValue(); OPC_SetHardware_Value[0].ServerHandle = Hardware_Write.Items[0].ServerHandle; OPC_SetHardware_Value[0].Value = 0; OPC_SetHardware_Value[1] = new Opc.Da.ItemValue(); OPC_SetHardware_Value[1].ServerHandle = Hardware_Write.Items[1].ServerHandle; OPC_SetHardware_Value[1].Value = HMI_PB_SCAN_HARDWARE_VALUE; Opc.IRequest OPCRequest; Hardware_Write.Write(OPC_SetHardware_Value, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); }
/// <summary> /// 向指定单个变量写入值 /// </summary> /// <param name="index"></param> /// <param name="itemName"></param> /// <param name="value"></param> public void writeOpc(int index, string itemName, string value) { Opc.Da.ItemValue[] opcItemValues = new Opc.Da.ItemValue[1]; foreach (Opc.Da.Item opcItem in opcServer.Subscriptions[index].Items) { if (opcItem.ItemName.Equals(itemName)) { opcItemValues[0] = new Opc.Da.ItemValue(opcItem); break; } } if (opcItemValues == null) { return; } opcItemValues[0].Value = value; opcServer.Write(opcItemValues); }
private bool isAbortRequested = false; //требование остановить поток /// <summary> /// /// </summary> /// <param name="serverName">Полное имя сервера</param> /// <param name="period">периодичность обмена</param> public OPCThread(string serverName, int period) { thread = new Thread(new ThreadStart(ThreadJob)); fullServerName = serverName; this.period = period; List <Item> itm = new List <Item>(); List <DOStruct> listDO = new List <DOStruct>(); //получение списка сигналов DO которые будут читаться по OPC foreach (DOStruct d in DOStruct.items) { if (d.OPCtag != "" && d.En) { listDO.Add(d); itm.Add(new Item(new ItemIdentifier(Properties.Settings.Default.OPCDevice + '!' + d.OPCtag))); } } arrayDO = listDO.ToArray(); opcDOItemsForRead = itm.ToArray(); itm = new List <Item>(); //формирование списка сигналов AO для чтения по OPC List <AOStruct> listAO = new List <AOStruct>(); foreach (AOStruct item in AOStruct.items) { if (item.OPCtag != "" && item.En) { listAO.Add(item); itm.Add(new Item(new ItemIdentifier(Properties.Settings.Default.OPCDevice + '!' + item.OPCtag))); } } arrayAO = listAO.ToArray(); opcAOItemsForRead = itm.ToArray(); Debug.WriteLine("DO items: " + opcDOItemsForRead.Length.ToString()); Debug.WriteLine("AO items: " + opcAOItemsForRead.Length.ToString()); LogWriter.AppendLog("DO OPC items: " + opcDOItemsForRead.Length.ToString()); LogWriter.AppendLog("AO OPC items: " + opcDOItemsForRead.Length.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="ItemValueArgs"/> class. /// </summary> /// <param name="value">The value of the process data to be be provided to all subscribers of the <see cref="I4UAServer.OnValueChanged"/>.</param> public ItemValueArgs(Opc.Da.ItemValueResult value) { Value = value; }
// Write the Selected Operation Value to the PLC private void OperationWriteOPC() { Opc.Da.Item[] OPC_ItemID = new Opc.Da.Item[8]; OPC_ItemID[0] = new Opc.Da.Item(); OPC_ItemID[0].ItemName = SpotWeld_Tag_Name + "HMI_Operation_One_PB"; OPC_ItemID[1] = new Opc.Da.Item(); OPC_ItemID[1].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Two_PB"; OPC_ItemID[2] = new Opc.Da.Item(); OPC_ItemID[2].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Three_PB"; OPC_ItemID[3] = new Opc.Da.Item(); OPC_ItemID[3].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Four_PB"; OPC_ItemID[4] = new Opc.Da.Item(); OPC_ItemID[4].ItemName = SpotWeld_Tag_Name + "HMI_Operation_One_Selected"; OPC_ItemID[5] = new Opc.Da.Item(); OPC_ItemID[5].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Two_Selected"; OPC_ItemID[6] = new Opc.Da.Item(); OPC_ItemID[6].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Three_Selected"; OPC_ItemID[7] = new Opc.Da.Item(); OPC_ItemID[7].ItemName = SpotWeld_Tag_Name + "HMI_Operation_Four_Selected"; OPC_ItemID = OperationSelection_Write.AddItems(OPC_ItemID); Opc.Da.ItemValue[] ItemID_OPCValue = new Opc.Da.ItemValue[8]; ItemID_OPCValue[0] = new Opc.Da.ItemValue(); ItemID_OPCValue[1] = new Opc.Da.ItemValue(); ItemID_OPCValue[2] = new Opc.Da.ItemValue(); ItemID_OPCValue[3] = new Opc.Da.ItemValue(); ItemID_OPCValue[4] = new Opc.Da.ItemValue(); ItemID_OPCValue[5] = new Opc.Da.ItemValue(); ItemID_OPCValue[6] = new Opc.Da.ItemValue(); ItemID_OPCValue[7] = new Opc.Da.ItemValue(); // switch the values written based on which operation was selected switch (SelectedOperation) { case 1: // Operation #1 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 1; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 0; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 0; ItemID_OPCValue[3].ServerHandle = OperationSelection_Write.Items[3].ServerHandle; ItemID_OPCValue[3].Value = 0; ItemID_OPCValue[4].ServerHandle = OperationSelection_Write.Items[4].ServerHandle; ItemID_OPCValue[4].Value = 1; ItemID_OPCValue[5].ServerHandle = OperationSelection_Write.Items[5].ServerHandle; ItemID_OPCValue[5].Value = 0; ItemID_OPCValue[6].ServerHandle = OperationSelection_Write.Items[6].ServerHandle; ItemID_OPCValue[6].Value = 0; ItemID_OPCValue[7].ServerHandle = OperationSelection_Write.Items[7].ServerHandle; ItemID_OPCValue[7].Value = 0; break; case 2: // Operation #2 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 0; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 1; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 0; ItemID_OPCValue[3].ServerHandle = OperationSelection_Write.Items[3].ServerHandle; ItemID_OPCValue[3].Value = 0; ItemID_OPCValue[4].ServerHandle = OperationSelection_Write.Items[4].ServerHandle; ItemID_OPCValue[4].Value = 0; ItemID_OPCValue[5].ServerHandle = OperationSelection_Write.Items[5].ServerHandle; ItemID_OPCValue[5].Value = 1; ItemID_OPCValue[6].ServerHandle = OperationSelection_Write.Items[6].ServerHandle; ItemID_OPCValue[6].Value = 0; ItemID_OPCValue[7].ServerHandle = OperationSelection_Write.Items[7].ServerHandle; ItemID_OPCValue[7].Value = 0; break; case 3: // Operation #3 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 0; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 0; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 1; ItemID_OPCValue[3].ServerHandle = OperationSelection_Write.Items[3].ServerHandle; ItemID_OPCValue[3].Value = 0; ItemID_OPCValue[4].ServerHandle = OperationSelection_Write.Items[4].ServerHandle; ItemID_OPCValue[4].Value = 0; ItemID_OPCValue[5].ServerHandle = OperationSelection_Write.Items[5].ServerHandle; ItemID_OPCValue[5].Value = 0; ItemID_OPCValue[6].ServerHandle = OperationSelection_Write.Items[6].ServerHandle; ItemID_OPCValue[6].Value = 1; ItemID_OPCValue[7].ServerHandle = OperationSelection_Write.Items[7].ServerHandle; ItemID_OPCValue[7].Value = 0; break; case 4: // Operation #4 ItemID_OPCValue[0].ServerHandle = OperationSelection_Write.Items[0].ServerHandle; ItemID_OPCValue[0].Value = 0; ItemID_OPCValue[1].ServerHandle = OperationSelection_Write.Items[1].ServerHandle; ItemID_OPCValue[1].Value = 0; ItemID_OPCValue[2].ServerHandle = OperationSelection_Write.Items[2].ServerHandle; ItemID_OPCValue[2].Value = 0; ItemID_OPCValue[3].ServerHandle = OperationSelection_Write.Items[3].ServerHandle; ItemID_OPCValue[3].Value = 1; ItemID_OPCValue[4].ServerHandle = OperationSelection_Write.Items[4].ServerHandle; ItemID_OPCValue[4].Value = 0; ItemID_OPCValue[5].ServerHandle = OperationSelection_Write.Items[5].ServerHandle; ItemID_OPCValue[5].Value = 0; ItemID_OPCValue[6].ServerHandle = OperationSelection_Write.Items[6].ServerHandle; ItemID_OPCValue[6].Value = 0; ItemID_OPCValue[7].ServerHandle = OperationSelection_Write.Items[7].ServerHandle; ItemID_OPCValue[7].Value = 1; break; } Opc.IRequest OPCRequest; OperationSelection_Write.Write(ItemID_OPCValue, 123, new Opc.Da.WriteCompleteEventHandler(WriteCompleteCallback), out OPCRequest); }
/// <summary> /// Writes the value of the specified item property. /// </summary> public Opc.IdentifiedResult Write( PropertyID propertyID, Opc.Da.ItemValue value) { // initialize result and validate property. IdentifiedResult result = new IdentifiedResult(); result.ItemName = m_itemID; result.ItemPath = null; result.ResultID = ValidatePropertyID(propertyID, accessRights.writable); result.DiagnosticInfo = null; // handle value writes. if (propertyID == Property.VALUE) { // copy value. m_value = Opc.Convert.Clone(value.Value); // update quality if specified. if (value.QualitySpecified) { m_quality = value.Quality; } // update timestamp if specified. if (value.TimestampSpecified) { m_timestamp = value.Timestamp; } // return results. return(result); } // lookup property description. PropertyDescription property = PropertyDescription.Find(propertyID); if (property == null) { result.ResultID = ResultID.Da.E_INVALID_PID; return(result); } // check datatype. if (!property.Type.IsInstanceOfType(value.Value)) { result.ResultID = ResultID.Da.E_BADTYPE; return(result); } // write non-value switch (propertyID.Code) { // standard properties. case DATATYPE: { m_datatype = (System.Type)value.Value; return(result); } case QUALITY: { m_quality = (Quality)value.Value; return(result); } case TIMESTAMP: { m_timestamp = (DateTime)value.Value; return(result); } case ACCESSRIGHTS: { m_accessRights = (accessRights)value.Value; return(result); } case SCANRATE: { m_scanRate = (float)value.Value; return(result); } case EUTYPE: { m_euType = (euType)value.Value; return(result); } case EUINFO: { m_euInfo = (string[])Opc.Convert.Clone(value.Value); return(result); } case HIGHEU: { m_maxValue = (double)value.Value; return(result); } case LOWEU: { m_minValue = (double)value.Value; return(result); } // other defined properties. default: { if (!m_properties.Contains(propertyID)) { result.ResultID = ResultID.Da.E_INVALID_PID; return(result); } m_properties[propertyID] = Opc.Convert.Clone(value.Value); break; } } // write complete. return(result); }
// Token: 0x0600067C RID: 1660 RVA: 0x00010E9E File Offset: 0x0000FE9E public ItemValueResult(ItemValue item) : base(item) { }
public static void RsLinx_OPC_Client_Write(string ItemName, int Value) { try { //Creo un istanza di OPC.server Opc.Da.Server server; //Parametro necessario alla connect OpcCom.Factory fact = new OpcCom.Factory(); //Creo un istanza di Sottoscrizione Opc.Da.Subscription groupWrite; //Creo un istanza di SubscriptionState, utile per controllare lo stato della sottoscrizione Opc.Da.SubscriptionState groupStateWrite; //Creo un array di OPC.Da.Item Opc.Da.Item[] items = new Opc.Da.Item[1]; //Setto factory e url del server, come url utilizzo quello del RSLinx OPC Server server = new Opc.Da.Server(fact, null); server.Url = new Opc.URL(Url); //Connetto il server server.Connect(); //Istanzio la sottoscrizione groupStateWrite = new Opc.Da.SubscriptionState(); groupStateWrite.Name = "Group Write"; //Questo valore deve essere true se voglio aver la possibilità di leggere, se devo solo scrivere lo metto false groupStateWrite.Active = false; //Creo il gruppo sul server groupWrite = (Opc.Da.Subscription)server.CreateSubscription(groupStateWrite); //Creo l'Item da scrivere (se il gruppo non lo possiede, lo devo inserire) Opc.Da.Item[] itemToAdd = new Opc.Da.Item[1]; itemToAdd[0] = new Opc.Da.Item(); itemToAdd[0].ItemName = ItemName; //Creo l'istanza di ItemValue che possiede il mio Item e il valore che voglio assegnargli Opc.Da.ItemValue[] writeValues = new Opc.Da.ItemValue[1]; writeValues[0] = new Opc.Da.ItemValue(itemToAdd[0]); //Controllo se l'oggetto esiste nel gruppo bool itemFound = false; foreach (Opc.Da.Item item in groupWrite.Items) { if (item.ItemName == itemToAdd[0].ItemName) { //Se lo trovo gli setto il nuovo valore writeValues[0].ServerHandle = item.ServerHandle; itemFound = true; } } if (!itemFound) { //Se non ho trovato l'oggetto nel gruppo lo aggiungo.. groupWrite.AddItems(itemToAdd); writeValues[0].ServerHandle = groupWrite.Items[groupWrite.Items.Length - 1].ServerHandle; } //...gli setto il valore writeValues[0].Value = Value; //e lo scrivo groupWrite.Write(writeValues); } catch (Exception ex) { //Se viene lanciata un'eccezione la mostro MessageBox.Show(ex.Message); } }
void ThreadJob() { try { srv = new Opc.Da.Server(new OpcCom.Factory(), new Opc.URL(fullServerName)); srv.Connect(); if (!srv.IsConnected) { throw new Exception("не удалось подключиться к серверу OPC"); } } catch { if (refMainWindow != null) { refMainWindow.Dispatcher.Invoke(refMainWindow.ofsFail); } return; } bool isFirstCycle = true; while (true) { //-------------- OPC ---------------- opcitems.Clear(); foreach (DIStruct di in DIStruct.items) { if ((di.OPCtag != "" && di.IsChanged && di.En) || (isFirstCycle)) { itm = new Opc.Da.ItemValue(Properties.Settings.Default.OPCDevice + '!' + di.OPCtag) { Value = di.ValDI ^ di.InvertDI }; opcitems.Add(itm); di.IsChanged = false; } } foreach (AIStruct ai in AIStruct.items) { if ((ai.OPCtag != "" && ai.IsChanged && ai.En) || (isFirstCycle)) { itm = new Opc.Da.ItemValue(Properties.Settings.Default.OPCDevice + '!' + ai.OPCtag); if (ai.PLCDestType == EPLCDestType.Float) { itm.Value = ai.fValAI; } else { itm.Value = ai.ValACD; } opcitems.Add(itm); ai.IsChanged = false; } } try { // Записываем сигналы DI AI srv.Write(opcitems.ToArray()); //читаем сигналы DO для которых задан тег if (opcDOItemsForRead.Length > 0) { readResult = srv.Read(opcDOItemsForRead); for (int i = 0; i < readResult.Length; i++) { try { arrayDO[i].ValDO = (bool)readResult[i].Value; } catch { } } } //читаем сигналы AO для которых задан тег if (opcAOItemsForRead.Length > 0) { readResult = srv.Read(opcAOItemsForRead); for (int i = 0; i < readResult.Length; i++) { try { if (arrayAO[i].PLCDestType == EPLCDestType.ADC) { object val = readResult[i].Value; if (val is short) { arrayAO[i].ValACD = (ushort)((Int16)readResult[i].Value); } if (val is ushort) { arrayAO[i].ValACD = (UInt16)readResult[i].Value; } } else { arrayAO[i].fVal = (float)readResult[i].Value; } } catch { } } Debug.WriteLine(opcAOItemsForRead.Length.ToString() + " AO tags read"); } } catch { //System.Windows.Forms.MessageBox.Show("OPC Thread exception:\n\r" + ex.Message); // LogWriter.AppendLog("Чтение по OPC прервано"+Environment.NewLine); if (refMainWindow != null) { refMainWindow.Dispatcher.Invoke(refMainWindow.ofsFail); } } isFirstCycle = false; Thread.Sleep(period); if (isAbortRequested) { return; } }//loop }
public void Work() { int i = 5; if (i == 1) { url = new Opc.URL("opcda://192.168.0.4/OPC.PHDServerDA.1"); server = new Opc.Da.Server(fact, null); server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); Opc.Da.Item[] itemCollection = new Opc.Da.Item[1]; itemCollection[0] = new Opc.Da.Item { ItemName = "RTOS.TEST.PV", MaxAge = -1 }; Opc.Da.ItemValueResult[] result = server.Read(itemCollection); Console.WriteLine(result[0].Value); server.Disconnect(); } else if (i == 2) { url = new Opc.URL("opcda://192.168.0.4/OPC.PHDServerDA.1"); server = new Opc.Da.Server(fact, null); server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); groupState = new Opc.Da.SubscriptionState(); groupState.Name = "Group"; groupState.ServerHandle = null; groupState.ClientHandle = Guid.NewGuid().ToString(); groupState.Active = true; groupState.UpdateRate = 1000; groupState.Deadband = 0; groupState.Locale = null; groupRead = (Opc.Da.Subscription)server.CreateSubscription(groupState); string[] itemName = { "RTOS.TEST.PV", "RTOR.TI1237.DACA.PV" }; Opc.Da.Item[] items = new Opc.Da.Item[2]; for (int j = 0; j < items.Length; j++) { items[j] = new Opc.Da.Item(); items[j].ClientHandle = Guid.NewGuid().ToString(); items[j].ItemPath = null; items[j].ItemName = itemName[j]; } groupRead.AddItems(items); //groupRead.DataChanged += new Opc.Da.DataChangedEventHandler(group_DataChanged); Opc.Da.ItemValueResult[] results = groupRead.Read(groupRead.Items); foreach (ItemValueResult result in results) { Console.WriteLine("{0},{1},{2},{3}", result.ItemName, result.Value, result.Quality, result.Timestamp); } //groupRead.DataChanged -= new Opc.Da.DataChangedEventHandler(group_DataChanged); groupRead.RemoveItems(groupRead.Items); server.CancelSubscription(groupRead); groupRead.Dispose(); server.Disconnect(); } else if (i == 3) { url = new Opc.URL("opcda://192.168.0.4/OPC.PHDServerDA.1"); server = new Opc.Da.Server(fact, null); server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); groupState = new Opc.Da.SubscriptionState(); groupState.Name = "Group"; groupState.ServerHandle = null; groupState.ClientHandle = Guid.NewGuid().ToString(); groupState.Active = true; groupState.UpdateRate = 1000; groupState.Deadband = 0; groupState.Locale = null; groupWrite = (Opc.Da.Subscription)server.CreateSubscription(groupState); string[] itemName = { "RTOS.TEST.PV", "RTOS.TEST2.PV" }; Opc.Da.Item[] items = new Opc.Da.Item[2]; for (int j = 0; j < items.Length; j++) { items[j] = new Opc.Da.Item(); items[j].ClientHandle = Guid.NewGuid().ToString(); items[j].ItemPath = null; items[j].ItemName = itemName[j]; } groupWrite.AddItems(items); //groupRead.DataChanged += new Opc.Da.DataChangedEventHandler(group_DataChanged); Opc.Da.ItemValue[] writeValues = new Opc.Da.ItemValue[groupWrite.Items.Length]; for (int k = 0; k < groupWrite.Items.Length; k++) { writeValues[k] = new Opc.Da.ItemValue((ItemIdentifier)groupWrite.Items[k]); } writeValues[0].Value = 5; writeValues[1].Value = 6; groupWrite.Write(writeValues); Console.WriteLine("Press any key to close..."); Console.ReadLine(); //groupRead.DataChanged -= new Opc.Da.DataChangedEventHandler(group_DataChanged); groupWrite.RemoveItems(groupWrite.Items); server.CancelSubscription(groupWrite); groupWrite.Dispose(); server.Disconnect(); } else if (i == 4) { url = new Opc.URL("opchda://192.168.0.4/OPC.PHDServerHDA.1"); serverHda = new Opc.Hda.Server(fact, null); serverHda.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); try { } catch (Opc.ConnectFailedException opcConnEx) { Console.WriteLine(string.Format("Could not connect to server {0}", "OPC.PHDServerHDA.1")); Console.WriteLine(opcConnEx.ToString()); } Console.WriteLine("Are we connected? " + serverHda.IsConnected); string[] itemName = { "RTOS.TEST.PV", "RTOS.TEST2.PV" }; Opc.Hda.Trend groupHda = new Trend(serverHda); groupHda.Name = "HDA"; groupHda.AggregateID = AggregateID.NOAGGREGATE; DateTime startTime = DateTime.Now.AddHours(-1); DateTime endTime = DateTime.Now; groupHda.StartTime = new Opc.Hda.Time(startTime); groupHda.EndTime = new Opc.Hda.Time(endTime); groupHda.MaxValues = 0; groupHda.IncludeBounds = false; serverHda.Trends.Add(groupHda); Opc.Hda.Item[] items = new Opc.Hda.Item[2]; for (int m = 0; m < 2; m++) { items[m] = new Opc.Hda.Item(); items[m].ItemName = itemName[m]; items[m].ItemPath = null; items[m].ClientHandle = Guid.NewGuid().ToString(); } IdentifiedResult[] identifiedResult = serverHda.CreateItems(items); if (identifiedResult != null) { foreach (IdentifiedResult item in identifiedResult) { if (item.ResultID.Succeeded()) { groupHda.Items.Add(new Opc.Hda.Item(item)); } } } Opc.Hda.ItemValueCollection[] results = groupHda.ReadRaw(); Opc.Hda.ItemValueCollection result1 = results[0]; foreach (Opc.Hda.ItemValue result in result1) { Console.WriteLine("{0},{1},{2}", result.Value, result.Quality, result.Timestamp); } serverHda.Disconnect(); } else if (i == 5) { url = new Opc.URL("opchda://192.168.0.4/OPC.PHDServerHDA.1"); serverHda = new Opc.Hda.Server(fact, null); serverHda.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); try { } catch (Opc.ConnectFailedException opcConnEx) { Console.WriteLine(string.Format("Could not connect to server {0}", "OPC.PHDServerHDA.1")); Console.WriteLine(opcConnEx.ToString()); } Console.WriteLine("Are we connected? " + serverHda.IsConnected); string[] itemName = { "RTOR.TI1237.DACA.PV", "RTOS.TEST2.PV" }; Opc.Hda.Trend groupHda = new Trend(serverHda); groupHda.Name = "HDA"; groupHda.AggregateID = AggregateID.AVERAGE; DateTime startTime = DateTime.Now.AddHours(-1); DateTime endTime = DateTime.Now; //TimeSpan span = endTime.Subtract(startTime); //int calcInterval = ((int)span.TotalSeconds); //groupHda.ResampleInterval = (decimal)calcInterval; //DateTime startTime = new DateTime(2020, 6, 12, 9, 0, 0, 0); //DateTime endTime = new DateTime(2020, 6, 12, 10, 0, 0, 0); int calcInterval = 300; groupHda.ResampleInterval = (decimal)calcInterval; groupHda.StartTime = new Opc.Hda.Time(startTime); groupHda.EndTime = new Opc.Hda.Time(endTime); groupHda.MaxValues = 0; groupHda.IncludeBounds = false; serverHda.Trends.Add(groupHda); Opc.Hda.Item[] items = new Opc.Hda.Item[2]; for (int m = 0; m < 2; m++) { items[m] = new Opc.Hda.Item(); items[m].ItemName = itemName[m]; items[m].ItemPath = null; items[m].ClientHandle = Guid.NewGuid().ToString(); } IdentifiedResult[] identifiedResult = serverHda.CreateItems(items); if (identifiedResult != null) { foreach (IdentifiedResult item in identifiedResult) { if (item.ResultID.Succeeded()) { groupHda.Items.Add(new Opc.Hda.Item(item)); } } } Opc.Hda.ItemValueCollection[] results = groupHda.ReadProcessed(); Opc.Hda.ItemValueCollection result1 = results[0]; foreach (Opc.Hda.ItemValue result in result1) { Console.WriteLine("{0},{1},{2}", result.Value, result.Quality, result.Timestamp); } serverHda.Disconnect(); } }
internal IdentifiedResult Write(PropertyID propertyID, Opc.Da.ItemValue value) { return(Write(propertyID, value, true)); }