PORTS Forum
Ports and Interfaces => Serial Ports => Topic started by: GlennP 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)
-
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
-
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
-
ReadExisting will return instead of hanging if there is no data, which should help.
Jan
-
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??
-
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
-
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