//一行中获取地址和区域 /根据一行的数据 返回固定的 plc单元格参数 这个函数 是返回绝对地址的 private bool getAddrAndArea(DataRow row, ref int mAddr, ref int mArea, ref string DSmode) { DSmode = row["mode"].ToString().Trim(); string area = "";///地址区域 string valueStr = row["addr"].ToString().Trim(); int count = 0; ConstantMethod.getAddrAndAreaByStr(valueStr, ref mAddr, ref area, DeviceId); //地址超了 无效 暂且定XDM 最大69999 if ((mAddr < 0) || (mAddr > Constant.DTTCPMAXAddr)) { return(false); } if (!int.TryParse(row["count"].ToString().Trim(), out count)) { return(false); } if (DeviceId == Constant.xzjDeivceId) { mArea = XJPLCPackCmdAndDataUnpack.AreaGetFromStr(area); mAddr = XJPLCPackCmdAndDataUnpack.RelAbsGet(mAddr, mArea, DeviceId); } else { mArea = DTTcpCmdPackAndDataUnpack.GetIntAreaFromStr(area); mAddr = DTTcpCmdPackAndDataUnpack.GetAbsAddrFromStr(mAddr, area); } return(true); }
public void ReadBitTest() { //发送 // C7 97 00 00 00 19 //01 43 01 //00 12 以下是地址和 读取个数 //01 C0 20 00 00 01 //01 C0 20 05 00 01 //01 C0 20 2D 00 01 //73 86 //回复: //AE 00 00 00 // 00 0E // 01 43 01 // 00 09 以下是值 // 00 01 00 // 00 01 01 // 00 01 01 int[] addr = { 0, 5, 45 }; int[] id = { Constant.MXAddrId, Constant.MXAddrId, Constant.MXAddrId }; int[] count = { 9, 1, 1 }; List <byte> cmdInLst = new List <byte>(); //反馈可变 ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(DTTcpCmdPackAndDataUnpack.PackReadBitCmd(addr, id, count, cmdInLst))); ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(cmdInLst.ToArray())); }
//开始分割 public void splitLstGetLst(List <int> splitLst, List <DTTcpPlcInfo> pLst, List <List <byte> > ReadCmdOutLst, List <List <byte> > ReadCmdInLst, int id ) { //开始分割 for (int i = 0; i < splitLst.Count; i++) { DTTcpPlcInfo[] temp; List <byte> tempOut = new List <byte>(); List <byte> tempIn = new List <byte>(); int skipCount = ConstantMethod.getSum(splitLst, i); temp = pLst.Skip(skipCount).Take(splitLst[i]).ToArray(); if (id == Constant.D_ID) { DTTcpCmdPackAndDataUnpack.ASPLCPackReadDCmd(temp, tempOut, tempIn); } if (id == Constant.M_ID) { DTTcpCmdPackAndDataUnpack.ASPLCPackReadMCmd(temp, tempOut, tempIn); } if (tempOut.Count > 0) { ReadCmdOutLst.Add(tempOut); } if (tempIn.Count > 0) { ReadCmdInLst.Add(tempIn); } } }
public DTTcpPlcInfo(int addr, string areaIn, string Mode) { this.relativeaddr = addr; this.StrArea = areaIn; absAddr = DTTcpCmdPackAndDataUnpack.GetAbsAddrFromStr(relativeaddr, StrArea); intArea = DTTcpCmdPackAndDataUnpack.GetIntAreaFromStr(StrArea); ValueMode = Mode; }
public void ReadDataTest() { //根据手册来 int[] addr = { 0, 1, 2, 100 }; int[] id = { Constant.MBAddrId, Constant.MBAddrId, Constant.MBAddrId, Constant.MBAddrId }; int[] count = { 2, 2, 2, 4 }; List <byte> cmdInLst = new List <byte>(); //反馈可变 ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(DTTcpCmdPackAndDataUnpack.PackReadByteCmd(addr, id, count, cmdInLst))); ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(cmdInLst.ToArray())); }
public void WriteByteTest() { List <byte[]> value = new List <byte[]>(); byte[] value0 = { 100, 0 }; byte[] value1 = { 200, 0 }; byte[] value2 = { 0x2c, 0x01 }; value.Add(value0); value.Add(value1); value.Add(value2); int[] addr = { 0, 5, 9 }; int[] id = { Constant.MBAddrId, Constant.MBAddrId, Constant.MBAddrId }; int[] count = { 2, 2, 2 }; List <byte> cmdInLst = new List <byte>(); //反馈可变 ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(DTTcpCmdPackAndDataUnpack.PackWriteByteCmd(addr, id, count, value, cmdInLst))); ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(cmdInLst.ToArray())); }
public DTTcpPlcInfo(int addr, string areaIn, string Mode, int plcId) { this.relativeaddr = addr; this.StrArea = areaIn; //区分 dvp15mc 和 As PLC if (plcId == Constant.xzjDeivceId) { intArea = XJPLCPackCmdAndDataUnpack.AreaGetFromStr(areaIn); absAddr = XJPLCPackCmdAndDataUnpack.RelAbsGet(addr, intArea, 0); } else { absAddr = DTTcpCmdPackAndDataUnpack.GetAbsAddrFromStr(relativeaddr, StrArea); intArea = DTTcpCmdPackAndDataUnpack.GetIntAreaFromStr(StrArea); } ValueMode = Mode; }
public void WriteBitTest() { List <byte[]> value = new List <byte[]>(); byte[] value0 = { 0x00, 0x00 }; byte[] value1 = { 0 }; byte[] value2 = { 0 }; value.Add(value0); value.Add(value1); value.Add(value2); int[] addr = { 45, 20, 0 }; int[] id = { Constant.MXAddrId, Constant.MXAddrId, Constant.MXAddrId }; int[] count = { 15, 1, 1 }; List <byte> cmdInLst = new List <byte>(); //反馈可变 dtClient.CmdOut = DTTcpCmdPackAndDataUnpack.PackWriteBitCmd(addr, id, count, value, cmdInLst); dtClient.sendTestData(); ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(dtClient.CmdOut)); ConstantMethod.ShowInfo(richTextBox1, ConstantMethod.byteToHexStr(cmdInLst.ToArray())); }
/// <summary> /// 本函数的思路: /// 1.根据需要读取的表格 进行分解成单个地址的单元下发指令 /// 2.信捷PLC 发送一条读取指令会包含D区起始地址和M区地址 并跟着相应的读取个数 /// 3.相邻地址相差7以内的 可以一次性读完 /// </summary> void PackCmdReadDMDataOut(DataTable dataForm0) { List <DTTcpPlcInfo> plcInfoLst = new List <DTTcpPlcInfo>(); DPlcInfo.Clear(); MPlcInfo.Clear(); //分段的参数 初始化 tcpClientManager.ReadDCmdOutLst.Clear(); tcpClientManager.ReadDCmdInLst.Clear(); tcpClientManager.ReadMCmdOutLst.Clear(); tcpClientManager.ReadMCmdInLst.Clear(); splitDLst.Clear(); splitMLst.Clear(); foreach (DataRow row in dataForm0.Rows) { int mAddr = -1; int count = -1; int mArea = -1; string strSplit2 = ""; string DSmode = ""; //单字还是双字 #region 获取地址个数区域 创建plcinfo类 并添加到集合 if (row == null) { return; } if (!getAddrAndArea(row, ref mAddr, ref mArea, ref DSmode, ref strSplit2, ref count)) { continue; } //这里数组进行统计 //传入数据起始地址 个数 区域 模式 DTTcpPlcInfo[] tmpInfoLst = DTTcpCmdPackAndDataUnpack.GetPlcInfo(mAddr, count, strSplit2, DSmode, DeviceId); if (tmpInfoLst.Count() > 0) { plcInfoLst.AddRange(tmpInfoLst); } #endregion } #region 排序 去重复 统计DM 起始点 //排序 按照绝对地址 plcInfoLst = plcInfoLst.OrderBy(x => x.AbsAddr).ToList(); //去重复 plcInfoLst = plcInfoLst.Distinct(new ModelComparerDTTcpInfo()).ToList(); //分离D 区 M区 if (DeviceId == Constant.xzjDeivceId) { DPlcInfo = plcInfoLst.FindAll(t => t.IntArea < (Constant.HSD_ID)); MPlcInfo = plcInfoLst.FindAll(t => t.IntArea > (Constant.HSD_ID)); } else { DPlcInfo = plcInfoLst.FindAll(t => t.IntArea > (Constant.MXAddrId)); MPlcInfo = plcInfoLst.FindAll(t => t.IntArea < (Constant.QWAddrId)); } #endregion #region 根据断点 建立命令的表格缓冲lst 然后创建读取DM区域的命令 tcpClientManager.ClearCmd(); if (DeviceId == Constant.xzjDeivceId) { //针对Dplc进行拆分 按照数据数量 进行拆分 逐渐加1 if (DPlcInfo.Count > Constant.maxplcInfoCount) { splitDLst = splitLstGet(DPlcInfo.Count, Constant.maxplcInfoCount); splitLstGetLst(splitDLst, DPlcInfo, tcpClientManager.ReadDCmdOutLst, tcpClientManager.ReadDCmdInLst, Constant.D_ID); } else { if (DPlcInfo.Count > 0) { DTTcpCmdPackAndDataUnpack.ASPLCPackReadDCmd (DPlcInfo.ToArray(), tcpClientManager.ReadDCmdOut, tcpClientManager.ReadDCmdIn); } } //针对Dplc进行拆分 按照数据数量 进行拆分 逐渐加1 if (MPlcInfo.Count > Constant.maxplcInfoCount) { splitMLst = splitLstGet(MPlcInfo.Count, Constant.maxplcInfoCount); splitLstGetLst(splitMLst, MPlcInfo, tcpClientManager.ReadMCmdOutLst, tcpClientManager.ReadMCmdInLst, Constant.M_ID); } else if (MPlcInfo.Count > 0) { DTTcpCmdPackAndDataUnpack.ASPLCPackReadMCmd (MPlcInfo.ToArray(), tcpClientManager.ReadMCmdOut, tcpClientManager.ReadMCmdIn); } //dataform表格里面地址对应dplcinfo和mplcinfo的数据对应起来 AsPlcFindIndexInPlcInfo(dataForm0, DPlcInfo, MPlcInfo); } else { DTTcpCmdPackAndDataUnpack. PackReadDCmd(DPlcInfo.ToArray(), tcpClientManager.ReadDCmdOut, tcpClientManager.ReadDCmdIn); DTTcpCmdPackAndDataUnpack. PackReadMCmd(MPlcInfo.ToArray(), tcpClientManager.ReadMCmdOut, tcpClientManager.ReadMCmdIn); //dataform表格里面地址对应dplcinfo和mplcinfo的数据对应起来 FindIndexInPlcInfo(dataForm0, DPlcInfo, MPlcInfo); } }
public event socDataProcess EventDataProcess;//利用委托来声明事件 #region 数据写操作 public bool SetMultipleDMArea(int Addr, int count, List<byte[]> value, string Area) { if (isDeviceReady) { List<int> addr = new List<int>(); List<int> addressid = new List<int>(); List<int> count0 = new List<int>(); int addrid = DTTcpCmdPackAndDataUnpack.GetIntAreaFromStr(Area); if (DeviceId == Constant.xzjDeivceId) { addrid = XJPLCPackCmdAndDataUnpack.AreaGetFromStr(Area); for (int i = 0; i < count; i++) { if(value.Count>0) addr.Add(Addr + i*(value[0].Count()/2)); addressid.Add(addrid); count0.Add(1); } } else for (int i = 0; i < count; i++) { addr.Add(Addr+i); addressid.Add(addrid); count0.Add(1); } SetDMCmdOut.Clear(); SetDMCmdIn.Clear(); if (DeviceId == Constant.xzjDeivceId) { if (addrid < Constant.HSD_ID) SetDMCmdOut.AddRange(DTTcpCmdPackAndDataUnpack.AsPlcPackWriteByteCmd(addr.ToArray(), addressid.ToArray(), count0.ToArray(), value, SetDMCmdIn)); else SetDMCmdOut.AddRange(DTTcpCmdPackAndDataUnpack.AsPlcPackWriteBitCmd(addr.ToArray(), addressid.ToArray(), count0.ToArray(), value, SetDMCmdIn)); } else { if (addrid > Constant.MXAddrId) SetDMCmdOut.AddRange(DTTcpCmdPackAndDataUnpack.PackWriteByteCmd(addr.ToArray(), addressid.ToArray(), count0.ToArray(), value, SetDMCmdIn)); else SetDMCmdOut.AddRange(DTTcpCmdPackAndDataUnpack.PackWriteBitCmd(addr.ToArray(), addressid.ToArray(), count0.ToArray(), value, SetDMCmdIn)); } ConstantMethod.Delay(200); //延时一下 防止数据发太快 监控上看不到 //防止前面在写数据 while (isWriteCmd) { Application.DoEvents(); } isWriteCmd = true; ConstantMethod.DelayWriteCmd(Constant.WriteCommTimeOut, ref isWriteCmd); return (!isWriteCmd); } return false; }