public string ReadPlcDbValue(string plcName, ref string errText) { lock (plcName) { try { errText = string.Empty; PLClock plcRead = plcClass(plcName, ref errText); if (errText.Trim().Length > 0) { return("01aaa"); } List <object> read = plcRead.ReadPlc(); if (read == null) { return("01bbb"); } if (read.Count == 0) { return("01ccc"); } return(read[0].ToString()); } catch (Exception ex) { errText = ex.Message.ToString(); return("01ddd");// throw ex; } } }
/// <summary> /// 写plc的值 /// </summary> public string WritePlcDataDB(string plcName, object[] value) { string errText = string.Empty; PLClock plcRead = plcClass(plcName, ref errText); if (errText.Trim().Length > 0) { return(errText); } plcRead.WirtePlc(value); return(errText); }
public PLClock plcClass(string plcName, ref string errText) { lock (plcLock) { try { errText = string.Empty; PLClock plcRead; if (plcCollection.Keys.Count(r => r == plcName) > 0) { plcRead = plcCollection[plcName]; return(plcRead); } plcRead = new PLClock(); plcRead.ConnectRemoteServer(); plcRead.PLCGroupAdd(); foreach (KeyValuePair <string, List <string> > item in typeClass) { string xmlName = item.Key; if (xmlName == plcName) { OpcRcw.Da.OPCITEMDEF[] Item = new OPCITEMDEF[item.Value.Count]; for (int i = 0; i < item.Value.Count; i++) { Item[i].szAccessPath = ""; Item[i].szItemID = item.Value[i].ToString(); Item[i].bActive = 1; //是否激活 Item[i].hClient = i + 1; //表示ID Item[i].dwBlobSize = 0; Item[i].pBlob = IntPtr.Zero; Item[i].vtRequestedDataType = 8; plcRead.PLCItemAdd(Item); } break; } } plcCollection.Add(plcName, plcRead); return(plcRead); } catch (Exception ex) { errText = ex.Message.ToString(); return(null); } } }