Author Topic: Need a ReadExisting() example for C#....  (Read 13262 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Need a ReadExisting() example for C#....
« on: February 17, 2012, 06:32:40 am »
Hi All,

I am reading back for a board and I think the board is 'randomly' dropping '>' which is used as a prompt occasionally I think one of these is not sent with the data meaning that my ReadExisting()
does not catch the data while it does appear in a monitor program I am running in the Event Handler.

Glenn

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Need a ReadExisting() example for C#....
« Reply #1 on: February 17, 2012, 10:01:15 am »
Is the problem that the sender is not sending ">" or is the problem that the receiver is not seeing every character sent?

Jan

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Need a ReadExisting() example for C#....
« Reply #2 on: February 17, 2012, 10:58:49 am »
It looked like I was not waiting long enough. What I am doing was is using the data Serial Data Received Event Handler to check via ReadExisting() and then checking for a '>' with  if (End = text.EndsWith(">")) it appears that the string every so often does not have '>' so the PC ignores it.  Also I want the program to STOP dead and ideally put a Message Box up to high light this fact.
public int WaitForData(int Timeout)
        {
         //   bool Respond = false;
            //bool WaitforPrompt = false;

            Stopwatch sw = Stopwatch.StartNew();

               this.Invoke(new MethodInvoker(delegate()
               {
                   ReplyData = rtbIncoming.Text;
               }));

                //if((ReplyData.Contains(">")||(ReplyData.Contains("OK"))))
                //                   Respond = true;
                //else
                //                    Respond = false;

            while (sw.ElapsedMilliseconds < Timeout && myComPort.BytesToRead > 0)// && Respond == false)// && rtbIncoming.Text.Contains(">") || rtbIncoming.Text.Contains("OK"))
            {
                lblwhere.Text += ".";
                System.Threading.Thread.Sleep(1);  // sleep for 1 millisecond
            }

            int elapsed = (int)sw.ElapsedMilliseconds;
            sw.Stop();
            return elapsed;
        }

That is the timer I am using to try to govern the comms calling via value = WaitForData(10); value how ever does not have any thing returned to it.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Need a ReadExisting() example for C#....
« Reply #3 on: February 17, 2012, 12:30:44 pm »
If the sender isn't sending ">", you need to find out why.

If the sender is sending ">" but you're not seeing it with ReadExisting, are you reading multiple times to be sure you get everything that's being sent? You may need to piece together the complete command from multiple reads.

Jan