/// <summary> /// 批量读取寄存器 /// </summary> bool ReadBlockReg(string reg, int num, string namefun, out int[] value) { g_MtReadBlock.WaitOne(); value = null; try { bool blResult = false; if (ParSetPLC.P_I.BlAnnotherPLC) { ComDataPLC c_I = new ComDataPLC(FunPLC_enum.ReadBlockReg, reg, num, namefun, value); blResult = ReadData(c_I, out value); } else { blResult = PortPLC_I.g_BasePortPLC.ReadBlockReg(reg.Replace("\\n", "\n"), num, out value); } return(blResult); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } finally { g_MtReadBlock.ReleaseMutex(); } }
/// <summary> /// 读取单个寄存器 /// </summary> bool ReadReg(string reg, out int value) { value = 0; g_MtRead.WaitOne(); try { int intValue = 0; bool blResult = false; if (ParSetPLC.P_I.BlAnnotherPLC) { ComDataPLC c_I = new ComDataPLC(FunPLC_enum.ReadReg, reg, 1, "", value); blResult = ReadData(c_I, out value); } else { blResult = PortPLC_I.g_BasePortPLC.ReadReg(reg, out intValue); } return(blResult); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } finally { g_MtRead.ReleaseMutex(); } }
/// <summary> /// 写批量寄存器 /// </summary> bool WriteBlockReg(string reg, int num, int[] value, string nameFun) { g_mtWriteBlock.WaitOne(); try { bool blResult = false; if (ParSetPLC.P_I.BlAnnotherPLC) { ComDataPLC c_I = new ComDataPLC(FunPLC_enum.WriteBlockReg, reg, num, nameFun, value); blResult = WriteData(c_I); } else { blResult = PortPLC_I.g_BasePortPLC.WriteBlockReg(reg.Replace("\\n", "\n"), num, value, nameFun); } if (!blResult) { CommunicationState_event("WriteBlockReg写入寄存器失败:" + reg, "red"); } return(blResult); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } finally { g_mtWriteBlock.ReleaseMutex(); } }
bool ReadData(ComDataPLC data, out int value) { value = 0; try { if (WriteData(data)) { //记录日志 if (ParSetPLC.P_I.BlAnnotherPLCLog) { LogPLC.L_I.WritePLC("PLCWrite_FineVision", data.NameFun, data.Reg.Replace("\n", ",").PadLeft(7, ' '), data.Value.ToString(), "0"); } string key = data.Reg + data.Time; bool blContains = false; if (g_HtPLCData.Contains(key)) { blContains = true; } int i = 0; while (!blContains) { Thread.Sleep(10); i++; if (i > 100) { return(false); } if (g_HtPLCData.Contains(key)) { blContains = true; value = (int)g_HtPLCData[key]; //记录日志 if (ParSetPLC.P_I.BlAnnotherPLCLog) { LogPLC.L_I.WritePLC("PLCWrite_FineVision", data.NameFun, "", value.ToString(), "0"); } return(true); } else { } } return(false); } return(false); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } }
bool WriteData(ComDataPLC data) { try { bool blResult = WriteData(data.AllData); if (ParSetPLC.P_I.BlAnnotherPLCLog) { LogPLC.L_I.WritePLC("PLCWriteData_FineVision", data.NameFun, data.Reg.Replace("\n", ",").PadLeft(7, ' '), data.Value.ToString(), "0"); } return(blResult); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } }
/// <summary> /// 写单个寄存器 /// </summary> /// <param name="reg"></param> /// <param name="value"></param> /// <returns></returns> bool WriteReg_Write6(string reg, int value) { g_mtWrite.WaitOne(); try { //离线,不写入PLC if (RegeditPLC.R_I.BlOffLinePLC) { return(true); } bool blResult = false; if (ParSetPLC.P_I.BlAnnotherPLC) { ComDataPLC c_I = new ComDataPLC(FunPLC_enum.WriteBlockReg, reg, 1, "", value); blResult = WriteData(c_I); } else { blResult = PortPLC_I.g_BasePortPLC6.WriteReg(reg, value, "WritePLC6"); } if (!blResult) { CommunicationState_event("WriteReg6写入寄存器失败:" + reg, "red"); } return(blResult); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } finally { g_mtWrite.ReleaseMutex(); } }
bool ReadData(ComDataPLC data, out int[] value) { value = null; try { if (WriteData(data)) { //记录日志 if (ParSetPLC.P_I.BlAnnotherPLCLog) { LogPLC.L_I.WritePLC("PLCReadData_FineVision", data.NameFun, data.Reg.Replace("\n", ",").PadLeft(7, ' '), data.Value.ToString(), "0"); } string key = data.Time; if (g_HtPLCData.Contains(key)) { value = (int[])g_HtPLCData[key]; g_HtPLCData.Remove(key); return(true); } int i = 0; while (!g_HtPLCData.Contains(key)) { Thread.Sleep(10); i++; if (i > 50) { return(false); } } if (g_HtPLCData.Contains(key)) { value = (int[])g_HtPLCData[key]; g_HtPLCData.Remove(key); string strValue = ""; for (int j = 0; j < value.Length; j++) { strValue += value[j].ToString(); } //记录日志 if (ParSetPLC.P_I.BlAnnotherPLCLog) { LogPLC.L_I.WritePLC("PLCReadData_FineVision", data.NameFun, "", strValue, "0"); } return(true); } else { Log.L_I.WriteError(NameClass, "接收到结果得HtResult有未读取得PLC值"); } return(true); } return(false); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } }