How to pass "char string" to function

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to pass a char string I changed to a function?

 

This code doesn't work:

 

char AT_Send [] = "AT$SF=1122\r"; // 11x

void Send_AT_CMD(char str[], int n_chars) {
    for (int n = 0; n_chars; n++) {
        USART_0_write(str[n]);
        while (!(USART0.STATUS & USART_TXCIF_bm)); //wait for USART TX complete
        USART0.STATUS = USART_TXCIF_bm; //Clear TXCIF flag
    }
}

Send_AT_CMD(AT_Send, 11);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is all standard C stuff.

Within a function parameter list, you can do either

void Send_AT_CMD(char str[], int n_chars) {

or

void Send_AT_CMD(char *str, int n_chars) {

They mean the same thing (the first way is just an alternative syntax that is allowed within function parameter list).

So I don't see anything wrong with how you are passing in parameters.

Your for loop, however, looks highly suspect

for (int n = 0; n_chars; n++) {
                 ^^^^^

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

MrKendo wrote:

for (int n = 0; n_chars; n++) {

  change this to:

MrKendo wrote:

for (int n = 0; n < n_chars; n++) {

Note however C strings, ie those enclosed with "", are null terminated, is your array large enough for this extra character?

Normally string functions test for the null to end, rather then count characters!

 

Jim

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kevil wrote:
This code doesn't work

What do you mean, "doesn't work" ?

 

  • What did you expect it to do ?
  • What is it actually doing ?
  • What testing / investigation / debugging have you done to find the problem ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
is your array large enough for this extra character?

Kevil wrote:

char AT_Send [] = "AT$SF=1122\r"; // 11x

So, yes - it is automatically the correct length.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Appreciate your help. I forgot to set a condition for n:

 

n < n_chars instead of n_chars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If your function isn't going to be modifying the string you really should declare it as const:

void Send_AT_CMD( char const str[], int n_chars );
void Send_AT_CMD( char const * str, int n_chars );

This has a variety of benefits including letting you pass string literals to the function without getting compilation errors/warnings, and letting users and the compiler know that strings passed to the function won't be modified.

 

As ki0bk pointed out, quoted strings are null-terminated and you can always null-terminate strings you create at runtime. If all strings are null-terminated, you can rewrite your function like so:

void Send_AT_CMD(char const * str ) {
    char c;
    while (( c = *str++ )) {
        USART_0_write( c );
        while (!(USART0.STATUS & USART_TXCIF_bm)); //wait for USART TX complete
        USART0.STATUS = USART_TXCIF_bm; //Clear TXCIF flag
    }
}

Send_AT_CMD( "AT$SF=1122\r" );

Depending on the AVR variant you are using you probably want to write the function so that it can also handle strings that have been placed in ROM.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you. I am changing the string before calling the function e.g. writing two bytes (uint16_t measured ADC voltage converted to ASCII e.g 11.22 Volts *100 = 1122 = 0x0244.  "AT$SF=1122\r" will be "AT$SF=4402\r".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kevil wrote:
I am changing the string before calling the function

That has nothing to do with making the input parameter to the function 'const' !

 

As  apcountryman  says, making the input parameter to the function 'const' tells you that the function does not modify it.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...