MSVS C#的SerialPort接收数据丢失 [英] MSVS C# SerialPort Received Data Loss

查看:425
本文介绍了MSVS C#的SerialPort接收数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用C#在MSVS串行通信工具。它与光子MCU和蓝牙模块进行通信。

I'm trying to create a Serial Communication tool on MSVS using C#. it communicates with the Photon MCU and a bluetooth dongle.

当的开始按钮是pressed,用户界面​​发送一个1,它首先发送当前时间戳,并开始从该函数发生器的流数据的光子。当停止按钮时pressed,它首先发送102秒(由于对光子结束计时器问题),其中当光子接收,它停止发送函数发生器的数据。然后它睡觉用于第二和发送一个3,它发送另一个当前时间戳。然后,UI在InBuffer丢弃数据并停止读取数据。

When the "start" button is pressed, the UI sends a "1" to the Photon which it first sends the current time stamp and starts streaming data from the function generator. When the "stop" button is pressed, It first sends 10 "2"s (due to the timer issue on the photon's end) which the when the Photon receives, it stops transmitting the function generator's data. Then it sleeps for a second and sends a "3" which it sends another current time stamp. Then the UI discards data in the InBuffer and stops reading data.

connectBT 与启动按钮和 disconnectBT 与停止按钮连接。

connectBT is connected with the start button and the disconnectBT is connected with the stop button.

这是code,我有现在:

This is the code that I have right now:

SerialPort serial = new SerialPort();
string recieved_data;
int startBuffer = 0;

private void connectBT(object sender, RoutedEventArgs e)
{
    startBuffer++; // keep track of BT open counter
    if (serial.IsOpen) Debug.WriteLine("BT Open");

    // first time BT is open and BT is not open
    if (!serial.IsOpen)
    {
        if (startBuffer == 1)
        {
            // COM port properties
            serial.PortName = "COM7";
            serial.BaudRate = 38400;
            serial.Handshake = Handshake.None;
            serial.Parity = Parity.None;
            serial.DataBits = 8;
            serial.StopBits = StopBits.One;
            serial.ReadTimeout = 200;
            serial.WriteTimeout = 50;
            serial.Open();
        }

        startButton.Content = "Recording";
        Send_Data("1"); // tell Photon to start sending data
        serial.DiscardInBuffer(); // discard whatever is in inbuffer
        serial.DataReceived += new SerialDataReceivedEventHandler(Recieve); // start receiving data
    }

    // after BT has been opened and start button has been pressed again
    else if (serial.IsOpen && startBuffer > 1)
    {
        startButton.Content = "Recording";
        Send_Data("1");
        serial.DiscardInBuffer();
        serial.DataReceived += new SerialDataReceivedEventHandler(Recieve);
    }
}

// stop button is pressed
private void disconnectBT(object sender, RoutedEventArgs e)
{

    // send "2" ten times to tell photon to stop transmitting function generator data
    int i = 0;
    while (i < 10)
    {
        Send_Data("2");
        Thread.Sleep(1);
        i++;
    }
    Thread.Sleep(1000);
    Send_Data("3"); // send a 3 to tell photon to send the last time stamp
    Thread.Sleep(1000);

    serial.DiscardInBuffer(); // discard in buffer
    serial.DataReceived -= Recieve; // stop receiving data
    //serial.Close(); // close BT
    startButton.Content = "Start";

}

private void Recieve(object sender, SerialDataReceivedEventArgs e)
{

    recieved_data = serial.ReadLine();
    Debug.WriteLine(recieved_data);

}

我运行到哪里,当我preSS的停止按钮,即从蓝牙发送的数据的最后一块丢失的问题。我从来没有收到我应该当停止按钮pssed $ P $已收到的最后一个时间戳。根据我们的数学,我们应该可以接收每秒(500Hz的)500分,但我只收到其中的约100。

I'm running into an issue where when I press the "stop" button, the last chunk of data that was sent from the bluetooth is lost. I never receive the last time stamp that I'm supposed to have received when the stop button is pressed. According to our math, we're supposed to be receiving 500 points per second (500Hz) but I only receive about 100 of them.

我的理论是,用户界面​​以较慢的接收数据(或延迟)率和 serial.DiscardInBuffer 甚至在此之前的数据可以丢弃收到的数据打印到调试输出。我知道一个事实,即所有的第一个和最后我收到的数据,因为与数据包相关计数器的值都在那里。基本上,如果我收到1〜500个数据点,我只收到1〜100。我也只白蚁试图用它发送1,2和3的用户界面应该是和我得到的所有数据,我需要他们。我不故意关闭BT。

My theory is that the UI is receiving data at a slower (or a delayed) rate and the serial.DiscardInBuffer discard the received data even before that the data can be printed to the Debug output. I know for a fact that all the data between the first and last I receive are all there because of counter values associated with the data packets. Basically if I were to receive 1~500 data points, I only receive 1~100. I've also tried it with just termite with sending 1,2, and 3 as the UI is supposed to be and I get all the data as I need them. I don't close BT on purpose.

我能做些什么,以prevent这个数据丢失?那我在我的code做错了,我不应该做或做正确的蓝牙协议?这是我第一次写的蓝牙code,所以我与它相当陌生。

What can I do to prevent this data loss? What am I doing wrong in my code that I shouldn't be doing or be doing for the correct bluetooth protocol? This is my first time writing bluetooth code so I'm fairly unfamiliar with it.

推荐答案

感谢大家的响应,他们都帮我达到了我的问题的解决方案,但在什么固定它被延迟发送的时间结束了 3和废弃我inBuffer和关闭接收连接。

Thank you all for your response, they all helped me reaching the solution for my issue, but in the end what fixed it was delaying the time between sending the "3" and discarding my inBuffer and closing the Receive connection.

async Task DelayBT()
{
    await Task.Delay(100);
}

Thread.sleep代码()并没有因为它的性质禁用线程中的所有动作(这是我仍然需要)的工作,所以这种方法工作就像一个魅力。我只是叫等待DelayBT 地方,我需要的延迟。

Thread.Sleep() didn't work because of its nature disabling all action within the thread (which I still needed) so this method worked like a charm. I just called await DelayBT where I needed the delay.

希望这有助于任何人运行到相同的问题我。

Hope this helps anyone running into the same issue as me.

这篇关于MSVS C#的SerialPort接收数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