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

Asked

Viewed 44 times

0

I declared a variable char SendDataCmd[256] which will store the commands sent from the computer to the microcontroller via UART. Commands have different sizes as for example RST, ADV ON e SET SNAME=BC118 among others. I have a function that will send the commands, follows 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 commands inside SendDataCmd since they have different size than the declared vector, how could you store them correctly within this vector?

  • 1

    Want to copy the text into the SendDataCmd ? If this is the case the function you are looking for is strcpy

1 answer

0

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

SendDataCmd[] = "blablabla";

This is not how strings are assigned in C. First, the [] is not part of the variable name, so there is no reason why it should appear on the left side of the =.

Then, even if you did the assignment that way, you’d have to declare SendDataCmd as pointer and not as array.

Global variables are bad and hateful. Their variable huart1 be 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 function BC118_Init does not return any value with any significance, so it should be void instead of int.

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

The command to be passed in the second parameter is of type char *, then you should 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");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.