/// <summary> /// XY 地址 8进制转换成十进制 进行类建立 /// </summary> /// <param name="addr"></param> /// <returns></returns> public XJPlcInfo[] GetPlcInfo(int addr, int count, string XYM, string mode) { List <XJPlcInfo> plcinforlst = new List <XJPlcInfo>(); int addrreal = addr; if (XJPLCPackCmdAndDataUnpack.AreaGetFromStr(XYM.Trim()) > Constant.HM_ID) { addrreal = ConstantMethod.GetXYAddr8To10(addr); } for (int i = 0; i < count; i++) { XJPlcInfo tmpInfo = new XJPlcInfo(addrreal, XYM.Trim(), mode); tmpInfo.Xuhao = -1; plcinforlst.Add(tmpInfo); addrreal++; } return(plcinforlst.ToArray()); }
public void SetPlcInfo(XJPlcInfo pInfo0) { pInfo = pInfo0; }
/// <summary> /// 这里增加一个 就是 主form太大了 分割一个form出来 但是 绑定 还是在原来那个主form上 /// </summary> /// <param name="dataForm0"></param> void SplitPackCmdReadDMDataOut(DataTable dataForm0, DataTable dataForm1) { List <XJPlcInfo> plcInfoLst = new List <XJPlcInfo>(); List <XJPlcInfo> MPlcInfo = new List <XJPlcInfo>(); foreach (DataRow row in dataForm0.Rows) { int mAddr = 0; int count = 0; string strSplit1; string strSplit2; string DSmode; //单字还是双字 #region 获取地址 个数 区域 创建plcinfo类 并添加到集合 if (row == null) { return; } if (row["addr"].ToString() == null || row["mode"].ToString() == null || row["count"].ToString() == null) { return; } ; //添加各个单元 //取数字 用替代的字符的方法 取数组就替换字母为空 取字母就替换数字 strSplit1 = Regex.Replace(row["addr"].ToString(), "[A-Z]", "", RegexOptions.IgnoreCase); //取字母 strSplit2 = Regex.Replace(row["addr"].ToString(), "[0-9]", "", RegexOptions.IgnoreCase); DSmode = row["mode"].ToString(); if (!int.TryParse(row["count"].ToString(), out count)) { continue; } //地址超了 无效 暂且定XDM 最大69999 if (!int.TryParse(strSplit1, out mAddr) || (mAddr < 0) || (mAddr > Constant.XJMaxAddr)) { continue; } //字母大于4 无效地址 if (strSplit2.Count() > 3) { continue; } //这里数组进行统计 if (DSmode.Equals(Constant.DoubleMode) && (XJPLCPackCmdAndDataUnpack.AreaGetFromStr(strSplit2) < Constant.M_ID)) { count = count * 2; } //传入数据起始地址 个数 区域 模式 XJPlcInfo[] tmpInfoLst = XJPLCcmd.GetPlcInfo(mAddr, count, strSplit2, DSmode); if (tmpInfoLst.Count() > 0) { plcInfoLst.AddRange(tmpInfoLst); } #endregion } #region 排序 去重复 统计DM 起始点 //排序 按照绝对地址 plcInfoLst = plcInfoLst.OrderBy(x => x.AbsAddr).ToList(); //去重复 plcInfoLst = plcInfoLst.Distinct(new ModelComparer()).ToList(); //分离D 区 M区 DPlcInfo = plcInfoLst.FindAll(t => t.IntArea < (Constant.HSD_ID + 1)); MPlcInfo = plcInfoLst.FindAll(t => t.IntArea > (Constant.HSD_ID)); DPlcInfo = InsertPlcInfo(DPlcInfo); //将D区分解出来 出来 变成一个一个单个的地址 尽量保持连续 MPlcInfo = InsertPlcInfo(MPlcInfo); plcInfoLst = DPlcInfo.Union(MPlcInfo).ToList <XJPlcInfo>(); #endregion #region 根据断点 建立命令的表格缓冲lst 然后创建读取DM区域的命令 //开始打包 List <int> addrLst = new List <int>(); //连续地址的起始地址 List <int> idLst = new List <int>(); //地址是D xy HSD List <int> addrcount = new List <int>(); //起始地址开始 读取几个寄存器 List <int> breakPoint = new List <int>(); //在 //首先要确定断点 breakPoint.Add(0); //获取不连续点的位置 D区域 for (int i = 0; i < plcInfoLst.Count - 1; i++) { if (((plcInfoLst[i + 1].RelAddr - plcInfoLst[i].RelAddr) > 1) || (plcInfoLst[i + 1].IntArea != plcInfoLst[i].IntArea)) { int bp = i + 1; breakPoint.Add(bp); } } breakPoint.Add(plcInfoLst.Count); //d区在前面,M区在后面 根据断点来区分 //统计D区起始地址个数 统计M区起始地址个数 //D区返回数据可以根据Dplcinfo集合来预算 //但M区需要知道M起始地址个数 for (int j = 0; j < breakPoint.Count; j++) { if (breakPoint[j] < plcInfoLst.Count) { addrLst.Add(plcInfoLst[breakPoint[j]].AbsAddr); idLst.Add(plcInfoLst[breakPoint[j]].IntArea); addrcount.Add(plcInfoLst[breakPoint[j + 1] - 1].RelAddr - plcInfoLst[breakPoint[j]].RelAddr + 1); } } //这里d区的话 需要指定一下双字情况下的 另外一个字节 FindHighPlcInfo(DPlcInfo); //这里M区麻烦一点 分成n个M单元组 每个单元组 有个起始地址 MPlcInfoAll = new List <List <XJPlcInfo> >(); for (int i = 0; i < addrLst.Count; i++) { List <XJPlcInfo> mplst = new List <XJPlcInfo>(); if (idLst[i] > Constant.HSD_ID) { for (int k = 0; k < addrcount[i]; k++) { XJPlcInfo p = new XJPlcInfo(); p.ValueMode = Constant.BitMode; p.ByteValue = new byte[1]; p.IntArea = idLst[i]; p.AbsAddr = addrLst[i] + k; p.Xuhao = k; mplst.Add(p); } } if (mplst.Count > 0) { MPlcInfoAll.Add(mplst); } } int mCount = 0; for (int i = 0; i < MPlcInfoAll.Count; i++) { double cntdb = (double)MPlcInfoAll[i].Count / 8; mCount = mCount + (int)Math.Ceiling(cntdb); } //绑定主form FindIndexInPlcInfo(dataForm1, DPlcInfo, MPlcInfoAll); XJPLCcmd.PackCmdReadDMDataOut(addrLst, idLst, addrcount, 5 + DPlcInfo.Count * 2 + mCount); #endregion addrLst = null; idLst = null; addrcount = null; breakPoint = null; //GC.Collect(); //GC.WaitForPendingFinalizers(); }