Click here to Skip to main content
15,898,640 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

This related to a question asked a little while ago in
a for loop not loop not looping[^]
I am using the WaitForData routine but need to modify it
so as the
C#
while (timeWaited < Timeout  && myComPort.BytesToRead < NbDataToWait)

which when used as
C#
Value = WaitForData(4, 100);

will wait for 4 Bytes of data and time out 100 mS, I would like it to wait a > coming back that the board has seen the command. A better way (faster) would be to wait for an OK from the board would it be more sensible method be below demoed in a button click
C#
<pre lang="cs">private void button1_Click(object sender, EventArgs e)
      {
          string TextContaing;
          int LocationOf;

          TextContaing = textBox1.Text;
          MessageBox.Show(TextContaing);


          LocationOf = TextContaing.IndexOf(">");
          if (LocationOf > 0)
          {
              MessageBox.Show("Got One!   "+LocationOf.ToString());
          }

      }


What is the correct method of waiting for an OK or a > from the board
Posted
Updated 15-Feb-12 4:27am
v2

1 solution

Hi.

You could try this. It should read all the data on COM3 and return the values.
Thereafter it is a simple case of checking for the ">" character.

Unfortunately without any further information i cannot provide a more accurate solution.

C#
static bool WaitForData(int NbDataToWait, int Timeout)
        {
            bool Result = false;
            int TimeWaited = 0;
            System.IO.Ports.SerialPort SPort = new System.IO.Ports.SerialPort();

            SPort.PortName = "COM3";
            SPort.Open();

            byte[] MyResult = new byte[SPort.BytesToRead];

            SPort.Read(MyResult, 0, SPort.BytesToRead);

            while (TimeWaited < Timeout && SPort.BytesToRead < NbDataToWait)
            {
                for (int i = 0; i < MyResult.Length; i++)
                {
                    if (MyResult[i].ToString() == ">")
                    {
                        Result = true;
                    }
                }

                TimeWaited++;
            }

            return Result;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900