Array elements not assigned in loop

elexinverter
Posts: 5
Joined: Fri May 21, 2021 4:23 am

Array elements not assigned in loop

Postby elexinverter » Fri May 21, 2021 4:42 am

Greetings,
I am beginner with ESP32 and using VSCode with PlatformIO plugin and Arduino framework.
Below is a code snippet:
  1. struct MasterStatusDef
  2. {
  3.   uint8_t members[40];
  4. };
  5.  
  6. union
  7. {
  8.   struct MasterStatusDef Value;
  9.   uint8_t Byte[sizeof(struct MasterStatusDef)];
  10. }MasterStatusRx, MasterStatusTx;
  11.  
  12. void setup()
  13. {
  14.   xTaskCreatePinnedToCore(ProcessTask, "Process", 2048, NULL, 1, NULL, 0);
  15.   ESP_LOGE(TAG,"initdone");
  16. }
  17.  
  18. void loop()
  19. {
  20.   // put your main code here, to run repeatedly:
  21.   vTaskDelay(1000 / portTICK_PERIOD_MS);
  22. }
  23.  
  24. void ProcessTask(void* pvParameters)
  25. {
  26.   uint8_t i;
  27.   for(;;)
  28.   {
  29.     bzero(MasterStatusRx.Byte, sizeof(MasterStatusRx.Byte));
  30.     bzero(MasterStatusTx.Byte, sizeof(MasterStatusTx.Byte));
  31.     for(i = 0; i < 5; i++)
  32.     {
  33.       MasterStatusTx.Byte[i] = 'a';
  34.       if(i > 1)   // **CONDITION A**
  35.       {
  36.         MasterStatusRx.Byte[i] = 'a';
  37.         ESP_LOGE(TAG, "%d", i);  // LOOP VARIABLE
  38.       }
  39.     }
  40.     ESP_LOGE(TAG, "%s", MasterStatusRx.Byte);
  41.     ESP_LOGE(TAG, "%s", MasterStatusTx.Byte);
  42.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  43.   }
  44. }
If the above code is executed I get only elements filled with character 'a' only in MasterStatusTx.Byte and not in the MasterStatusRx.Byte. The output of LOOP VARIABLE line is printed on terminal with incrementing values of 'i'.
Also if the CONDITION A is changed to 'if(1)' then the elements of MasterStatusRx.Byte are filled as expected.

I am unable to what i am missing on. Any help appreciated.

Thanks
Amar

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: Array elements not assigned in loop

Postby ESP_Sprite » Fri May 21, 2021 5:52 am

Your code zeroes out both values, then fills MasterTxStatus with 'a', but only fills the 2nd byte and up of MasterRxStatus with a's, leaving the first byte a zero. Strings in C are zero-terminated, that is, things like ESP_LOGE will print all characters in the string until they find a zero byte. Which is the first character in MasterRxStatus, so that gets printed as an empty string.

elexinverter
Posts: 5
Joined: Fri May 21, 2021 4:23 am

Re: Array elements not assigned in loop

Postby elexinverter » Fri May 21, 2021 6:53 am

My bad :oops: .
I made a logical error while framing the snippet, since I cant post the original code.
But your response helped in finding the logical bug in my actual code.
Since I have made all elements of array zero at beginning, the bug in index computation skipped one element and the string would terminate while printing at the place of skipped element since it is zero value (i need a holiday :D ).
Thanks for the help.

Not related to topic::
while sending data from PC to EPS32 via UART using terminal:
I want to know when does the event UART_DATA occur, per byte or there is a timeout? Cause as i type on keyboard in terminal, it occurs on every keystroke. If I paste a string into terminal it would occur after the entire string is received. I am using the UART event example from ESP-IDF.

Regards,
Amar

Who is online

Users browsing this forum: axellin, Google [Bot] and 113 guests