Ports
May 24, 2013, 01:01:31 pm
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
>
Need a wait for data type pause in C#....
Pages: [
1
]
« previous
next »
Print
Author
Topic: Need a wait for data type pause in C#.... (Read 1175 times)
GlennP
Frequent Contributor
Posts: 126
Need a wait for data type pause in C#....
«
on:
February 15, 2012, 07:47:50 am »
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.
The version in red is the version that is in use now is this recommended as it uses a Thread Sleep and it is important not to lock the machine up
Glenn
public int WaitForData(int NbDataToWait, int Timeout)
{
int timeWaited = 0;
int timeToWait = 1; // 1 ms
// && there is no > in reply
while (timeWaited < Timeout && 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;
}
*/
Logged
Jan Axelson
Administrator
Frequent Contributor
Posts: 1629
Re: Need a wait for data type pause in C#....
«
Reply #1 on:
February 15, 2012, 11:06:58 am »
Thread sleep sleeps the thread. The application can have other threads that are awake.
See my Com Port Terminal example for a way to do it with the DataReceived event:
http://www.lvr.com/serport.htm#my_example_code
Jan
Logged
GlennP
Frequent Contributor
Posts: 126
Re: Need a wait for data type pause in C#....
«
Reply #2 on:
February 17, 2012, 06:23:08 am »
Thanks for that I had solve it sort of.
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...