Author Topic: Data recieved event triggering with no data??  (Read 16702 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Data recieved event triggering with no data??
« on: November 01, 2010, 05:36:54 am »
Hi All,

I am trying to use the DataRecieved event to read a device so far it has produced an oddity the event is raised saying there is data but no data is not there so my app goes splat.
Can I use BytesToRead as Serial Port Complete shows on page 191

if(ComPort.BytesToRead >0)
{
    receivedData = ComPort.ReadBytes();
    rtbIncomingDUT.Text +=receivedData;
}
else
{
   NoData = true;
}
Taking that receivedData is a char and NoData is a boolean will this work?
Glenn (got to go willl up date further when free)

 




Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Data recieved event triggering with no data??
« Reply #1 on: November 03, 2010, 08:40:08 am »
That should work. In fact, the MS documentation for DataReceived says:

Use the BytesToRead property to determine how much data is left to be read in the buffer.

Jan

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Data recieved event triggering with no data??
« Reply #2 on: November 03, 2010, 05:05:20 pm »
Hi,

If I make the event handler where I was going to put the while loop below but change ReadByte() to a ReadExisting() so the loop becomes
   
while(DUTComPort.BytesToRead > 0)
   {
      boolFalseTrip = false;   
      DUTdata = DUTComPort.ReadExisting();
      rtbDUTIncoming.Text += DUTdata;
   }
   if(DUTComPort.BytesToRead == 0)
   {
        boolFalseTrip = true;
    }

will this get around the problem I am having with the interrupt being tripped with no data???
Glenn


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Data recieved event triggering with no data??
« Reply #3 on: November 03, 2010, 09:18:36 pm »
ReadExisting will return instead of hanging if there is no data, which should help.

Jan

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Data recieved event triggering with no data??
« Reply #4 on: November 08, 2010, 06:08:15 am »
Hi Jan (& anybody else out there!)

I have a project that is being awkward it involves two board of custom design (PIC based).  I am being forced to use the below function for Comms to the board due to an earlier mess.  It's principle is fairly simple Write() sends a command and waits in the function for a reply. I have a timer running to avoid lock ups of the board not answering which could mean it has crashed.  The problem I am now seeing it the recieved data flag is being raised one byte comes in and that satisfys the test conditions the data gets processed and there is not a complete byte ready. Instead of it being #RE=152,143,112 it only #RE=152,  it gets past the checks and falls flat on its face.

   while ((DUTComPort.BytesToRead > 0) && (gotresponse == false))
                {
                    boolFalseTrip = false;
                    NoDataAtPort.Start();
                    Data_From_DUT = DUTComPort.ReadExisting();
                    if (Data_From_DUT.Contains('\r'))
                    {
                        gotresponse = true;
                    }
                    else if (!Data_From_DUT.Contains('\r'))
                    {
                        gotresponse = false;
                    }
                    rtbDUTIncoming.Text += Data_From_DUT;
                }

Seems to get it self in a knot??

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Data recieved event triggering with no data??
« Reply #5 on: November 08, 2010, 09:43:49 am »
If you are saying that even though the received data doesn't contain \r, gotresponse is set to true, put a breakpoint there and see what the data actually is.

Jan

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Data recieved event triggering with no data??
« Reply #6 on: November 08, 2010, 11:41:29 am »
Hmm???  It appears the devices I'm talking to are  possibly crashing/ignoring me (I say crashing as the current draw goes up...) :o

Glenn