C#如何将字节写入byte []数组的中间

时间:2019-11-12 17:12:21

标签: c# .net

下面的函数ReadPipe()读取字节的块,我需要每个块转到byte [] packet_buffer中的下一个位置。但我不知道如何告诉.ReadPipe将字节写入packet_buffer内。

如果是C,我可以指定:* packet_buffer [下一个块的字节索引]

如何在C#中执行此操作?

public static int receive_SetStreamPipe_2( byte[] packet_buffer,  int bytes_to_read )
    {
        uint received_chunk_bytes = 0;
        int remaining_bytes = bytes_to_read;
        int total_transferred_bytes = 0;


        // Use DataPipeInformation to get the actual PipeID             
        ftStatus = USB_device_selection0.SetStreamPipe( FT_pipe_information.PipeId, (UInt32)bytes_to_read );             
        if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                    return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS



        // For each chunk 'o bytes:
        for(;;)
        {
            // Read chunk of bytes from FPGA:
                ftStatus = USB_device_selection0.ReadPipe( FT_pipe_information.PipeId, 
                                                            packet_buffer( remaining_bytes )  , <<<<<<<<<<<<<<  THIS WON'T WORK
                                                            (uint)remaining_bytes,
                                                            ref received_chunk_bytes );
            if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                        return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS

            total_transferred_bytes +=  (int)received_chunk_bytes;
            remaining_bytes -=  (int)received_chunk_bytes;


            // Get more if not done:
            if( total_transferred_bytes <  bytes_to_read )
            {
                continue;  // go get more
            }

            return 0;
        }
    }

2 个答案:

答案 0 :(得分:0)

根据CodeCaster的回答,到目前为止最好的答案是我已要求FTDI公司让USB主机驱动程序提供带偏移量的过载。

答案 1 :(得分:-2)

对您的代码进行以下假设,我确实不必这样做,请阅读[问]并提供所有相关详细信息:

  • receive_SetStreamPipe_2(byte[] packet_buffer, int bytes_to_read)
    • 由您实施
    • packet_buffer中接收长度至少为bytes_to_read且需要精确填充bytes_to_read个字节的数组
  • USB_device_selection0.ReadPipe(FT_pipe_information.PipeId, packet_buffer, (uint)remaining_bytes, ref received_chunk_bytes)
    • 从索引0填充packet_buffer,并且没有带有偏移量的重载(例如Stream.Write(buffer, offset, count)
    • 最多可填充remaining_bytes,但可能更少
    • received_chunk_bytes分配给已读取的字节数

然后,您需要引入一个临时缓冲区,然后将其复制到最终缓冲区。可以从API信息中获得最佳的缓冲区大小,但是我们需要1024个字节:

uint received_chunk_bytes = 0;
int remaining_bytes = bytes_to_read;
int total_transferred_bytes = 0;

// Create a smaller buffer to hold each chunk
int chunkSize = 1024;
byte[] chunkBuffer = new byte[chunkSize];

// ...

for (;;)
{
    // Read chunk of bytes from FPGA into chunkBuffer, chunk size being the the buffer size or the remaining number of bytes, whichever is less
    ftStatus = USB_device_selection0.ReadPipe(FT_pipe_information.PipeId, 
                                              chunkBuffer
                                              (uint)Math.Min(chunkSize, remaining_bytes),
                                              ref received_chunk_bytes);

    if (ftStatus != FTDI.FT_STATUS.FT_OK)                 
                    return -(int)ftStatus;     // lookup:    FTDI.FT_STATUS

    // Copy the chunk into the output array
    Array.Copy(chunkBuffer, 0, packet_buffer, total_transferred_bytes, received_chunk_bytes);

    total_transferred_bytes +=  (int)received_chunk_bytes;
    remaining_bytes -=  (int)received_chunk_bytes;

    // ...