/// <summary> /// DEBUG purpose :Sends a packet and returns as string whatever is answered by arduino /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public string DEBUG_SendPacketAndReturnLineContent(ARCPO_Packet pPacket) { pPacket.mExpectAcknowledge = true; bool vSent = pPacket.SendPacket(this.mPort); Thread.Sleep(1000); if (this.mPort.BytesToRead > 0) { return(this.mPort.ReadExisting()); } else { return(null); } }
/// <summary> /// Sends a packet, retrying in case it fails and returns if ok /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public bool SendPacket(ARCPO_Packet pPacket) { if (!this.IsSerialConnected() && !this.ConnectSerial()) { return false; } bool vOk = false; int vTryCount = 0; while (!vOk && vTryCount < this.RetryCount) { //force the expect acknowledge except if no retry asked pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge); //try to make sure we release the wait for ack flag try { if (pPacket.mExpectAcknowledge) { lock (mWaitingForAckSynchroKey) { this.mWaitingForAck = true; } } bool vSent = pPacket.SendPacket(this.mPort); if (vSent && pPacket.mExpectAcknowledge) { //locks the line lock (this.mPollingMessageSynchroKey) { #region get the acknowledgement packet //tries n times to read a acknowledge while (!vOk && vTryCount < this.RetryCount) { //wait longer and longer Thread.Sleep(100 * vTryCount); bool vAckOk = false; try { ARCPO_Packet vAck = new ARCPO_Packet(); vAckOk = vAck.ReadPacket(this.mPort); if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK")) { vOk = true; } } catch (TimeoutException) { } vTryCount++; } #endregion } } else { //retry } } catch (InvalidOperationException) { //disconnected ? this.DisconnectSerial(); } finally { //anycases, release the line this.mWaitingForAck = false; } //count the tries vTryCount++; } return vOk; }
/// <summary> /// Sends a packet, retrying in case it fails and returns if ok /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public bool SendPacket(ARCPO_Packet pPacket) { if (!this.IsSerialConnected() && !this.ConnectSerial()) { return(false); } bool vOk = false; int vTryCount = 0; while (!vOk && vTryCount < this.RetryCount) { //force the expect acknowledge except if no retry asked pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge); //try to make sure we release the wait for ack flag try { if (pPacket.mExpectAcknowledge) { lock (mWaitingForAckSynchroKey) { this.mWaitingForAck = true; } } bool vSent = pPacket.SendPacket(this.mPort); if (vSent && pPacket.mExpectAcknowledge) { //locks the line lock (this.mPollingMessageSynchroKey) { #region get the acknowledgement packet //tries n times to read a acknowledge while (!vOk && vTryCount < this.RetryCount) { //wait longer and longer Thread.Sleep(100 * vTryCount); bool vAckOk = false; try { ARCPO_Packet vAck = new ARCPO_Packet(); vAckOk = vAck.ReadPacket(this.mPort); if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK")) { vOk = true; } } catch (TimeoutException) { } vTryCount++; } #endregion } } else { //retry } } catch (InvalidOperationException) { //disconnected ? this.DisconnectSerial(); } finally { //anycases, release the line this.mWaitingForAck = false; } //count the tries vTryCount++; } return(vOk); }
/// <summary> /// DEBUG purpose :Sends a packet and returns as string whatever is answered by arduino /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public string DEBUG_SendPacketAndReturnLineContent(ARCPO_Packet pPacket) { pPacket.mExpectAcknowledge = true; bool vSent = pPacket.SendPacket(this.mPort); Thread.Sleep(1000); if (this.mPort.BytesToRead > 0) { return this.mPort.ReadExisting(); } else { return null; } }