简体   繁体   English

C#Visual Studio每毫秒通过串行端口读取一个数据包

[英]C# visual studio reading a data pack every ms with the serial port

I have a window form application that needs to read in a byte array once every ms. 我有一个窗口窗体应用程序,需要每毫秒一次读取一个字节数组。 The byte array varies in size, but will never be longer than 36 bytes. 字节数组的大小不同,但永远不会超过36个字节。 The code I currently have seems to omit some of the data packages. 我目前拥有的代码似乎省略了一些数据包。 One of the bytes in the package is a count, so I can tell some of the packages are being missed. 包中的一个字节是一个计数,因此我可以告诉您某些包丢失了。 After serialPort1.Read(data, 0, bytesToRead) the rest of the code is just to process the byte array into an int array and add it to a list. 在serialPort1.Read(data,0,bytesToRead)之后,剩下的代码只是将字节数组处理为int数组并将其添加到列表中。 That code all works like I expect it to. 该代码的所有工作都与我期望的一样。

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

       int bytesToRead = serialPort1.BytesToRead; 
       byte[] data = new byte[bytesToRead];
       serialPort1.Read(data, 0, bytesToRead); 

       data = replaceBytes(data);
       long[] dataIntigerValuesPlusCountValue = convertDataToInt(data);
       List<long> dataIntigerValuePlusCountValueList = dataIntigerValuePlusCountValue.OfType<long>().ToList();
       dataList.Add(dataIntigerValuePlusCountValueList);

}

I have a window form application that needs to read in a byte array once every ms. 我有一个窗口窗体应用程序,需要每毫秒一次读取一个字节数组。

First off, you shouldn't have to read 'every ms', you'll lock up the UI if you're in the UI thread (which you are since you're not using threading). 首先,您不必阅读“每毫秒”,如果您处于UI线程中(因为您未使用线程),就可以锁定UI。

The byte array varies in size, but will never be longer than 36 bytes. 字节数组的大小不同,但永远不会超过36个字节。

Instead of trying to read a single packet every MS, you should buffer data, 36 bytes at a time, and then process it. 与其尝试每个MS读取一个数据包,不如一次缓冲36个字节的数据,然后对其进行处理。 Eg Ready 36 bytes, find the byte which tells you how long the packet should be, then process that many bytes. 例如,准备好36个字节,找到一个告诉您数据包应该多长时间的字节,然后处理那么多字节。 On your next serial port read, concatenate the array of any unused bytes with the next 36 bytes you read (your buffer array size should be at least 72 bytes, and on each loop, you should check if the current size is greater than 36, and if it is, loop an additional time so that at a given time, you have at most 1 full packet in the byte array buffer). 在您下次读取串行端口时,请将所有未使用的字节数组与接下来读取的36个字节连接起来(您的缓冲区数组大小应至少为72个字节,并且在每个循环中,您应检查当前大小是否大于36,如果是,则循环一个额外的时间,以便在给定的时间,字节数组缓冲区中最多有1个完整的数据包)。

Also, instead of blocking the UI thread, and instead of utilizing threads, you should use async/await continuously read read data and then process your data that way. 另外,应该使用async / await连续读取读取的数据,然后以这种方式处理数据,而不是阻塞UI线程,而不是利用线程。 How to continually read data asynchronously . 如何连续不断地异步读取数据

That way you would get the packet size, then wait for the buffer to be the appropriate size, then you process the data. 这样,您将获得数据包的大小,然后等待缓冲区为适当的大小,然后处理数据。

The way you're currently doing it will drop the extra data buffered and desync your connection. 您当前的操作方式将删除缓冲区中多余的数据并取消连接的同步。 That said, you should also try and use a flag byte (or a newline character) to signify the start or end of packet. 也就是说,您还应该尝试使用标志字节(或换行符)来表示数据包的开始或结束。 Obviously if it's not your hardware you can't control that, but if you can, that would guarantee the stream never desyncs. 显然,如果不是您的硬件,您将无法控制它,但是如果可以,那将保证流永远不会脱离同步。

TL:DR: Timing is unreliable, just process the packets as they are received. TL:DR:时间不可靠,只在接收到数据包时对其进行处理。

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

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