缓冲区buffer,字节数组 bytep[] arraysample与内存流memory stream

本文介绍了.NET中的缓冲区概念,它涉及到内存的直接操作,通常表现为字节数组。字节数组是二进制数据的底层表示,适用于处理数据文件、图像文件等。MemoryStream类提供了以内存为存储的流,允许用字节数组进行数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 buffer缓冲区

The word Buffer itself says that it works on direct Memory. In .NET, it is basically a manipulation of unmanaged memory represented as arrays of bytes.

要点1: 缓冲区是内存的一部分

要点2: 在程序中可以表示为字节数组。

 

2字节数组 byte[ ]  arraySample

You are dealing with binary data that is organized into a series of bytes in your C# program, which may be part of a data file, image file, compressed file, downloaded server response, or many other files. The C# language provides a byte array type that is an ideal representation of this data in terms of its accurate representation in memory. Here we examine the byte array type in the C# language.

要点1: 低层的数据是二进制数据,如0101001。

要点2: byte[]字节数组可以让程序直接来操作二进制数据。

要点3: 这些二进制数据可以表示很多东西,如数据文件,图像文件,压缩文件等。

 

3 memory stream

The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty.

要点1: 这些数据流是在内存中储存的数据流。

要点2: 用字节数组byte[]来表示

 

(参考资料

http://dotnetperls.com/byte-array

MSDN)

 

在STM32F4中,可以使用DMA和中断来实现串口环形缓冲区接收多字节数据。 一种常见的方法是使用DMA,在接收到数据时触发DMA传输,将数据存储到环形缓冲区中。具体步骤如下: 1. 初始化串口和DMA,将DMA模式设置为循环模式。 2. 设置DMA的目的地址为环形缓冲区的当前位置,设置传输大小为1字节。 3. 开启串口接收中断,并在中断服务函数中触发DMA传输。 4. 在DMA传输完成中断中更新环形缓冲区指针,并重新配置DMA传输。 以下是一个示例代码,仅供参考: ```c #define BUFFER_SIZE 64 uint8_t buffer[BUFFER_SIZE]; volatile uint8_t head = 0; volatile uint8_t tail = 0; void USART2_IRQHandler(void) { if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { // 触发DMA传输 DMA_Cmd(DMA1_Stream5, ENABLE); } } void DMA1_Stream5_IRQHandler(void) { if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5) != RESET) { // 更新缓冲区指针 head = (head + 1) % BUFFER_SIZE; // 重新配置DMA传输 DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5); DMA_Cmd(DMA1_Stream5, DISABLE); DMA_SetCurrDataCounter(DMA1_Stream5, 1); DMA_SetMemory0Address(DMA1_Stream5, (uint32_t)&buffer[head]); DMA_Cmd(DMA1_Stream5, ENABLE); } } int main(void) { // 初始化串口和DMA // 配置DMA DMA_DeInit(DMA1_Stream5); DMA_InitTypeDef DMA_InitStruct; DMA_StructInit(&DMA_InitStruct); DMA_InitStruct.DMA_Channel = DMA_Channel_4; DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&buffer[head]; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStruct.DMA_BufferSize = 1; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStruct); // 开启串口接收中断 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 启动DMA传输 DMA_Cmd(DMA1_Stream5, ENABLE); while (1) { // 处理接收到的数据 if (head != tail) { // 读取缓冲区中的数据 uint8_t data = buffer[tail]; // 更新缓冲区指针 tail = (tail + 1) % BUFFER_SIZE; // 处理数据 // ... } } } ``` 在上面的代码中,head和tail分别表示环形缓冲区的头部和尾部。当接收到数据时,触发DMA传输将数据存储到缓冲区的当前位置。当DMA传输完成时,更新头部指针,并重新配置DMA传输。在主循环中,读取缓冲区中的数据并处理。由于环形缓冲区是循环的,当头部指针超过缓冲区末尾时,会自动返回到缓冲区开头。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值