Hi All,
I am trying to perfect a bit of test code for a device we have updated (amongst other thing replaced a 9 Way with USB) I want to a method of waiting for a command to come back,the routine waits while there are no bytes in the buffer and the time is less the value passed to it. NbDataToWait is a bit vague I would prefer to wait for '>' come back but can't seem to think of a way to do it quickly!
public int WaitForData(int NbDataToWait, int Timeout)
{
int timeWaited = 0;
int timeToWait = 1; // 1 ms
// && there is no > in reply
while (timeWaited < Timeout && myComPort.BytesToRead < NbDataToWait)// && myComPort.BytesToRead(">"))
{
timeWaited += timeToWait;
lblwhere.Text += ".";
System.Threading.Thread.Sleep(timeToWait );
}
return timeWaited;
}
/* public int WaitForDataOK(int NbDataToWait, int Timeout)
{
int timeWaited = 0;
int timeToWait = 1; // 1 ms
// && there is no > in reply
while (timeWaited < Timeout && myComPort.BytesToRead > 0)
{
timeWaited += timeToWait;
lblwhere.Text += ".";
System.Threading.Thread.Sleep(timeToWait);
}
return timeWaited;
}
*/
UPDATE:: I believe I need to wait for a >
Loading
Sam HobbsPosted Feb 18, 2012, 12:33 AM
Glenn PattonPosted Feb 17, 2012, 9:00 AM
Sam HobbsPosted Feb 17, 2012, 8:51 AM
There is a forum in the MSDN forums that is for asking where to ask questions. You could try asking there where is a good place to ask your questions.
I tried a search. There appears to be possibilities there.
Serial communications is older technology and newsgroups are also older technology. There is certainly many answers in the newsgroups and Google Groups still has newsgroups in it. So if this link works then it will help you find newsgroups.
Glenn PattonPosted Feb 17, 2012, 8:29 AM
I really need a Serial Class specialty area for these questions, for the hardware side www.lvr.com is good, MSDN are quite good (if at times not quite right) can you suggest a board that is better for Serial Comms stuff that is actually in use.
Most of my questions do tend to be on timing and interrupts and the related.
Glenn
Sam HobbsPosted Feb 15, 2012, 12:34 PM
Certainly it would seem that the UI is not relevant, but unfortunately there are more non-UI things that depend on the UI than probably should. If you are asking how to interface with the UI then that would depend on the UI.
Glenn PattonPosted Feb 15, 2012, 12:06 PM
Glenn PattonPosted Feb 15, 2012, 12:05 PM
Glenn PattonPosted Feb 15, 2012, 12:05 PM
Glenn PattonPosted Feb 15, 2012, 12:04 PM
Sam HobbsPosted Feb 15, 2012, 11:53 AM
Actually, WaitForSingleObject has a timeout. That should be very easy to use. Your problem depends o using asynchronous reads which can waited on using WaitForSingleObject with a timeout. That would work, correct? It sounds like an easy and effective solution.
Glenn PattonPosted Feb 15, 2012, 11:41 AM
Glenn (thanks)
VulpesPosted Feb 15, 2012, 11:28 AM
However, if you're reading the bytes elsewhere and placing them in textBox1, then you could stop looping when ">" has been read if that's sooner than the other two conditions, as follows:
Glenn PattonPosted Feb 15, 2012, 11:15 AM
VulpesPosted Feb 15, 2012, 10:52 AM
I assume the bytes in the buffer are being read elsewhere. To be able to tell whether a ">" has been read or not, you need to access the buffer where you're storing those bytes. The following ignores this for now and just waits until all the bytes have been read or the timeout has occurred:
using System.Diagnostics;
// .....