/// <summary> /// 同步写数据,支持按照位写 /// </summary> /// <param name="dataAddress">地址位;R2000</param> /// <param name="dataValues"></param> /// <returns></returns> public OperateResult SyncWriteData(string dataAddress, string dataValues) { if (string.IsNullOrEmpty(dataAddress) || string.IsNullOrEmpty(dataValues)) { return(new OperateResult("传入的参数都不能为空")); } if (dataAddress.Length <= 1) { return(new OperateResult("Tag地址长度不正确")); } string dataValue = dataValues.Split('|')[0]; #region 特殊地址解析 string[] addresses = dataAddress.Split('|'); if ((addresses.Length > 1) && (!string.IsNullOrEmpty(addresses[1]))) { dataValue = string.Format(addresses[1].Replace("#13", "\n"), dataValues.Split('|')); dataAddress = addresses[0]; } #endregion string sInfo = $"写入 Fanuc CNC 设备IP({ServerIP}) 地址({dataAddress}) 值({dataValue}) "; try { string sValueType = dataAddress.Substring(0, 1); //类型 short valueType = 0; //参数类型 string sAddr = dataAddress.Substring(1); //内部地址 string startAddr = sAddr; int iGetPos = -1; //按位进行读时的位。 if (sAddr.Contains('.')) //按位写 { startAddr = sAddr.Substring(0, sAddr.IndexOf('.')); iGetPos = SafeConverter.SafeToInt(sAddr.Substring(sAddr.IndexOf('.') + 1)); } #region 获取类型 valueType = ValueTypeByStr(sValueType); if ((valueType == 0) && (dataAddress != "写入刀补")) //不支持的类型 { string errorType = $"Fanuc CNC 设备 IP({ServerIP}) 地址({dataAddress}) 值({dataValue}),写入失败,不支持的类型."; Console.WriteLine(errorType); return(new OperateResult(errorType)); } #endregion #region Focas 写值 Console.WriteLine($"---Fanuc CNC 设备 IP({ServerIP}) 地址({dataAddress}) 值({dataValue})"); string error = ""; ushort handel = GetFocasHandle(out error); //获取 连接Focas的Handel short ret; if (!string.IsNullOrEmpty(error)) { Console.WriteLine(sInfo + $"失败,连接错误代码({error})"); return(new OperateResult(handel, sInfo + "失败,连接错误代码(" + error + ")")); } ushort nAddr = SafeConverter.SafeToUshort(startAddr); //地址 short nRValue = SafeConverter.SafeToShort(dataValue); //写入值 if (dataAddress == "写入刀补") { #region 写入刀补 ret = DownToolOffset(handel, dataValue, out error); if (ret != Focas1.EW_OK) //失败,两次写入 { FreeFocasHandle(out error); //释放 handel = GetFocasHandle(out error); //重新连接Focas的Handel if (!string.IsNullOrEmpty(error)) { Console.WriteLine(sInfo + $"失败,连接错误代码({error})"); return(new OperateResult(ret, sInfo + "失败,连接错误代码(" + error + ")")); } ret = DownToolOffset(handel, dataValue, out error); } #endregion } else if (sValueType == "#") //写宏变量 { #region 写宏变量 ret = SetMacroData(handel, SafeConverter.SafeToShort(nAddr), SafeConverter.SafeToDouble(dataValue), out error); if (ret != Focas1.EW_OK) //失败,两次写入 { FreeFocasHandle(out error); //释放 handel = GetFocasHandle(out error); //重新连接Focas的Handel if (!string.IsNullOrEmpty(error)) { Console.WriteLine(sInfo + $"失败,连接错误代码({error})"); return(new OperateResult(ret, sInfo + "失败,连接错误代码(" + error + ")")); } ret = SetMacroData(handel, SafeConverter.SafeToShort(nAddr), SafeConverter.SafeToDouble(dataValue), out error); } #endregion } else if (valueType < 100) { #region 写PMC,先读后写 Focas1.IODBPMC0 pmcdata1 = new Focas1.IODBPMC0(); // for 1 Byte ret = Focas1.pmc_rdpmcrng(handel, valueType, 0, nAddr, nAddr, 9, pmcdata1); // D data of 1 Byte if (ret != Focas1.EW_OK) //失败,两次写入 { FreeFocasHandle(out error); //释放 handel = GetFocasHandle(out error); //重新连接Focas的Handel if (!string.IsNullOrEmpty(error)) { Console.WriteLine(sInfo + $"失败,连接错误代码({error})"); return(new OperateResult(ret, sInfo + "失败,连接错误代码(" + error + ")")); } ret = Focas1.pmc_rdpmcrng(handel, valueType, 0, nAddr, nAddr, 9, pmcdata1); // D data of 1 Byte if (ret != Focas1.EW_OK) { Console.WriteLine(sInfo + "失败(读取时),错误代码(" + ret + ")"); return(new OperateResult(ret, sInfo + "失败(读取时),错误代码(" + ret + ")")); } } if ((iGetPos >= 0) && (iGetPos <= 7)) //按位读取数据 { int oldValue = pmcdata1.cdata[0]; byte newValue = (byte)(1 << iGetPos); if (nRValue > 0) //置位 { pmcdata1.cdata[0] = (byte)(oldValue | newValue); //按位或 } else //复位 { pmcdata1.cdata[0] = (byte)(oldValue & (newValue ^ 255)); //按位与 } } else //正常读写 { pmcdata1.cdata[0] = Convert.ToByte(nRValue); } ret = Focas1.pmc_wrpmcrng(handel, nAddr, pmcdata1); #endregion } else { return(new OperateResult(handel, "写Fanuc CNC 数据失败!错误的类型!")); } if (ret != Focas1.EW_OK) { Console.WriteLine(sInfo + "失败(写入时),错误代码(" + ret + ")"); return(new OperateResult(ret, sInfo + "失败(写入时),错误代码(" + ret + ")")); } Console.WriteLine("-----" + sInfo + "成功---"); #endregion return(OperateResult.CreateSuccessResult()); //返回成功 } catch (Exception ex) { string error = sInfo + $"失败,错误为({ex.Message})"; Console.WriteLine(error); return(new OperateResult(error)); } }