BLE data packet

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
bewac_2776061
Level 1
Level 1
First like received

Hello ,

i wanted to send a data packet from server to client. Because there were two different data types, the uint32 and uint8. I've defined a uint8 array, which had the length 5:

uint8 sendDataPacket[5];

in the first 4 array's units, i've saved the uint32: firstly i separated the uint32 into 4 uint8s, and then saved every uint8 in each array unit. The last data type uint8, i saved it in the last array unit of sendDataPacket[5].

After the sending from the server and receiving of the Client, i readed them by using 'CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T RX_data = (CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *) eventparam; ' and then saved them in another uint8 receiveDataPacket[5] on the client side.

Unfortunately, i could not read the last uint8 from the receiveDatapacket[5]. The data there was not what i expected.

Could anyone tell me, what was going wrong?

0 Likes
10 Replies
Anonymous
Not applicable

Most likely you are not storing the uint8 into the array for sending in the first place correctly. Try looking at your code to write the values into the notification to see if you are just passing a reference or something. Also, you need to make sure the length of the array for sending the data is set to 5 for it to use all 5 values.

It would be easier to help if you show us the code in question for sending and receiving the data.

0 Likes

On the Server side:

I've converted the analog signal to digital signal, this time i used uint16 and uint8, and stored them in the uint8 sendDataPacket[3];

1)       uint8 sendDataPacket[3];

           int16 x= Sensor_ADC_CountsTo_mVolts(0, Sensor_ADC_GetResult16(0)) ;

           uint16 y=(uint16)x;

           uint8 buttons_temp = SendButtons8();   // 0b00000010

           sendDataPacket[0]= (uint8)y;

           sendDataPacket[1]= (uint8)(y >> 8u);

           sendDataPacket[2]= (uint8)buttons_temp ;

2)        CYBLE_GATTS_HANDLE_VALUE_NTF_T      sendTxDataNtf;

           sendTxDataNtf.value.val  = sendDataPacket;

           sendTxDataNtf.value.len  = 1;

           sendTxDataNtf.attrHandle = CYBLE_SERVER_ACCEL_SERVER_ACCEL_TX_DATA_CHAR_HANDLE;

           CyBle_GattsNotification(cyBle_connHandle, &sendTxDataNtf);

           CyBle_ProcessEvents();

And the Client side:

1)         case CYBLE_EVT_GATTC_HANDLE_VALUE_NTF:

            CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *RECEIVE_RX_data = (CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *)eventparam;

             receiveDataPacket[0]=(uint8)(*RECEIVE_RX_data->handleValPair.value.val);

             receiveDataPacket[1]=(uint8)((*RECEIVE_RX_data->handleValPair.value.val)>>8u);

             receiveDataPacket[2]=(uint8)((*RECEIVE_RX_data->handleValPair.value.val)>>16u);

             break;

Thanks!

You set the data length for the notification to '1', so only 1 byte will be transferred. Use

sendTxDataNtf.value.len  = 1;

0 Likes

I've tried sendTxDataNtf.value.len = 3; but it doesn't work.

I send the code, because i am new in programming with Cypress BLE !, so could you please check them?

Thank you!

Server: app_Buttons.c

------------------------------------------------------------------------------------------------------------

/*******************************************************************************

* File Name: app_Buttons.c

*******************************************************************************/

#include "main.h"

#include "stdio.h"

#include "app_Buttons.h"

uint8 SendButtons8()

{

    uint8 ButtonsState=0b00000000;

   

    if(Pin_S1_Read())

    {

        ButtonsState =0b00000001;

    }else if(Pin_S2_Read())

    {

        ButtonsState =0b00000010;

    }

   

    return ButtonsState;

}

/* [] END OF FILE */

Server: app_accel.c

------------------------------------------------------------------------------------------------------------

/*****************************************************************************************

* File Name: app_accel.c

*****************************************************************************************/

#include "app_accel.h"

#include "app_Ble.h"

void SendCommand()

{

  

    CYBLE_GATTS_HANDLE_VALUE_NTF_T      sendTxDataNtf;

   

    if(Scan_Accelerometer())

    {

          if(connStatus== CONNECTED)

          {       

                   sendTxDataNtf.value.val  = sendDataPacket;

                   sendTxDataNtf.value.len  = 3;

                   sendTxDataNtf.attrHandle = CYBLE_SERVER_ACCEL_SERVER_ACCEL_TX_DATA_CHAR_HANDLE;

                   CyBle_GattsNotification(cyBle_connHandle, &sendTxDataNtf); 

                   CyBle_ProcessEvents();     

           }

   }else

  {

      CySysPmSleep();

   }

}

uint8 Scan_Accelerometer()

       if(Sensor_ADC_IsEndConversion(Sensor_ADC_RETURN_STATUS))

       {

             int16 x= Sensor_ADC_CountsTo_mVolts(0, Sensor_ADC_GetResult16(0)) ;

             uint16 y=(uint16)x;

             //state=NORMAL;

             uint8 buttons_temp = SendButtons8(); //z.B.0b00000010

       

            sendDataPacket[0]= (uint8)y;

            sendDataPacket[1]= (uint8)(y >> 8u);      

            sendDataPacket[2]= (uint8)buttons_temp ;

       

           return 1;

    }else{

           return 0;  

    }

}

/* [] END OF FILE */

Server: app_Ble.c

------------------------------------------------------------------------------------------------------------

/*******************************************************************************

* File Name: app_Ble.c

*******************************************************************************/

#include "app_Ble.h"

uint16 mtuSize      = CYBLE_GATT_MTU;  

extern uint8 applicationPower;

void HandleBleProcessing(void)

