I'm still beginner and trying to count incoming pulses (square wave) via pin 10 (S6) of parallel port. Frequency input is 1Hz-100Hz. No pulses are when the logic is HI, and I start counting as soon as its logic is LOW.
I did something like this:
int y = 126; // HI logic
do
{
y = PortAccess.Input(889);
textBox1.Text = Convert.ToString(y);
textBox1.Refresh();
} while ((y == 126) || (y == 62) ); //126 is HI level, 62 is LOW
and i detect and count the changes of HI and LOW in a textbox:
private void textBox1_TextChanged(object sender, EventArgs e)
{
count = count + 1;
textBox3.Text = Convert.ToString(count);
textBox3.Refresh();
}
with this code i count the pulses twice the input frequency
however the wait loop (do while) will make the application frozen if I switch to another application.
Any suggestion here? I'm trying to do a multithreading but cant figure out how and whether it's the correct solution.
I also tried to sample the incoming signal (square wave) with a timer (1ms -30ms interval), but it cant count correctly (count too slow most of the time). Am I coding it correctly?
private void timer1_Tick(object sender, EventArgs e)
{
y = PortAccess.Input(889);
if (y == 126)
{
textBox1.Text = Convert.ToString(y);
}
else if (y == 62)
{
textBox1.Text = Convert.ToString(y);
Can someone help me with better algorithm?
thx a lot
Loading
rayPosted Jan 24, 2008, 7:42 PM
the idea as to why i need to update the textbox in real time is because i want to display something like a distance counter or gas pump counter :)
Mike GoldPosted Jan 24, 2008, 12:11 AM
since it is difficult for the eye to see anyway at this frequency, you can certainly do the pulse counting in a separate thread and throttle the display once a second(using a timer event handler), update the textbox with the latest count.
Thread _oThread;
MyForm()
{
_oThread= new Thread(new ThreadStart(CountPulses));
_oThread.Start();
}
int _pulseCount = 0;
void CountPulses()
{
while () {
_pulseCount =
}
}
// timer event handler
timer_tick
{
textBox1.Text = _pulseCount.ToString();
}
}
rayPosted Jan 23, 2008, 6:39 PM
private void Form1_Idle(object sender, System.EventArgs e)
{
y = PortAccess.Input(889);
textBox1.Text = Convert.ToString(y);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "62")
{
count = count + 1;
}
textBox3.Text = Convert.ToString(count);
textBox3.Refresh();
}
private void btnStart_Click(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Form1_Idle);
}// end method
i wonder why (do while) loop makes the program not responsive.
Mike HankeyPosted Jan 22, 2008, 8:02 AM
This handler will be called when your CPU is idling so that it other processes will not be affected.
Secondly when you set the text in a TextBox you do not need to refresh it as you are doing!
Good luck, hope this helps!
Mike