How to store a word within a vector with a size larger than the word

0

I declare a char SendDataCmd[256] variable that will store commands sent from the computer to the microcontroller via UART . The commands have different sizes such as RST, ADV ON e SET SNAME=BC118 among others. I have a function that will send the commands, below.

#include "bc118.h"
#include "usart.h"
#include "main.h"

 char SendDataCmd[256];

int BC118_Init()
{
    SendDataCmd[] = "RST";//Linha que não funciona

    HAL_UART_Transmit(&huart1, &SendDataCmd, 1, 100);

    SendDataCmd[] = "ADV ON";//Linha que não funciona

    HAL_UART_Transmit(&huart1, &SendDataCmd, 1, 100);



}

I'm having trouble storing the commands inside SendDataCmd since they have different size than the declared vector, how could I store them correctly within that vector?

    
asked by anonymous 19.07.2018 / 14:31

1 answer

0

I do not know code for microcontrollers, but the code you gave does not compile. The error is on the lines you've marked:

SendDataCmd[] = "blablabla";

This is not how strings are assigned to C. First, [] is not part of the variable name, so it does not have to appear on the left side of = .

Then, even though you did the assignment in this way, you would have to declare SendDataCmd as a pointer instead of an array.

Global variables are bad and odious. Your variable huart1 is global until you go there, as long as you never want to interact with two of them. But this SendDataCmd has no need to be global.

If the BC118_Init function returns no value with any significance, then it should be void instead of int .

As I researched, the third parameter of HAL_UART_Transmit should be the size of the command, so using the fixed value 1 will not work.

The command to be passed in the second parameter is of type char * , so you must pass the string directly instead of its address.

Maybe what you want is this:

#include <string.h>
#include "bc118.h"
#include "usart.h"
#include "main.h"

void transmitir(char *comando) {
    HAL_UART_Transmit(&huart1, comando, strlen(comando), 100);
}

void BC118_Init() {
    transmitir("RST");
    transmitir("ADV ON");
}
    
19.07.2018 / 19:38