//进行程序启动,停止的控制 //01表示停止 //02表示继续 public void ProcessStopRun(string RunOrStop) { //进行停止程序的控制 if (RunOrStop == "01") { m_autorunfalg = 2; m_lastStopTicket = HyTick.GetTickCount(); LogHelper.Log("************" + System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + "程序运行中止" + "***********"); } //进行启动程序的控制 else if (RunOrStop == "02") { m_autorunfalg = 1; m_StopPeriod_tick = m_StopPeriod_tick + HyTick.GetTickCount() - m_lastStopTicket; LogHelper.Log("************" + System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + "程序运行继续" + "***********"); } }
//获取命令缓冲区中的命令信息 public int cmdmany_getcmd(int devindex) { int ExecSec = 0; if (m_autorunfalg == 0) { m_CmdIndex = 0; return(0); } else if (m_autorunfalg == 1) { if (m_CmdIndex == 0 && m_process_tick == 0) { m_process_tick = HyTick.GetTickCount(); //设定当前的Ticket时钟 m_StopPeriod_tick = 0; } if (SequenceCmdList.Count > 0 && m_CmdIndex < SequenceCmdList.Count) //表示有多命令要开始执行了 { if (devindex == SequenceCmdList[m_CmdIndex].devIndex) // { ExecSec = SequenceCmdList[m_CmdIndex].secs * 1000 + m_StopPeriod_tick; if (HyTick.TickTimeIsArrived(m_process_tick, ExecSec)) //说明时间到了需要下发命令了,进行下发命令的组织。 { devd[devindex].CmdFlag = true; m_CmdIndex = m_CmdIndex + 1; return(m_CmdIndex); } } } if (m_CmdIndex == SequenceCmdList.Count) //表示已经执行完成 { m_autorunfalg = 0; m_CmdIndex = 0; LogHelper.Log("************" + System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + "程序运行成功结束" + "***********"); return(m_CmdIndex); } } return(0); }
//处理总的通讯调度 public int process(int m_appdataindex) { bool acmd = false; try { switch (m_process_step) { case 0: //建立IP端口的连接 if (m_appdata.devd[m_appdataindex].ComType == "TCP") { AppMain_start(); } m_process_step = 1; //主通信流程调度 break; case 1: //判断端口是否连接成功 if (m_appdata.devd[m_appdataindex].ComType == "TCP" && m_Comm.Connected == true) { m_appdata.devd[m_appdataindex].CommFlag = true; m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, true); //更新设备设备的通讯状态 m_process_step = 2; //连接成功准备发送命令 } else { if (m_appdata.devd[m_appdataindex].ComType == "TCP") { m_process_step = 0; //否则进行重新连接 m_appdata.devd[m_appdataindex].CommFlag = false; m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, false); //更新设备设备的通讯状态 } else { m_appdata.devd[m_appdataindex].CommFlag = true; m_appdata.Process_EquCommFlag(m_appdata.devd[m_appdataindex].devAddr, true); m_process_step = 2; //连接成功准备发送命令 } } break; case 2: acmd = false; int cmdindex = 0; cmdindex = m_appdata.cmdmany_getcmd(m_appdataindex); if (cmdindex > 0) //判断是否有批命令发送 { if (m_appdata.devd[m_appdataindex].CmdFlag == true) // 获取当前的命令 { // 根据cmdindex获取命令 acmd = true; } } if (acmd) //根据获取到的 { string sendIP = string.Empty; string ComType = m_appdata.devd[m_appdataindex].ComType; byte[] sendbyte = m_appdata.Create_cmdbyte_ByIndex(cmdindex - 1); if (ComType == "TCP") { m_Comm.SendData(sendbyte, ref sendIP); m_process_step = 4; //处理发送后的反馈 } else if (ComType == "UDP") //UDP方式的命令发送 { UdpSendMessage(m_appdataindex, sendbyte); //需要进行处理接收事务 if (m_appdata.devd[m_appdataindex].remoteSendDateChk == "TRUE") { m_appdata.Process_EquComm_RunStatus("01"); //暂停当前的进程 m_UdpServer = new UdpServer(); m_UdpServer.StartListen(m_appdata.devd[m_appdataindex].LocalIp, m_appdata.devd[m_appdataindex].Recvport); if (m_UdpServer.RecvLength > 0) { m_appdata.Process_EquComm_RunStatus("02"); //继续执行 } else { //执行程序中止执行 m_appdata.Process_EquComm_RunStatus("03"); //中止执行,在这里需要调用 } } m_process_step = 2; } LogHelper.Log(System.DateTime.Now.ToString(HyConst.DATETIME_yMdHmsf_STRING) + " " + ComType + (cmdindex - 1).ToString("00") + "Send:" + HexEncoding.Instance.GetString(sendbyte)); m_appdata.Process_EquComm_Message(System.DateTime.Now, m_appdata.GetSendMessage(cmdindex - 1), true, true); /* * if (cmdindex == 1) * { * m_appdata.CmdIndexStatus(cmdindex, true); * } * if (cmdindex > 1 && m_appdata.SequenceCmdList[cmdindex].secs > m_appdata.SequenceCmdList[cmdindex - 1].secs) * { * m_appdata.CmdIndexStatus(cmdindex, true); * } */ m_appdata.CmdIndexStatus(cmdindex - 1, true); } else { string sendIP = string.Empty; byte[] sendbyte = m_appdata.GetCmd_Bydev(m_appdataindex); m_Comm.SendData(sendbyte, ref sendIP); m_appdata.Process_EquComm_Message(System.DateTime.Now, HexEncoding.Instance.GetString(sendbyte), true, false); m_process_step = 3; m_process_tick = HyTick.GetTickCount(); } break; case 3: //处理获得的数据内容 if (m_Protocol.m_recvdatalen > 0) { //处理接收到的数据 Receive_Date(m_appdataindex, m_Protocol.m_recvdata); m_appdata.Process_EquComm_Message(System.DateTime.Now, HexEncoding.Instance.GetString(m_Protocol.m_recvdata), false, true); //处理完了就清除掉数据 m_Protocol.m_recvdatalen = 0; //处理完成后继续发送 m_process_step = 2; } else { if (HyTick.TickTimeIsArrived(m_process_tick, 1000)) { m_process_step = 2; } } // 判断通信,只有TCP的方式才进行这么处理 if (m_Comm.Connected == false && m_appdata.devd[m_appdataindex].ComType == "TCP") { m_process_step = 0; } break; case 4: //Tcp的方式处理控制命令返回协议处理 if (m_Protocol.m_recvdatalen > 0) //只要接收到数据就进行继续发送 { m_process_step = 2; } break; } return(0); } catch { return(-2); } }