0

I use STM32L151CB I have function, which get data on ttl it uint8_t rxb[32], then I want it array convert to hex uint8_t AppDate[0] = rxb[0], AppDate must be a hex (0x13). I have two file uart_board.c the code below, but AppDate in file main.c. How I must use return in my function

uart_board.c:

int32_t resDateLidar(void){
  uint8_t rxb[32];
  uint8_t* lidar[10];
  char* str;
  char hex_num[3] = {0};
  //HAL_UART_Receive_IT( &UartContext[uartId].UartHandle, &UartContext[uartId].RxData, 1 );
  if(HAL_UART_Receive(&UartContext[UART_1].UartHandle, &rxb, 32, 300) == HAL_OK){
    DelayMs(100);
    for (int i = 0; i < 32; i++)
      {
        if((rxb[i] == 89) && (rxb[i+1] == 89)){
          lidar[0] = rxb[i];
          lidar[1] = rxb[i + 1];
          lidar[2] = rxb[i + 2];
          lidar[3] = rxb[i + 3];
          lidar[4] = rxb[i + 4];
          lidar[5] = rxb[i + 5];
          lidar[6] = rxb[i + 6];
          lidar[7] = rxb[i + 7];
          lidar[8] = rxb[i + 8];
          lidar[9] = rxb[i + 9];
          lidar[10] = rxb[i + 10];

          //str = (char*)lidar;

        }

      }
    //HAL_UART_Transmit(&UartContext[UART_1].UartHandle, &rxb, 32, 300);
    //HAL_UART_Transmit(&UartContext[UART_1].UartHandle, &lidar, 10, 300);
    DelayMs(1000);

  }

  return lidar;
}

