Home > Back-end >  Expression must have pointer-to-object type at C
Expression must have pointer-to-object type at C

Time:01-30

Here are the codes in order to send a string, get a response, and clears the buffer.

uint8_t usart6_tx_buffer[] = "AT CPSI?\r\n";

while (usart6_tx_counter < sizeof(usart6_tx_buffer))
{
    while (usart_flag_get(USART6, USART_TDBE_FLAG) == RESET)
        ;
    usart_data_transmit(USART6, usart6_tx_buffer[usart6_tx_counter  ]);
}
delay_sec(1);
usart6_tx_counter = 0x00;
printf("string: %s\r\n", usart6_rx_buffer);
for (usart6_rx_counter = 0; usart6_rx_counter < sizeof(usart6_rx_buffer); usart6_rx_counter  )
{
    usart6_rx_buffer[usart6_rx_counter] = 0;
}
usart6_rx_counter = 0;

This will send a "AT CPSI?" string, receive data and prints it.

However, since i'm using this codes many times, i want to make that codes to a function like below.

void USART6_TxRx(uint8_t input)
{
    while (usart6_tx_counter < strlen(input))
    {
        while (usart_flag_get(USART6, USART_TDBE_FLAG) == RESET)
            ;
        usart_data_transmit(USART6, input[usart6_tx_counter  ]);
    }
    delay_sec(1);
    usart6_tx_counter = 0x00;
    printf("string: %s\r\n", usart6_rx_buffer);
    for (usart6_rx_counter = 0; usart6_rx_counter < sizeof(usart6_rx_buffer); usart6_rx_counter  )
    {
        usart6_rx_buffer[usart6_rx_counter] = 0;
    }
    usart6_rx_counter = 0;
}

and want to use it like below.

uint8_t input;
uint8_t usart6_tx_buffer[] = "AT CPSI?\r\n";
USART6_TxRx(*usart6_tx_buffer[]);

which i assume that should be work, but it keeps showing

expression must have pointer-to-object type usart_data_transmit(USART6, input[usart6_tx_counter  ]);

whenever i build it.

are there anythings that i am missing?

CodePudding user response:

uint8_t usart6_tx_buffer[] = "AT CPSI?\r\n";
uint8_t input[];

void USART6_TxRx(uint8_t *input, int size);
void USART6_TxRx(uint8_t *input, int size)
{
    while (usart6_tx_counter < size)
    {
        while (usart_flag_get(USART6, USART_TDBE_FLAG) == RESET)
            ;
        usart_data_transmit(USART6, input[usart6_tx_counter  ]);
    }
    delay_sec(1);
    usart6_tx_counter = 0x00;
    printf("string: %s\r\n", usart6_rx_buffer);
    for (usart6_rx_counter = 0; usart6_rx_counter < sizeof(usart6_rx_buffer); usart6_rx_counter  )
    {
        usart6_rx_buffer[usart6_rx_counter] = 0;
    }
    usart6_rx_counter = 0;
}

void USART6_TxRx(usart6_tx_buffer, sizeof(usart6_tx_buffer));

using like this works.

CodePudding user response:

usart6_tx_counter You cannot use the operators on an array. Furthermore, you most likely want that array to be const. You could so something like this instead:

const uint8_t usart6_tx_buffer[] = "AT CPSI?\r\n";

for(int i=0; i<sizeof(usart6_tx_buffer)-1; i  )  // -1 if not to send the null terminator
{
  while (usart_flag_get(USART6, USART_TDBE_FLAG) == RESET)
    ;
  usart_data_transmit(USART6, usart6_tx_buffer[i]);
}

Similarly, USART6_TxRx(*usart6_tx_buffer[]); is gibberish, pass on USART6_TxRx(usart6_tx_buffer) to a function like:
void USART6_TxRx(const uint8_t* input)

  •  Tags:  
  • c
  • Related