Author Topic: How to wait for a reply without blocking port?  (Read 19467 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
How to wait for a reply without blocking port?
« on: December 09, 2011, 11:02:11 am »
Hi All,

I am writing a bit of C# code to upload some dummy values to a board.
I have the code that spits out the relavent string in the Event handler I have
        private void ProcessBuffer()
        {

            byte[] bytes = new byte[buffer.Count];
            buffer.CopyTo(0, bytes, 0, buffer.Count);
            buffer.RemoveRange(0, buffer.Count);
            string data;

            rtbIncoming.Clear();
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            data = enc.GetString(bytes);
            this.BeginInvoke(new MethodInvoker(delegate() { rtbIncoming.Text += data; }));// @@@ JG change
            // MessageBox.Show(data);
//need to check for an OK coming back

           

           
        }
        private void myComPort_DataRecieved(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte[] data = new byte[myComPort.BytesToRead];
            myComPort.Read(data, 0, data.Length);
            if ((data.Length > 1) && (data[data.Length - 1] == 13))
            {
                Array.Resize(ref data, data.Length);
            }
            buffer.AddRange(data);
            ProcessBuffer();
         
           
        }


This was the standard Event handler I have been using for a age but I need to change it to wait for an OK opions please, would I be better off waiting in a timer for the OK or not?

Glenn

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: How to wait for a reply without blocking port?
« Reply #1 on: December 09, 2011, 02:04:49 pm »
The whole point of the DataReceived event is to wait for a reply without blocking the main app. If you need to wait for a specific "OK" response, the DataReceived code can check for that. Maybe I don't understand what you want to do.

Jan

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: How to wait for a reply without blocking port?
« Reply #2 on: December 12, 2011, 06:47:26 am »
I want the reply in  myComPort_DataRecieved to search for an OK 0D before it sends the next, as I can send correctly one piece of data the reply of '0D OK 0D >' what I am thinking is might the flag go up That an interrupt has happened it does not see the whole string just the initial 0D? I need to wait for a little while for the whole reply to come back I think experimentation continues..... :P