难以从数组中读取值

时间:2017-09-15 16:19:35

标签: c++ arrays function

我正在编写一个C ++代码,我正在努力解决一些相当简单的问题:

我已经声明了一个数组

uint8_t *received_data作为我代码中的全局变量。

然后我在一个函数中分配它的内存:

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {

    if(params->type == 3){
        for(int i = 0; i < params->advertisingDataLen; i++){
            if(params->advertisingData[i] == 0x16){
                if(params->advertisingData[i+1] == 0x34 && params->advertisingData[i+2] == 0x23){
                    received_data_size = params->advertisingDataLen - (i + 3);
                    received_data = new uint8_t[received_data_size];
                    for(int index = i+3; index < params->advertisingDataLen; index++){
                        received_data[index] = params->advertisingData[index];
                        //printf("%02x ", received_data[index]);//params->advertisingData[index]);                       
                        //printf("\r\n");
                    }
                }
            }    
        }  
    }
}

请注意,评论的printf正在打印我正确收到的数据。

但是在我的主要内容中,当我尝试相同的printf时,我大部分时间都会收到垃圾,有时我会在前三个地方收到数组的最后三个元素,然后是垃圾。

我的主要是:

int main(void)
{
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);
    bool state = true;
    while(true){
        ble.waitForEvent();
        measurement[2]++;
        printf("In the loop \n");
        for(int i = 0; i < received_data_size; i++){
            printf("%02x ", received_data[i]);//params->advertisingData[index]);                       
            printf("\r\n");
        }
        delete[] received_data;
    }
}

截至目前的整个代码是:

#include "mbed.h"
#include "ble/BLE.h"

/* Optional: Device Name, add for human read-ability */
const static char     DEVICE_NAME[] = "G4";

uint16_t uuid16_list[]        = {0x2334};

/* You have up to 26 bytes of advertising data to use. */
const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   /* Example of hex data */

uint8_t meas = 0;

uint8_t received_data_size;

static uint8_t *received_data;


uint8_t measurement[] = {0x34,0x23, meas};

/* Optional: Restart advertising when peer disconnects */
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    BLE::Instance().gap().startAdvertising();
}
/**
 * This function is called when the ble initialization process has failed
 */
void onBleInitError(BLE &ble, ble_error_t error)
{
    /* Avoid compiler warnings */
    (void) ble;
    (void) error;

    /* Initialization error handling should go here */
}    


void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {

    if(params->type == 3){
        for(int i = 0; i < params->advertisingDataLen; i++){
            if(params->advertisingData[i] == 0x16){
                if(params->advertisingData[i+1] == 0x34 && params->advertisingData[i+2] == 0x23){
                    received_data_size = params->advertisingDataLen - (i + 3);
                    received_data = new uint8_t[received_data_size];
                    for(int index = i+3; index < params->advertisingDataLen; index++){
                        received_data[index] = params->advertisingData[index];
                        //printf("%02x ", received_data[index]);//params->advertisingData[index]);                       
                        //printf("\r\n");
                    }
                }
            }    
        }  
    }
}

/**
 * Callback triggered when the ble initialization process has finished
 */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        onBleInitError(ble, error);
        return;
    }

    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }

    /* Set device name characteristic data */
    ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);

    /* Optional: add callback for disconnection */
    ble.gap().onDisconnection(disconnectionCallback);

    /* Sacrifice 3B of 31B to Advertising Flags */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);

    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, measurement, sizeof(measurement));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));


    /* Optional: Add name to device */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));

    /* Set advertising interval. Longer interval == longer battery life */
    ble.gap().setAdvertisingInterval(500); /* 100ms */

    /* Start advertising */
    //ble.gap().startAdvertising();

    /*Start Scanning*/
    ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
    ble.gap().startScan(advertisementCallback);
}

int main(void)
{
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);
    bool state = true;
    while(true){
        ble.waitForEvent();
        measurement[2]++;
        printf("In the loop \n");
        for(int i = 0; i < received_data_size; i++){
            printf("%02x ", received_data[i]);//params->advertisingData[index]);                       
            printf("\r\n");
        }
        delete[] received_data;
    }
}

BLE代表蓝牙低能耗。该代码基于mbed.org上的示例

我想我错过了什么,但我不确定到底是什么。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:3)

首先,如果不进一步查看其余代码,将永远不会写入全局的received_data的前三个字节。看一下adsCallback()的这一特定部分:

for(int index = i+3; index < params->advertisingDataLen; index++){
                    received_data[index] = params->advertisingData[index];

因此,用于写入received_data的索引始终以3的偏移量开始 - 从不开始 - 而应该从0开始。为此目的添加专用的索引变量。在main()中,您创建了一个从0开始的循环:

for(int i = 0; i < received_data_size; i++){
        printf("%02x ", received_data[i]);

因此,调试输出的前三个字节将始终包含随机数据。

相关问题