要求一直同步而且稳定读取外部设备的频率..另外有个form记录这个频率..我的朋友说只用do while...可以准确读取,但是form会没响应. 后来我用了thread 加do while可以读取...但是当高频的时候会慢....还有就是把程序最小化后...又会比实际的快....这个问题怎么解决????有谁做过类似的程序??可以请教下吗???只用do while读取的程序
public partial class Form1 : Form
{
float count = 0;
public Form1() {
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{
PortAccess.Output(888,255);
}
private void button1_Click(object sender, EventArgs e)
{
PortAccess.Output(888, 0); //textBox2.Text = Convert.ToString (PortAccess.Output(888,0);
} private void button3_Click(object sender, EventArgs e)
{
int y = 126;
do
{
y = PortAccess.Input(889);
textBox1.Text = Convert.ToString(y);
textBox1.Refresh();
} while (y == 120 || y == 56);
}
}
}
thread + do while public partial class DistancePulse : UserControl
{
double i = 0;
string direction = "1";
public double stopechain = 0;
int y;
Thread pulsesThread;
public DistancePulse()
{
InitializeComponent();
} Boolean count120 = false; private void countPulses()
{
do
{
y = PortAccess.Input(889); switch (y)
{ case 120: count120 = true; break;
} if (y == 56 && count120 == true )
{
switch (direction)
{ case "1": count120 = false; i++; break;
case "2": count120 = false; i--; break; }
} } while ((y == 80) || (y == 16) || (y == 48) || (y == 24) || (y == 112) || (y == 88) || (y == 120) || (y == 56));
}// end method
private void timer1_Tick(object sender, EventArgs e)
{
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
if (i >= 0)
{
string s = i + "";
s = s.PadLeft(7, '0');
string s1 = s.Substring(1, 3);
string s2 = s.Substring(4);
string s3 = s1 + "." + s2; lbl_display.Text = s3;
}
else {
double itemp = Math.Abs(i); string s = itemp + "";
s = s.PadLeft(7, '0');
string s1 = s.Substring(1, 3);
string s2 = s.Substring(4);
string s3 = "-"+s1 + "." + s2; lbl_display.Text = s3;
} } private void btn_play_Click(object sender, EventArgs e)
{
pulsesThread = new Thread(new ThreadStart(countPulses));
pulsesThread.Start();
timer1.Enabled = true;
}
private void DistancePulse_Load(object sender, EventArgs e)
{
PortAccess.Output(888, 0);
}
}//End Class
}thread + do whiel 测试结果2Hz ------> 20
3Hz 30
4Hz 40
5Hz 50
6Hz 56
7Hz 57
8Hz 63
9Hz 55
10Hz 7012Hz 75
15Hz 86
20Hz 122from here it seems that the program can catch up if the frequency is below 5Hzhowever i notice this:
it will run faster if we leave the program on the backgroundfor example:
The video counter is 6.459 and the current counter reading is 6.390
(obviously the counter is running too slow)and i browse the internet for say... 2 minutes (and keep the counter running on the background), then the video counter has reached 6.900, but your counter is counting 7.030, which is becoming too fast. If I leave the counter on the background for longer time (10 minutes) then the differences between the real video counter and your counter getting bigger (e.g. video counter 7.900 and your counter 8.600)
public partial class Form1 : Form
{
float count = 0;
public Form1() {
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{
PortAccess.Output(888,255);
}
private void button1_Click(object sender, EventArgs e)
{
PortAccess.Output(888, 0); //textBox2.Text = Convert.ToString (PortAccess.Output(888,0);
} private void button3_Click(object sender, EventArgs e)
{
int y = 126;
do
{
y = PortAccess.Input(889);
textBox1.Text = Convert.ToString(y);
textBox1.Refresh();
} while (y == 120 || y == 56);
}
}
}
thread + do while public partial class DistancePulse : UserControl
{
double i = 0;
string direction = "1";
public double stopechain = 0;
int y;
Thread pulsesThread;
public DistancePulse()
{
InitializeComponent();
} Boolean count120 = false; private void countPulses()
{
do
{
y = PortAccess.Input(889); switch (y)
{ case 120: count120 = true; break;
} if (y == 56 && count120 == true )
{
switch (direction)
{ case "1": count120 = false; i++; break;
case "2": count120 = false; i--; break; }
} } while ((y == 80) || (y == 16) || (y == 48) || (y == 24) || (y == 112) || (y == 88) || (y == 120) || (y == 56));
}// end method
private void timer1_Tick(object sender, EventArgs e)
{
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
if (i >= 0)
{
string s = i + "";
s = s.PadLeft(7, '0');
string s1 = s.Substring(1, 3);
string s2 = s.Substring(4);
string s3 = s1 + "." + s2; lbl_display.Text = s3;
}
else {
double itemp = Math.Abs(i); string s = itemp + "";
s = s.PadLeft(7, '0');
string s1 = s.Substring(1, 3);
string s2 = s.Substring(4);
string s3 = "-"+s1 + "." + s2; lbl_display.Text = s3;
} } private void btn_play_Click(object sender, EventArgs e)
{
pulsesThread = new Thread(new ThreadStart(countPulses));
pulsesThread.Start();
timer1.Enabled = true;
}
private void DistancePulse_Load(object sender, EventArgs e)
{
PortAccess.Output(888, 0);
}
}//End Class
}thread + do whiel 测试结果2Hz ------> 20
3Hz 30
4Hz 40
5Hz 50
6Hz 56
7Hz 57
8Hz 63
9Hz 55
10Hz 7012Hz 75
15Hz 86
20Hz 122from here it seems that the program can catch up if the frequency is below 5Hzhowever i notice this:
it will run faster if we leave the program on the backgroundfor example:
The video counter is 6.459 and the current counter reading is 6.390
(obviously the counter is running too slow)and i browse the internet for say... 2 minutes (and keep the counter running on the background), then the video counter has reached 6.900, but your counter is counting 7.030, which is becoming too fast. If I leave the counter on the background for longer time (10 minutes) then the differences between the real video counter and your counter getting bigger (e.g. video counter 7.900 and your counter 8.600)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货