文件的字节数超过预期...从 FTDI 读取数据并写入 .bin 文件

File has more bytes than expected...data read from FTDI and written into .bin file

我正在尝试使用 Visual C++ CLR 从 FTDI FT2232 设备以 windows 形式获取图像数据 (jpeg)。 这整个操作发生在按钮点击上。我在读取操作本身中将接收到的数据写入 .bin 文件。我收到的字节数约为 19k,文件有 57k 字节

我不允许上传图片所以我分享代码并拧干我得到的输出

    FT_HANDLE ftHandle;

    FT_STATUS ftStatus;
    DWORD BytesWritten;     //DWORD is 32 bit unsigned
    DWORD RxQueueBytes=0;
    DWORD TxQueueBytes;
    DWORD buff = 0;
    char TxBuffer[10];      // Contains data to write to device
    char RxBuffer[16000];  // Contains data recieved from the FTDI devide
    DWORD BytesReceived;
    DWORD RxBytes = 0;
    DWORD TxBytes = 0;
    DWORD EventDWord;


    ftStatus = FT_Open(0, &ftHandle);
    if (ftStatus != FT_OK) {
        // FT_Open failed

        return;
    }
    TxBuffer[TxBytes++] = 0x30;
    TxBuffer[TxBytes++] = 0x2;
    TxBuffer[TxBytes++] = 0x0;
    TxBuffer[TxBytes++] = 0x42;
    TxBuffer[TxBytes++] = 0x01;

    ftStatus = FT_Write(ftHandle, TxBuffer, TxBytes, &TxBytes);   // Data send command
            if (ftStatus == FT_OK) 
            {
                printf(" FT_Write is Ok \n");
                Sleep(5000);
            }


    FT_GetQueueStatus(ftHandle, &RxQueueBytes);         // Read queue
                if (ftStatus == FT_OK)
                {
                    printf(" (after wtite)Rx_queue_Bytes = %i\n", RxQueueBytes);
                }

    FT_GetStatus(ftHandle, &RxQueueBytes, &TxQueueBytes, &EventDWord);    // Read queue
            if (ftStatus == FT_OK) 
            {
                printf(" (after wtite)Rx_queue_Bytes = %i\n", RxQueueBytes);
                printf(" (after wtite)TxQueueBytes = %i\n", TxQueueBytes);
                printf(" (after wtite)EventDWord = %i\n\n\n\n", EventDWord);
            }




    ftStatus = FT_Read(ftHandle, RxBuffer, RxQueueBytes, &RxBytes);
    if (ftStatus == FT_OK)
    {



        printf(" FT_Read Ok\n");
        printf(" Recieved Byte = %c %c %c %c\n", RxBuffer);
        ofstream fout("ImageData.bin");  //writing ti file
                if (fout.is_open())
                {
                    //file opened successfully so we are here
                    printf("File Opened successfully!!\n");

                    for (int i = 0; i != RxQueueBytes; i++)
                    {
                        //if (RxBuffer[i]==31)
                        fout << RxBuffer[i] << endl; //writing ith character of array in the file
                    }
                    printf("Array data successfully saved into the file ImageData");
                }
                else //file could not be opened
                {
                    printf("File could not be opened.");
                }

    }
    else 
    {
        printf(" FT_Read Failed");
    }

    getchar();

    FT_Close(ftHandle);

    return;

输出显示

Rx_queue_Bytes = 19283

ImageData.bin 的属性说它有 57892 字节 请解释

以二进制模式打开文件并在 fout 命令行中删除 endl 解决了问题。

 ofstream fout("ImageData.dat", ios::out| ios::binary);
        if (fout.is_open())
        {
            //file opened successfully so we are here
            printf("File Opened successfully!!\n");

            for (int i = 0; i < Img_Read_index; i++)
            {

                fout << point[i] ; //writing ith character of array in the file
            }
            printf("Array data successfully saved into the file ImageData");
            fout.close();
        }
        else //file could not be opened
        {
            printf("File could not be opened.");
        }