0

我可以在 Visual Studio 中使用 Measurement Studio C# 从 USB 端口(来自数据采集系统)获取数字信号并绘制它,我该怎么做?我只是试图用它来绘制噪声信号,但我不能用它来从端口获取信号。

         // Declare and initialize an instance of WhiteNoiseSignal.
        WhiteNoiseSignal whiteNoise = new WhiteNoiseSignal();
        // Store the generated data in a double array named data.
        double[] data = whiteNoise.Generate(1000.0, 256);
        // Use the PlotY method to plot the data.
        plot.PlotY(data);

这是我使用的代码。

4

1 回答 1

0

Liza,根据您需要配置数字或模拟输入通道的设备。然后,您创建一个任务来监视您感兴趣的端口,并等待采集完成后回调。像这样的东西:

public void CreateAnalogInputTask()
{
try
{
    // create a new task
    myAITask = new Task();

    // create a new virtual channel
    myAITask.AIChannels.CreateVoltageChannel("Dev1/ai0, "", AITerminalConfiguration.Differential, -10, 10, AIVoltageUnits.Volts);

    // configure the timing
    myAITask.Timing.ConfigureSampleClock("",
        AISampleRate,
        SampleClockActiveEdge.Rising,
        SampleQuantityMode.ContinuousSamples,
        numberOfAISamples);
    myAITask.Stream.Buffer.InputBufferSize = 10000000;

    // verify the task
    myAITask.Control(TaskAction.Verify);

    // create the analogReader object
    analogInReader = new AnalogMultiChannelReader(myAITask.Stream);
    analogInReader.SynchronizeCallbacks = false;
    arAnalogInReader = analogInReader.BeginReadWaveform(numberOfAISamples, AnalogInputRead, myAITask);
}
catch (Exception ex)
{
    log.Error("", ex);

    if (myAITask != null)
        myAITask.Dispose();
}
}

然后在调用此回调时读取数据:

public void AnalogInputRead(IAsyncResult ar)
{

    try
    {
        try
        {
            myAIdata = analogInReader.EndReadWaveform(ar);
        }
        catch (DaqException ex)
        {
            if (ex.Error != 88710)
                throw;
            else
                log.Debug("DaqException 88710 ignored", ex);
        }

        for (int i = 0; i < numberOfAISamples; i++)
        {           
            myData[i] = myAIdata[0].Samples[i].Value;
        }
    }
    catch (Exception ex)
    {
        log.Error("", ex);
    }
}
于 2016-02-09T09:20:54.590 回答