I have sensor LIDAR, lidar send 9 byte, I can HAL_UART_Transmit data, on ttl. loop for to filter my data and only send 9 bytes of information instead of 32. I want to assign AppDate[0] = lidar[0],AppDate[1] = lidar[1],AppDate[2] = lidar[2]. (for example lidar send on my stm 89 89 194 0 13 134 13 160 55)

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
  • if `i == 31` then `rxb[i+1]` is accessing out of bounds. `uint8_t* lidar[10];` are 10 arrays of pointers, what are you modeling with it? What should it mean? Where should the pointers point? `How i must use return in my functio` - you have written the function, you could tell us. What should the function return? What should the value returned by the function mean? What are the data being transferred, what do they contain? How is the question related to lora and lorawan? What do you want to do? You should get ton of warnings at each `lidar[0] = rxb[i];` – KamilCuk Jan 22 '20 at 12:17
  • Two major problems: First of all `lidar` is an array of *pointers*, and you assign non-pointer values to its elements. Furthermore you define it as an array of `10` elements but use index `10` which is the *eleventh* element in the array. – Some programmer dude Jan 22 '20 at 12:21
  • 1
    First of all: Your question is somewhat unclear, partly because of sloppy editing. Even or perhaps especially if your native language is not English we'd appreciate it if you used proper punctuation and tried to formulate whole sentences and even words. Now to your assumed question. You may be confused about *text representations* of character values and the character *values* proper. For example, 65, 'A' and 0x41 are all the same thing in C or C++ (with the slight difference that in C++ 'A' is a `char`, but that's usually irrelevant except for overload resolution). – Peter - Reinstate Monica Jan 22 '20 at 12:23
  • @Someprogrammerdude look again. – Олександр Паланюк Jan 22 '20 at 12:38
  • @Peter-ReinstateMonica look again. – Олександр Паланюк Jan 22 '20 at 12:38
  • Neither in C nor in C++, a function can return a raw C array. Instead, you could provide a sufficient array as argument and let the function "fill" it. An array decays always to a pointer when used as function argument. Hence, it's even not necessary to use a reference to that array (though possible in C++). Btw. what is it? Tagged with [tag:c++] but rather looks like [tag:c]. – Scheff's Cat Jan 22 '20 at 12:39
  • @Scheff how to correctly write an argument to a function. I call this function in a file main.c, but this function stay on file uart_board.c – Олександр Паланюк Jan 22 '20 at 12:45
  • So, you need a prototype in `main.c`. Either declare the function in `main.c` or, even better, write the function declaration in a header `uart_board.h` which is included into `main.c`. You didn't read any book about C or C++, did you? – Scheff's Cat Jan 22 '20 at 12:47
  • @ОлександрПаланюк Why? None of the problems I (or KamilCuk) mentioned are fixed. You go out of bounds of your arrays, and you have incompatible assignments (assigning a `uint8_t` value to a `uint8_t*` element). Your compiler should really complain about the second thing (the incompatible types). Please listen to your compiler, it knows these things. – Some programmer dude Jan 22 '20 at 12:51
  • @Scheff i didn't read any book about C or C++, i need help. This my main.c below loop while and my function ``` void getchar_with_ttl(void) { uint8_t data = '\0'; //char *lidar; char hex_num[3] = {0}; uint8_t* lidar = '\0'; lidar == resDateLidar(); printf("lidar ="); printf(lidar); } while(1) { getchar_with_ttl(); //resDateLidar(); LoRaWAN_LOOP(); //getchar_loop(); } ``` – Олександр Паланюк Jan 22 '20 at 12:52
  • @ОлександрПаланюк Quite honestly, not reading books might be the reason you're having trouble? Learning a programming language is somewhat similar to learning a spoken or written language: It takes time, you need to study, and you won't be fluent immediately. [Here's a list of books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) that are considered good, please invest in a couple of them and take the time to learn C++ properly. – Some programmer dude Jan 22 '20 at 12:54
  • @Someprogrammerdude if HAL_UART_Transmit work, i can use this array, empty elements have some meaning, but only the first 7 are important to me – Олександр Паланюк Jan 22 '20 at 12:58
  • @Someprogrammerdude I work with Siemens controllers there are no such problems, now for one task I was transferred to C ++ and I have such a problem, I will read books, but now I need to solve a problem in C ++ now – Олександр Паланюк Jan 22 '20 at 13:00
  • Okay then, here's an important lesson: C and C++ are two very different languages, with different semantic rules. What works in one language may not work in the other. Most related to this problem, C++ have much stronger type-checking than C, which means that assignment between incompatible types that was "allowed" by C will generate errors in C++. The out-of-bounds indexing you do is a problem with both languages though. – Some programmer dude Jan 22 '20 at 13:38
  • @Someprogrammerdudeokay i adjust the indexes , what i do next? – Олександр Паланюк Jan 22 '20 at 13:47
  • @Someprogrammerdude what you say abou function "extern"? if i use "extern" i can get access to my array with main.c – Олександр Паланюк Jan 22 '20 at 14:09
  • @Someprogrammerdude The semantics of arrays and pointers are -- perhaps regrettably, because it prevented C++ from being a better language -- identical in C and C++, are they not? C++ has stricter -- syntactic! -- type checking in order to expose semantic problems also present when compiled as C. – Peter - Reinstate Monica Jan 22 '20 at 14:11
  • @Peter-ReinstateMonica Because `rxb[i]` (for any index `i`) have different type compared to `lidar[j]` (for any index `j`) the assignment `lidar[j] = rxb[i]` is invalid in C++. It's allowed in C though, but should generate a warning. But the out-of-bounds indexing will still be the same. – Some programmer dude Jan 22 '20 at 14:28
  • 1
    @ОлександрПаланюк Again, think about the types of the `rxb` and `lidar` arrays. What type is a single element of `rxb`? What type is a single element of `lidar`? What makes you think those two types are compatible? – Some programmer dude Jan 22 '20 at 14:30
  • @Peter-ReinstateMonica the sensor does not always send the correct parcel, sometimes there are corrupted messages, the length of the buffer 32 allows you to choose the right message, the correct message should be highlighted and so I use a lidar – Олександр Паланюк Jan 22 '20 at 14:58

0 Answers0