我正在尝试使用C#在MSVS上创建串行通信工具。它与Photon MCU和蓝牙适配器进行通信。MSVS C#SerialPort接收到数据丢失
当按下“开始”按钮时,UI向Photon发送一个“1”,它首先发送当前时间戳并开始从函数发生器发送流数据。当按下“停止”按钮时,它首先发送光子收到时10“2”(由于光子端的定时器问题),它停止发送函数发生器的数据。然后它休眠一秒钟并发送一个“3”,它发送另一个当前时间戳。然后用户界面放弃InBuffer中的数据并停止读取数据。
connectBT
与启动按钮连接,并且disconnectBT
与停止按钮连接。
这是代码,我现在所拥有的:
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);
}
我遇到了一个问题,当我按下“停止”按钮,这是从蓝牙发送的数据的最后一块是丢失。当按下停止按钮时,我从来没有收到我应该收到的最后一次印章。根据我们的数学,我们应该得到每秒500点(500Hz),但我只收到约100个。
我的理论是,用户界面以较慢(或延迟)的速率接收数据,即使在数据可以打印到调试输出之前,它也会丢弃接收到的数据。我知道,由于与数据包关联的计数器值,我收到的第一个和最后一个数据之间的所有数据全部存在。基本上如果我得到1〜500个数据点,我只能得到1〜100个数据点。我也尝试过,只要发送1,2和3的白蚁,因为UI应该是这样的,我可以根据需要获取所有数据。我没有故意关闭BT。
我该怎么做才能防止这种数据丢失?我在代码中做错了什么,我不应该为正确的蓝牙协议做或做什么?这是我第一次写蓝牙代码,所以我很不熟悉它。
如果不是全部调用睡眠模式,而是调用睡眠模式(50次),而不是循环调用20次,会有帮助吗?我想可能接收事件想要触发,但不能,因为你已经冻结了主线程。 –
您接收数据的方式可能非常错误。首先 - 你假设只有一行要阅读。不要尝试使用ReadLine并使用ReadExisting。其次 - 你只是把你读到的任何东西放到一个字符串中,每当一个新的事件被触发时就会被覆盖。其中一些字符串可能(几乎可以肯定)不能长时间处理它们。 –