public bool Connect(string WaitForPrompt) { try { // If the connect worked, setup a callback to start listening for incoming data // Dim recieveData As New AsyncCallback(AddressOf OnRecievedData) client.Open(); RecieveData recievedata = new RecieveData(Recieve); recievedata.BeginInvoke(new AsyncCallback(OnRecievedData), recievedata); // Threading.Thread.Sleep(5000) try { this.WaitFor(WaitForPrompt); } catch { throw new Exception("Prompt Not Returned"); } return true; } catch (Exception ex) { writelog(System.Environment.NewLine + "------54----------" + ex.Message + System.Environment.NewLine + ex.Source + "----------------"); return false; } }
private void ReceiveData() { string SendData = ""; int ByteCount = 0; int TermIndex; String RecieveData; Byte[] BytesOut = new Byte[256]; Byte[] BytesIn = new Byte[256]; if (isSenCon) { try { //Apend end code to Send Data SendData += "X"; //Clear BytesOut //Translate the passed message BytesOut = Encoding.ASCII.GetBytes(SendData); //Clear out Send Data SendData = "A"; //Send the message to the sensor client SenStream.Write(BytesOut, 0, BytesOut.Length); //Read the incoming data ByteCount = SenStream.Read(BytesIn, 0, BytesIn.Length); RecieveData = Encoding.ASCII.GetString(BytesIn, 0, ByteCount); //Parse and process the incoming data //Get and check the vertical TermIndex = RecieveData.IndexOf("X", StringComparison.Ordinal); VerLoc = VerticalCount2Loc(int.Parse(RecieveData.Substring(22, TermIndex - 22))); //Get and check the traverse TraLoc = TransverseVolt2Loc(double.Parse(RecieveData.Substring(6, 7))); //Get and check the lateral LatLoc = LateralVolt2Loc(double.Parse(RecieveData.Substring(14, 7)));//Recalibrated by BG, 2/8/13, for use with 4X loop on sensor //Check the pressure, door, and E-stop button //isDoorClosed = (int.Parse(RecieveData.Substring(0, 1)) == 1); isDoorClosed = true; isPressure = (int.Parse(RecieveData.Substring(2, 1)) == 1); isEmergencyButton = (int.Parse(RecieveData.Substring(4, 1)) == 1); } catch (ArgumentNullException ex) { MessageBox.Show("Error Receiving Data From Mystery Box, I would try to disconecct and power cycle the Mystery Box and connect again. -J\n\n" + ex.Message.ToString(), "Error Receiving Data From Mystery Box"); } } }
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() + "----------------"); } } }
public Form1() { InitializeComponent(); getAvailablePorts(); comboBox2.SelectedIndex = 12; comboBox3.SelectedIndex = 12; serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); serialPort2.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2); recieve = new RecieveData(DataRecieve); recieve2 = new RecieveData(DataRecieve2); string appPath = Path.GetDirectoryName(Application.ExecutablePath); if (File.Exists(appPath + "\\conf.ini")) { PopulateCommands(appPath + "\\conf.ini"); PopulateCommands2(appPath + "\\conf.ini"); } }