/// <summary> /// 待发送的指令管道调度 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="rBuf">收到正确的回传数据</param> /// <param name="wBuf">准备发送的指令数据</param> private static void SciScheduling(ref MBSci sci, ref byte[] rBuf, ref byte[] wBuf) { if (sciLock) //如果被加锁 直接退出 { return; } if (sci.cmd[0].addr == -1) { return; } if ((sci.cmd[sci.index].res != 0) || (sci.count >= sci.maxRepeatCount)) { sci.count = 0; //发送次数清零 if (sci.cmd[sci.index].res != 0) //如果收到了正常返回 { ReceiveDataProcess(rBuf, sci.cmd[sci.index].addr); //保存数据 rBuf = null; //清空当前接收缓冲区的内容, 以防下次重复读取 } else { //参数操作失败 } SciJumbNext(ref sci); } wBuf = SendPduPack((int)gNode, sci.cmd[sci.index]); //发送指令操作 sci.count++; //发送次数加1 }
/// <summary> /// 清空一次性读取指令集 /// </summary> /// <param name="sci">待发送的指命管道</param> private static void SciClearCmd(ref MBSci sci) { for (int i = sci.rtCount; i < MB_SCI_MAX_COUNT; i++) { sci.cmd[i].addr = -1; sci.cmd[i].len = 0; sci.cmd[i].res = 0; } }
/// <summary> /// 添加重复操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="addr">所添加指令的首地址</param> /// <param name="len">所添加指令的寄存器或线圈个数</param> /// <param name="func">所添加指令的功能码</param> private static void SciAddRepeatCmd(ref MBSci sci, int addr, int len, int func) { if (sci.rtCount >= MB_SCI_MAX_COUNT - 1) //超出指令管道最大长度 直接退出 { return; } if (len == 0) //地址的数据长度为空 直接退出 { return; } sci.cmd[sci.rtCount].addr = addr; sci.cmd[sci.rtCount].len = len; sci.cmd[sci.rtCount].func = func; sci.cmd[sci.rtCount].res = 0; sci.rtCount++; }
/// <summary> /// 添加一次性操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="addr">所添加指令的首地址</param> /// <param name="len">所添加指令的寄存器或线圈个数</param> /// <param name="stat">所添加指令的功能码</param> private static void SciAddCmd(ref MBSci sci, int addr, int len, int stat) { //if (len == 0) //地址的数据长度为空 直接退出 // return; for (int i = sci.rtCount; i < MB_SCI_MAX_COUNT; i++) { if (sci.cmd[i].addr == -1) //把指令载入到空的管道指令上 { sci.cmd[i].addr = addr; sci.cmd[i].len = len; sci.cmd[i].stat = stat; sci.cmd[i].res = 0; break; } } }
/// <summary> /// 跳到下一个操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> private static void SciJumbNext(ref MBSci sci) { if (sci.index >= sci.rtCount) //非实时读取地址会被清除 { sci.cmd[sci.index].addr = -1; sci.cmd[sci.index].len = 0; sci.cmd[sci.index].func = 0; } do { sci.index++; if (sci.index >= MB_SCI_MAX_COUNT) //超出指令最大范围 { sci.index = 0; if (sci.rtCount == 0) //如果固定实时读取 为空 直接跳出 { break; } } } while (sci.cmd[sci.index].addr == -1); sci.cmd[sci.index].res = 0; //本次返回状态清零 }
/// <summary> /// 清空重复读取指令集 /// </summary> /// <param name="sci">待发送的指命管道</param> private static void SciClearRepeatCmd(ref MBSci sci) { sci.rtCount = 0; }
/// <summary> /// 待发送的指令管道调度 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="rBuf">收到正确的回传数据</param> /// <param name="wBuf">准备发送的指令数据</param> private static void SciScheduling(ref MBSci sci, ref byte[] rBuf, ref byte[] wBuf) { if (sciLock) //如果被加锁 直接退出 return; if (sci.cmd[0].addr == -1) { return; } if ((sci.cmd[sci.index].res != 0) || (sci.count >= sci.maxRepeatCount)) { sci.count = 0; //发送次数清零 if (sci.cmd[sci.index].res != 0) //如果收到了正常返回 { ReceiveDataProcess(rBuf, sci.cmd[sci.index].addr); //保存数据 rBuf = null; //清空当前接收缓冲区的内容, 以防下次重复读取 } else { //参数操作失败 } SciJumbNext(ref sci); } wBuf = SendPduPack((int)gNode, sci.cmd[sci.index]); //发送指令操作 sci.count++; //发送次数加1 }
/// <summary> /// 跳到下一个操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> private static void SciJumbNext(ref MBSci sci) { if (sci.index >= sci.rtCount) //非实时读取地址会被清除 { sci.cmd[sci.index].addr = -1; sci.cmd[sci.index].len = 0; sci.cmd[sci.index].stat = 0; } do{ sci.index++; if (sci.index >= MB_SCI_MAX_COUNT) //超出指令最大范围 { sci.index = 0; if (sci.rtCount == 0) //如果固定实时读取 为空 直接跳出 break; } } while (sci.cmd[sci.index].addr == -1); sci.cmd[sci.index].res = 0; //本次返回状态清零 }
/// <summary> /// 添加重复操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="addr">所添加指令的首地址</param> /// <param name="len">所添加指令的寄存器或线圈个数</param> /// <param name="stat">所添加指令的功能码</param> private static void SciAddRepeatCmd(ref MBSci sci, int addr, int len, int stat) { if (sci.rtCount >= MB_SCI_MAX_COUNT - 1) //超出指令管道最大长度 直接退出 return; if (len == 0) //地址的数据长度为空 直接退出 return; sci.cmd[sci.rtCount].addr = addr; sci.cmd[sci.rtCount].len = len; sci.cmd[sci.rtCount].stat = stat; sci.cmd[sci.rtCount].res = 0; sci.rtCount++; }
/// <summary> /// 添加一次性操作指令 /// </summary> /// <param name="sci">待发送的指命管道</param> /// <param name="addr">所添加指令的首地址</param> /// <param name="len">所添加指令的寄存器或线圈个数</param> /// <param name="stat">所添加指令的功能码</param> private static void SciAddCmd(ref MBSci sci, int addr, int len, int stat) { if (len == 0) //地址的数据长度为空 直接退出 return; for (int i = sci.rtCount; i < MB_SCI_MAX_COUNT; i++) { if (sci.cmd[i].addr == -1) //把指令载入到空的管道指令上 { sci.cmd[i].addr = addr; sci.cmd[i].len = len; sci.cmd[i].stat = stat; sci.cmd[i].res = 0; break; } } }