{   

      switch (cyBle_state)

     {

            case CYBLE_STATE_ADVERTISING:

                    break;

       

           case CYBLE_STATE_CONNECTED:

                    connStatus= CONNECTED;

                    Sensor_ADC_Start();

                    Sensor_ADC_StartConvert();         

                    if(CyBle_GattGetBusStatus() != CYBLE_STACK_STATE_BUSY)

                    {

                         SendCommand();

                         applicationPower = DEEPSLEEP;

                    }

                    break;      

      

        case CYBLE_STATE_DISCONNECTED:

                CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);

                break;

        default:

                break;      

    }

}

void AppCallBack(uint32 event, void *eventParam)

{  

    switch (event)

    {

        case CYBLE_EVT_STACK_ON:

                break;

           

        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

                 RED_LED_ON();

                 CyDelay(1000);

                 break;

           

        case CYBLE_EVT_GATT_CONNECT_IND:

                GREEN_LED_ON();

                break;

                     

        case CYBLE_EVT_GATTS_XCNHG_MTU_REQ:          

            if(CYBLE_GATT_MTU > ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu)

            {

                mtuSize = ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu;

            }else

            {

                mtuSize = CYBLE_GATT_MTU;

            }          

            break;

        default:

            break;

    }

}

/* [] END OF FILE */

Client: BLE_CAR_Client.c

------------------------------------------------------------------------------------------------------------

/*******************************************************************************

* File Name: BLE_CAR_Client.c

*******************************************************************************/

#include "BLE_CAR_Client.h"

#include "LED.h"

CYBLE_CONN_HANDLE_T connHandle;

CYBLE_API_RESULT_T apiResult;

CYBLE_GAP_BD_ADDR_T connectPeriphDevice;

void FilterScanResponsePackets(CYBLE_GAPC_ADV_REPORT_T* scanReport)

{

      if(scanReport->data[1] == MANUFACTURER_SPECIFIC_DATA &&

          scanReport->data[2] == COMPANY_LSB &&

         scanReport->data[3] == COMPANY_MSB &&

         scanReport->data[4] == MAN_SPEC_DATA_LSB && scanReport->data[5] == MAN_SPEC_DATA_MSB)

      {

            CyBle_GapcStopScan();

            memcpy(connectPeriphDevice.bdAddr, scanReport->peerBdAddr,

               sizeof(connectPeriphDevice.bdAddr));

            ble_TRY_Server_found = TRUE;  

      }

}

void ApplicationEventHandler(uint32 event, void *eventparam)

{

       switch(event)

       {

             case CYBLE_EVT_STACK_ON:       

                      break;

            case CYBLE_EVT_GAPC_SCAN_START_STOP:

                     break;

           case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:

                    FilterScanResponsePackets((CYBLE_GAPC_ADV_REPORT_T *)eventparam);

                    break;

          case CYBLE_EVT_GATT_CONNECT_IND:

                  connHandle = *(CYBLE_CONN_HANDLE_T *)eventparam;

                  break;

         case CYBLE_EVT_GATT_DISCONNECT_IND:

                  RED_LED_ON();

                  CyDelay(1000);

                  break;

         case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

                 RED_LED_ON();

                 break;

        case CYBLE_EVT_GATTC_HANDLE_VALUE_NTF:

        {

               CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *UART_RX_data = (CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *)eventparam;

               receiveDataPacket[0]=(uint8)(*UART_RX_data->handleValPair.value.val); 

               receiveDataPacket[1]=(uint8)((*UART_RX_data->handleValPair.value.val)>>8u);

               receiveDataPacket[2]=(uint8)((*UART_RX_data->handleValPair.value.val)>>16u);

             break;

         }

       default:

            break;

       }

}

0 Likes

When you want to have someone looking at your project, please send the complete project ("File / Create Workspace Bundle"). This includes all files, and makes work easier.

For your problem: look at the actual definition of CyBle_GattsNotification and its parameter types. You will find that sendTxDataNtf.value.val is a pointer to the actual data (see type  CYBLE_GATT_VALUE_T), but you are trying to set its value directly. Use

sendTxDataNtf.value.val=&sendDataPacket;

instead.

0 Likes
lock attach
Attachments are accessible only for community members.

Thank you first!

Because i 've defined: uint8 sendDataPacket[3];

                     so i indicate the pointer as :   sendTxDataNtf.value.val  = &sendDataPacket[0];

I also send the entire packet of my project.

Thanks in advance!

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

An array of length 5 is index from array[0] up to array[4]. When you use array[5], you store the data outside of the array, so it will not be transmitted (because the BLE stack only takes the actual array). When you are unlucky, that memory location holds something important and your code crashes.

0 Likes
Anonymous
Not applicable

It depends on the code itself. He claims to be using 5 for all of the values, which would mean that he allocates space for a 6-byte array (zero-indexed), and the correct length of 5. And consistent access/usage in the code would be fine with this, as the unused byte would just be wasted space. But, yes, if you set the array to size 4 and assign values to array[5], then it will overflow and corrupt RAM space.

Hence why I was asking if he could give us the source code to see how he is using it, and if there is a pointer/referencing issue or something else.

0 Likes

The array declaration is (from the first post):

uint8 sendDataPacket[5];

so its clearly only 5 bytes long, and therefore accessing sendDataPacket[5] is not the array anymore.

0 Likes
Anonymous
Not applicable

Ah. Oops, I'm wrong. You are right @user_78878863. I was confusing the C notation with VB notation >.<

He should be using 0-4 as you stated above. The 5th byte would then be sendDataPacket[4] for reading and writing.

PS: VB uses uint8 arrayName[5] to signify an array from 0 to 5 inclusive, which was making me think he was declaring a 6-byte array and using only 5 bytes in it...

0 Likes