List<byte> sp_buffer = new List<byte>(4096); //串口缓存区 int sp_buffer_max = 4096; //串口缓存区最大缓存字节数 private void SerialDataReceive(object sender, SerialDataReceivedEventArgs e) //接收串口数据 { if (serialPort1.IsOpen == false) { serialPort1.Close(); return; } int Byte_len = serialPort1.BytesToRead; //读取缓存的数据长度 byte[] Rc_byte = new byte[Byte_len]; //定义一个字节数组 serialPort1.Read(Rc_byte, 0, Byte_len); //将缓存数据存储进字节数组里面 if (sp_buffer.Count > sp_buffer_max) //缓存超过字节数 先丢弃前面的字节 sp_buffer.RemoveRange(0, sp_buffer_max); //丢弃前面的字节0到sp_buffer_max,存入后进的数据 //?????????????????? sp_buffer.AddRange(Rc_byte); //存入缓存区 byte[] ruffer = new byte[9192]; //用来存放缓冲区的数据流 //??????? //对数据流进行筛选,缓冲区每一组数据个数大于4则为我们想要的数据流 if (sp_buffer.Count > 4) { sp_buffer.CopyTo(0, ruffer, 0, sp_buffer.Count); Task.Run(() => printf_data(ruffer, sp_buffer.Count, 1)); //打印数据流 } }
时间: 2024-02-14 11:35:37 浏览: 59
这段代码实现了串口数据的接收和缓存,并对数据进行筛选后打印出来。
首先,定义了一个名为 sp_buffer 的 List<byte> 类型的缓存区,用于存储串口接收到的数据。同时,定义了一个 sp_buffer_max 变量,表示缓存区的最大缓存字节数为 4096。
接着,在 SerialDataReceive() 方法中,先判断串口是否打开,若未打开则关闭串口并返回。接着,利用 serialPort1.BytesToRead 属性获取串口缓存的数据长度 Byte_len,再定义一个字节数组 Rc_byte,用于存储缓存数据。
然后,利用 serialPort1.Read() 方法将缓存数据存储进字节数组 Rc_byte 里面。接下来,判断缓存区 sp_buffer 的字节数是否超过了设定的 sp_buffer_max,若超过则先丢弃前面的字节0到sp_buffer_max,再存入后进的数据。最后,利用 sp_buffer.AddRange() 方法将缓存数据存入缓存区 sp_buffer。
最后,定义了一个名为 ruffer 的字节数组,用于存放缓冲区的数据流。利用 sp_buffer.Count 属性获取缓存区中数据的个数,若大于4,则将 sp_buffer 中的数据复制到 ruffer 数组中,并利用 Task.Run() 方法打印数据流。
相关问题
如何判断 List<List<int>> list_two和 List<List<int>> list_one元素相同
要判断两个 `List<List<int>>` 类型的列表 `list_one` 和 `list_two` 是否具有相同的元素,你可以使用 `SequenceEqual` 方法。首先,你需要引入 `System.Linq` 命名空间,然后使用 `SequenceEqual` 方法进行比较。
以下是一个示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<List<int>> list_one = new List<List<int>>();
List<List<int>> list_two = new List<List<int>>();
// 为 list_one 添加元素
list_one.Add(new List<int> { 1, 2, 3 });
list_one.Add(new List<int> { 4,5 });
list_one.Add(new List<int> { 6 });
// 为 list_two 添加元素,和 list_one 相同
list_two.Add(new List<int> { 1, 2, 3 });
list_two.Add(new List<int> { 4, 5 });
list_two.Add(new List<int> { 6 });
bool areEqual = list_one.SequenceEqual(list_two); Console.WriteLine("List one and List two are equal: " + areEqual);
}
}
```
运行上述代码,你将会看到输出为:"List one and List two are equal: True",表示两个列表的元素相同。
注意,`SequenceEqual` 方法会逐个比较两个列表中的元素,因此要求元素的顺序也要相同。如果你想要比较无序的列表,你可以使用其他方法进行排序或转换后再进行比较。
C#如何判断 List<List<int>> list_two和 List<List<int>> list_one元素相同
要判断两个 `List<List<int>>` 类型的列表 `list_one` 和 `list_two` 是否具有相同的元素,你可以使用 `SequenceEqual` 方法。首先,你需要引入 `System.Linq` 命名空间,然后使用 `SequenceEqual` 方法进行比较。
以下是一个示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<List<int>> list_one = new List<List<int>>();
List<List<int>> list_two = new List<List<int>>();
// 为 list_one 添加元素
list_one.Add(new List<int> { 1, 2, 3 });
list_one.Add(new List<int> { 4,5 });
list_one.Add(new List<int> { 6 });
// 为 list_two 添加元素,和 list_one 相同
list_two.Add(new List<int> { 1, 2, 3 });
list_two.Add(new List<int> { 4, 5 });
list_two.Add(new List<int> { 6 });
bool areEqual = list_one.SequenceEqual(list_two); Console.WriteLine("List one and List two are equal: " + areEqual);
}
}
```
运行上述代码,你将会看到输出为:"List one and List two are equal: True",表示两个列表的元素相同。
注意,`SequenceEqual` 方法会逐个比较两个列表中的元素,因此要求元素的顺序也要相同。如果你想要比较无序的列表,你可以使用其他方法进行排序或转换后再进行比较。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)