简体   繁体   English

从串口读取实时数据

[英]Reading Real time data from serial port

I am trying to read real time data from an accelerometer. 我正在尝试从加速度计读取实时数据。 Sampling frequency of the accelerometer is 2650Hz. 加速度计的采样频率为2650Hz。 I am getting proper data from serial port, but unable to match with the sampling frequency. 我从串行端口获取了正确的数据,但是无法与采样频率匹配。 The sampling frequency is varying from 2100Hz to 2400Hz and it is not stable. 采样频率在2100Hz至2400Hz之间变化,并且不稳定。 I am using a stop watch for timing reference. 我使用秒表作为时间参考。

Here is my code for receiving serial data. 这是我用于接收串行数据的代码。

private void toolStripButton12_Click(object sender, EventArgs e)
{
    serialPort1.PortName = comboBox1.Text;
    serialPort1.BaudRate = Convert.ToInt32(115200);

    if (!serialPort1.IsOpen)
    {
        serialPort1.Open();
    }                    
    sw.Start();
    serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived);
}

}


private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    {
        byteCount = serialPort1.BytesToRead;
        if (byteCount > 4000)
        byteCount = 4000;

        if (e.EventType == SerialData.Eof)
        return;
        byte[] buffer = new byte[byteCount];

        int readBytes = serialPort1.Read(buffer, 0, buffer.Length);

        // FIFO Implementation

        buffer.ToList().ForEach(b => newrecievedData1.Enqueue(b));
        if (newrecievedData1.Count < 4000) return;
        processdata3();

        int i = 0;

        {
            while (i <= packet3.Length-4)
            {
                while (packet3[i++] != 69) ;

                data = packet3[i++];
                a = data << 8;
                b = a + packet3[i++];
                c = b << 8;
                d = c + packet3[i++];
                Port1data.Add(d);


                countbyte[0]++;

                tick = (double)countbyte[0];
            }
        }

        t = (double)sw.Elapsed.TotalSeconds;             
        Sampling frequency = tick / t;
    }

    try
    {
        this.Invoke(new EventHandler(DisplayText));
    }
    catch
    {
    }
}

Int32[] packet3;

private Int32[] processdata3()
{
    if (newrecievedData1.Count >= 4000)
    {
        packet3 = Enumerable.Range(0, 4000).Select(h => newrecievedData1.Dequeue()).ToArray();
    }
    return packet3;
}

I want to exactly get 2650 Hz sampling frequency all the time.Any help is highly appreciated. 我想一直准确地获得2650 Hz的采样频率,我们将不胜感激。

That is 0.337 ms per sample. 即每个样本0.337毫秒。 That is really pushing the upper limits of how much code can be done per sample. 这确实提高了每个样本可以完成多少代码的上限。

Without some major optimization to your algorithms (possibly using custom collections designed specifically for your workload) I don't think your requirements are reachable using managed code. 如果没有对算法进行一些重大优化(可能使用专门为您的工作负载设计的自定义集合),我认为使用托管代码无法满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM