簡體   English   中英

難以從數組讀取值

[英]Difficulty in reading values from an array

我正在編寫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上的示例

我想我缺少了一些東西,但是我不確定到底是什么。 非常感謝您的幫助。

首先,如果不進一步看其余代碼,將永遠不會寫入全局receive_data的前三個字節。 看一看您的advertisementback()的這一部分:

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]);

因此,調試輸出的前三個字節將始終包含隨機數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM