C++ 中的内存映射流数据

Memory map streaming data in C++

我正在从 LiDAR 实时收集数据。我创建了一个 for 循环来迭代启动一个帧,收集数据并将其保存到一个数组 (distArray) 中。但是,我坚持使用内存映射部分。我的数组需要 500 个整数 ==> 2000 字节的内存.. 当我尝试使用 CopyMemory() 将每个数组元素映射到内存时,我收到以下错误“Exception throw at 0x5843335E (vcruntime140d.dll) in file.exe: 0xC0000005: 访问冲突写入位置 0x007E0000." .. 关于如何解决这个问题有什么想法吗?

下面的代码中有很多与问题无关的函数和头文件调用,请不要介意..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bta.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 2000
TCHAR szName[] = TEXT("mapObjFile");

void setTOFParameters(BTA_Handle btaHandle, uint32_t newTint, uint32_t newFPS, uint32_t newFMOD);
static void errorHandling(BTA_Status status);

const int arraySize = 500;
const int numFrames = 500;
int distArray[arraySize];

int main() {

    BTA_Status status;
    BTA_Config config;
    printf("BTAinitConfig()\n\n");
    status = BTAinitConfig(&config);
    errorHandling(status);

    uint8_t udpDataIpAddr[] = { 224, 0, 0, 1 };
    config.udpDataIpAddr = udpDataIpAddr;
    config.udpDataIpAddrLen = 4;
    config.udpDataPort = 10002;
    uint8_t tcpDeviceIpAddr[] = { 192, 168, 0, 10 };
    config.tcpDeviceIpAddr = tcpDeviceIpAddr;
    config.tcpDeviceIpAddrLen = 4;
    config.tcpControlPort = 10001;

    config.frameQueueMode = BTA_QueueModeDropOldest;
    config.frameQueueLength = 500;
    config.frameMode = BTA_FrameModeDistAmp;

    // OPEN Connection
    BTA_Handle btaHandle;
    printf("BTAopen()\n\n");
    status = BTAopen(&config, &btaHandle);
    errorHandling(status);

    printf("Service running: %d\n", BTAisRunning(btaHandle));
    printf("Connection up: %d\n\n", BTAisConnected(btaHandle));

    BTA_DeviceInfo *deviceInfo;
    printf("BTAgetDeviceInfo()\n");
    status = BTAgetDeviceInfo(btaHandle, &deviceInfo);
    errorHandling(status);
    printf("Device type: 0x%x\n", deviceInfo->deviceType);
    printf("BTAfreeDeviceInfo()\n\n");
    BTAfreeDeviceInfo(deviceInfo);

    // READ Register
    uint32_t regAddr = 0x1004; //MLX75123, Register 0x1000 = I2C_ADDRESS
    uint32_t regValue;
    status = BTAreadRegister(btaHandle, regAddr, &regValue, 0);
    errorHandling(status);
    printf("BTAreadRegister : Register 0x%04X has value 0x%04X\n\n", regAddr, regValue);

    for (int i = 1; i < numFrames; i++) {
    // GET The Frame
        printf("Getting distance and amplitude data :\n");
        BTA_Frame *frame;
        printf("BTAgetFrame()\n");
        status = BTAgetFrame(btaHandle, &frame, 300);
        errorHandling(status);

        BTA_DataFormat dataFormat;
        BTA_Unit unit;
        uint16_t xRes, yRes;

        // Getting the distance data into a buffer and calculating the average amplitude over the entire frame :
        uint16_t *distances;
        printf("BTAgetDistances()\n");
        status = BTAgetDistances(frame, (void**)&distances, &dataFormat, &unit, &xRes, &yRes);
        errorHandling(status);
        if (dataFormat == BTA_DataFormatUInt16) {
            uint32_t distAvg = 0;
            for (int y = 0; y < yRes; y++) {
                for (int x = 0; x < xRes; x++) {
                    distAvg += distances[x + y * xRes];
                }
            }

            if (xRes != 0 && yRes != 0) {
                distArray[i] = distAvg / xRes / yRes;
                printf("The average distance is %d.\n", distArray[i]);  
            }
        }

    // FREE The Frame
        printf("BTAfreeFrame()\n\n");
        status = BTAfreeFrame(&frame);
        errorHandling(status);

    // ----------------------- Memory Mapping -----------------------
        HANDLE hMapFile;
        LPCTSTR pBuf;

        hMapFile = CreateFileMapping(
            INVALID_HANDLE_VALUE,    // use paging file
            NULL,                    // default security
            PAGE_READWRITE,          // read/write access
            0,                       // maximum object size (high-order DWORD)
            BUF_SIZE,                // maximum object size (low-order DWORD)
            szName);                 // name of mapping object
        if (hMapFile == NULL)
        {
            _tprintf(TEXT("Could not create file mapping object (%d).\n"),
                GetLastError());
            return 1;
        }
        pBuf = (LPTSTR)MapViewOfFile(hMapFile,            // handle to map object
                                     FILE_MAP_READ, // read/write permission
                                     0,
                                     0,
                                     BUF_SIZE);
        if (pBuf == NULL)
        {
            _tprintf(TEXT("Could not map view of file (%d).\n"),
                GetLastError());

            CloseHandle(hMapFile);

            return 1;
        }

        CopyMemory((PVOID)pBuf, &distArray, BUF_SIZE);
        _getch();

        /*UnmapViewOfFile(pBuf);

        CloseHandle(hMapFile);*/

    }

    // CLOSE Connection
    printf("BTAclose()\n\n");
    status = BTAclose(&btaHandle);
    errorHandling(status);


    // END Program
    printf("Hit <ENTER> to close the window .. ");
    fgetc(stdin);
}

将数据映射到内存后,我将使用 Python (https://docs.python.org/3.0/library/mmap.html) 中的 mmap 库根据标记名参数访问数据 ...

看这里:https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa366535(v%3Dvs.85)

void CopyMemory(
  _In_       PVOID  Destination,
  _In_ const VOID   *Source,
  _In_       SIZE_T Length
);

第一个参数是目标,第二个是源。但是在你的代码中你有:

CopyMemory((PVOID)pBuf, &distArray, BUF_SIZE);

...反过来。所以例外是因为你将数据从 distArray 复制到 pBuf 这是只读内存的映射。