public void OnDataReceived(IDtuConnection c, DtuMsg msg) { if (msg == null) { Log.ErrorFormat("Received a null msg from {0}.", c); return; } if (!msg.IsOK()) { Log.DebugFormat("Invalid Msg, dropped."); return; } TranMsg tm = new TranMsg(msg.Databuffer); // Send ack. TranMsg ack = new HeartBeatTranMsg(Convert.ToInt32(msg.DtuId), tm.LoadSize); // ACK ack.ID = tm.ID; // SAME. c.Asend(ack.Marshall()); if (TranMsgType.HeartBeat == tm.Type) { // HeartBeat. Log.DebugFormat("heartBeat, send ack and continue"); return; } Log.DebugFormat("OnDataReceived: id={4},result={0}, len={1}, pkg={2}/{3}", msg.ErrorCode, msg.Databuffer != null ? msg.Databuffer.Length : 0, tm.PackageIndex, tm.PackageCount, tm.ID); if (!tm.IsLastPackage()) { // 入列. this.EnqueueMsg(tm); return; } // 最后一包已到达, 或仅一包. TranMsg outMsg = null; if (tm.PackageCount > 1) { Log.DebugFormat("Last package, combine them."); outMsg = this.CombineMsg(tm.Type); //组包 } else { outMsg = tm; //单包. } // 委托调用. if (this.OnTranMsgReceived != null) { this.OnTranMsgReceived(tm.Type, outMsg); } }
public bool HeartBeat() { if (this._dtuConnection.IsAvaliable()) { HeartBeatTranMsg req = new HeartBeatTranMsg(this.DtuCode, 0); TranAckMsg resp; if (this.SSend(req, 5, out resp)) { return(true); } } return(false); }