Ports
May 25, 2013, 07:41:22 am
Welcome,
Guest
. Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: SMF - Just Installed!
Home
Help
Search
Login
Register
Welcome to the PORTS Forum. Please post your questions, answers, and comments relating to USB and other hardware interfaces in the appropriate forums below. - Jan
Ports
>
General Category
>
Serial Ports
>
Data recieved event triggering with no data??
Pages: [
1
]
« previous
next »
Print
Author
Topic: Data recieved event triggering with no data?? (Read 2744 times)
GlennP
Frequent Contributor
Posts: 126
Data recieved event triggering with no data??
«
on:
November 01, 2010, 04: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)
Logged
Jan Axelson
Administrator
Frequent Contributor
Posts: 1631
Re: Data recieved event triggering with no data??
«
Reply #1 on:
November 03, 2010, 07: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
Logged
GlennP
Frequent Contributor
Posts: 126
Re: Data recieved event triggering with no data??
«
Reply #2 on:
November 03, 2010, 04: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
Logged
Jan Axelson
Administrator
Frequent Contributor
Posts: 1631
Re: Data recieved event triggering with no data??
«
Reply #3 on:
November 03, 2010, 08:18:36 pm »
ReadExisting will return instead of hanging if there is no data, which should help.
Jan
Logged
GlennP
Frequent Contributor
Posts: 126
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??
Logged
Jan Axelson
Administrator
Frequent Contributor
Posts: 1631
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
Logged
GlennP
Frequent Contributor
Posts: 126
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...)
Glenn
Logged
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
General Category
-----------------------------
=> USB
=> Serial Ports
=> Embedded Ethernet
=> Parallel Port
=> Other Interfaces
=> General Discussion
Loading...