static public void MessageHandler() { while (true) { if (!WaitEvent.WaitOne(10, false)) { agent.ScanPort(); continue; } WaitEvent.Reset(); try { ActMessage msg = NodeMaster.actmsg; #region write_regs if ((msg.action == "write_regs") && (msg.addrs.GetLength(0) == 1)) //only reg of one node is allowed { byte addr = msg.addrs[0]; if (agent[addr].status == NodeStatus.ST_IDLE) { agent[addr].write_vregs(msg.regs, msg.values); } actmsg.action = "done"; OverEvent.Set(); continue; } #endregion #region actions if ((msg.action == "fill") || (msg.action == "zero") || (msg.action == "empty") || (msg.action == "pass") || (msg.action == "pass")) { foreach (byte addr in msg.addrs) { if (agent[addr].status == NodeStatus.ST_IDLE) { agent[addr].write_vregs(new string[] { "flag_enable" }, new UInt32[] { NodeMaster.flag_cmd(msg.action) }); } } actmsg.action = "done"; OverEvent.Set(); continue; } if (msg.action == "flag_goon") { foreach (byte addr in msg.addrs) { if (agent[addr].status == NodeStatus.ST_IDLE) { agent[addr].write_vregs(new string[] { "flag_goon" }, new UInt32[] { 1 }); } } actmsg.action = "done"; OverEvent.Set(); continue; } #endregion #region refresh_regs if ((msg.action == "refresh_regs") && (msg.addrs.GetLength(0) == 1)) { if (agent[msg.addrs[0]] is SubNode) { GetRegister(msg.addrs[0], msg.regs); OverEvent.Set(); continue; } } #endregion #region flag_release if (msg.action == "flag_release") { if (!CriticalFlag(msg.addrs, "flag_release", 1)) { msg.action = "fail"; } else { foreach (byte n in msg.addrs) { if (agent[n] is WeighNode) { (agent[n] as WeighNode).cnt_match = -1; } } msg.action = "done"; } OverEvent.Set(); continue; } #endregion #region flash if (msg.action == "flash") { msg.action = "done"; foreach (byte addr in msg.addrs) { if (agent[addr].status == NodeStatus.ST_IDLE) { agent[addr].write_vregs(new string[] { "NumOfDataToBePgmed" }, new UInt32[] { 45 }); int retry = 20; do { Thread.Sleep(200); if (GetRegister(addr, new string[] { "NumOfDataToBePgmed" })) { if (agent[addr]["NumOfDataToBePgmed"] == 0) { break; } } } while (retry-- > 0); if (retry <= 0) { msg.action = "retry"; } } } OverEvent.Set(); continue; } #endregion #region start stop trigger if ((msg.action == "stop") || (msg.action == "start") || (msg.action == "trigger") || (msg.action == "intf")) { if (!CriticalFlag(msg.addrs, "flag_enable", NodeMaster.flag_cmd(msg.action))) { msg.action = "retry"; } else { if ((msg.action == "stop") || (msg.action == "start")) { foreach (byte n in msg.addrs) { if (agent[n] is WeighNode) { (agent[n] as WeighNode).cnt_match = -1; } } } msg.action = "done"; } OverEvent.Set(); continue; } #endregion #region query if (msg.action == "query") { foreach (byte addr in msg.addrs) { if (agent[addr].status == NodeStatus.ST_IDLE) { GetRegister(addr, new string[] { "mtrl_weight_gram", "mtrl_weight_decimal" }); } } actmsg.action = "done"; OverEvent.Set(); continue; } #endregion throw new Exception("Invalide commnad " + actmsg.action); } catch (Exception ex) { Debug.WriteLine(ex.Message); actmsg.action = "fail"; OverEvent.Set(); } } }