void BPPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { BPPort port = sender as BPPort; if (port != null && port.BytesToRead > 0 && e.EventType == SerialData.Chars) { string data = port.ReadExisting(); if (bp_processing) { int i_start = 0, i_end = 0; while (i_start < data.Length && (i_end = data.IndexOf(NewLine, i_start)) >= 0) { string cmd = bp_prefix; bp_prefix = string.Empty; if (i_end > i_start) { cmd += data.Substring(i_start, i_end - i_start); } if (cmd.Length > 0) { ProcessResponse(cmd); } i_start = i_end + 1; } if (i_start < data.Length) { bp_prefix = data.Substring(i_start); if (!string.IsNullOrEmpty(bp_prefix) && bp_waitFor != null && bp_waitFor.Contains(bp_prefix)) { bp_lines.Add(bp_prefix); bp_prefix = string.Empty; bp_waitFound = true; } } } else { bp_prefix = string.Empty; } if (EnableRecieveData && RecieveData != null && (_recieveDataAsyncResult == null || _recieveDataAsyncResult.IsCompleted)) { try { if (_recieveDataAsyncResult != null) { RecieveData.EndInvoke(_recieveDataAsyncResult); } } finally { _recieveDataAsyncResult = RecieveData.BeginInvoke(this, data, null, null); } } } }
/// <summary> /// Callback function for recieve data /// </summary> /// <param name="result"></param> /// <remarks></remarks> private void OnRecievedData(IAsyncResult result) { var resultClass = (AsyncResult)result; RecieveData d = (RecieveData)resultClass.AsyncDelegate; Int32 sz = d.EndInvoke(result); if (sz > 0) { string sRecieved = CleanDisplay(Encoding.ASCII.GetString(m_byBuff, 0, sz)); // Write out the data //If sRecieved.IndexOf("[c") <> -1 Then // Negotiate(1) //End If //If sRecieved.IndexOf("[6n") <> -1 Then // Negotiate(2) //End If this.strWorkingData.Append(sRecieved.ToLower()); this.strFullLog.Append(sRecieved); this.LOG = this.LOG + sRecieved; //Console.Write(sRecieved); //here take some rest System.Threading.Thread.Sleep(10); //create a new delegate RecieveData recievedata = new RecieveData(Recieve); recievedata.BeginInvoke(new AsyncCallback(OnRecievedData), recievedata); } else { try { if (client.Active) //here if client is still active then { //here take some rest System.Threading.Thread.Sleep(10); RecieveData recievedata = new RecieveData(Recieve); recievedata.BeginInvoke(new AsyncCallback(OnRecievedData), recievedata); } } catch (Exception ex) { writelog(System.Environment.NewLine + "------139----------" + ex.Message + System.Environment.NewLine + ex.Source.ToString() + "----------------"); } } }