我想用一个电机带动平台转动,然后平台上有个传感器采集数据,所以建立了2个线程,
一个负责电机的驱动以及传感器的开关,另一个就是专门采集数据的。
可是每次采集到的数据在20000多的时候就采集不了了。
下面附上代码,我确实没想到原因,开始以为是延时的问题,后来在第二个线程开始加了1ms的延时,还是不行。
//定义旋转所需时间
public static double PTime;
//定义旋转所需脉冲、采样到的点数
public int ringpulse, PPoints;
private void motor1()
{
//正向旋转一圈
Motion.mAcm_AxMoveRel(axhand[Axis], Convert.ToDouble(ringpulse));
//传感器开始测量
Cur115.StartMesure();
//休眠等待一圈扫描完成
Thread.Sleep(Convert.ToInt32(PTime * 1000));
//停止测量
Cur115.StopMesure();
//反转回零点
Motion.mAcm_AxMoveRel(axhand[Axis], Convert.ToDouble(ringpulse*(-1)));
}
private void sensor1()
{
//等待motor1执行一段时间
Thread.Sleep(1);
//读取数据
for (int i = 0; i < PPoints; i++)
{
//调用函数读取数据,方式为“取出并移除”
PDistance[i] = Cur115.GetData();
//输出
Console.WriteLine("distance{0}={1}", i, PDistance[i]);
}
}
private void test_Click(object sender, EventArgs e)
{
Cur115.ClearDataQueue();
Thread ThMotor1 = new Thread(new ThreadStart( motor1));
ThMotor1.IsBackground = true;
Thread thsensor = new Thread(new ThreadStart(sensor1));
thsensor.IsBackground = true;
//这个只是计算PPoints和ringpulse的函数,没其他作用。
CalforPM();
ThMotor1.Start();
thsensor.Start();
StepComplete_Bit = true;
}