• AVR Freaks

Hot!Passing a char array to a function but only 1st element getting sent

Author
gehan_s
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2013/01/18 23:52:52
  • Location: 0
  • Status: offline
2019/11/05 21:46:32 (permalink)
0

Passing a char array to a function but only 1st element getting sent

Hi all,
I am passing a char array to a function but it seems only the first element gets passed. I had implemented this code on an arduino which was working perfectly. But when I migrated to the microchip device it doesn't seem to like what I'm doing. I have attached the code and a debug screenshot. In this particular scenario I am passing "AT+ROLE1\r\n" but in the debug window I can only see the charter 'A'.
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mcc_generated_files/system.h"
#include "stdbool.h"
#include "project_defines.h"
#include "mcc_generated_files/uart1.h"

typedef enum
{
    HM10, 
    BG96, 
    terminal
}uartDevice;

void uartWrite(char data[50], uartDevice device);
void uartFlush(uartDevice device);

void bleInit();
bool bleCommand(char command[50], char result[50], uint16_t waitTime);
bool readCompareBLE(char result[50], uint16_t waitTime);

uint16_t getTime();
uint16_t elapsedTime(uint16_t startTime);
void delay(uint16_t delayTime);

int main(void)

    SYSTEM_Initialize(); 
    delay(2000);
    bleInit(); 
    while (1)
    {
    delay(2000);
    bleInit();
        
    }
    return 0;
}

void bleInit()
{
    uint8_t commandState = 0;
    bool initBLE = false;
    while(initBLE == false)
    {
        switch(commandState)
        {
            case 0:
                if(bleCommand(bleRole1, bleCommandOK, 100) == true)
                {
                    commandState = 1;
                }
                else
                {
                    
                }
                delay(1000);
            break;
            case 1:
                if(bleCommand(bleIMMEI1, bleCommandOK, 100) == true)
                {
                    commandState = 2;
                }
                else
                {
                    
                }
                delay(1000);
            break;
            case 2:
                if(bleCommand(bleReset, bleResetOK, 100))
                {
                    commandState = 0;
                }
                else
                {
                    
                }
                initBLE = true;
                delay(1000);
                uartWrite(bleScannerOK, terminal);
            break;
            default:
            break; 
        } 
    }
}

bool bleCommand(char command[50], char result[50], uint16_t waitTime)
{
    bool runCommandOk = false;
    uint8_t retryCount = 0;
    while(runCommandOk == false && retryCount < 1)
    { 
        uartWrite(command, HM10);
        runCommandOk = readCompareBLE(result, waitTime);
        retryCount = retryCount + 1; 
    }
    return runCommandOk;
}

 
#define XC_HEADER_TEMPLATE_H

#include <xc.h>

// common /////////////////////////////////////
char resendCommand[] = "RESEND COMMAND";
///////////////////////////////////////////////

// BLE definitions ////////////////////////////
char bleRole1[] = "AT+ROLE1\r\n";
char bleIMMEI1[] = "AT+IMME1\r\n";
char bleReset[] = "AT+RESET\r\n";
char bleCommandOK[] = "OK+Set1";
char bleResetOK[] = "OK+RESET";
char bleScannerOK[] = "BLE SCANNER OK";
///////////////////////////////////////////////

 
 
 
regards
Gehan 

Attached Image(s)

#1

16 Replies Related Threads

    andersm
    Super Member
    • Total Posts : 2666
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 09:41:19 (permalink)
    +2 (4)
    Do you understand the relationship between arrays and pointers in C?
    #2
    du00000001
    Just Some Member
    • Total Posts : 3156
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 10:05:08 (permalink)
    +2 (2)
    I'd love to see the code of your uartWrite() function.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 10:15:15 (permalink)
    +1 (1)
    Note the Arduino is C++ not C.
     
     "it seems only the first element gets passed". 
    What is leading you to that conclusion?
    Are you getting Compile warnings with this code?
    #4
    du00000001
    Just Some Member
    • Total Posts : 3156
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 10:26:38 (permalink)
    +1 (1)
    NKurzman
    What is leading you to that conclusion?

    Might be the fact that the watch window (screen copy within the first post) shows only a single character for *command
     
    @gehan_s
    Don't expect the debugger to interpret each and every aspect of your source code.
    You could check the memory window whether the string is available starting at whereever command is pointing to.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    1and0
    Access is Denied
    • Total Posts : 9864
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 10:47:05 (permalink)
    +3 (3)
    gehan_s
    I am passing a char array to a function but it seems only the first element gets passed.
    ...
    In this particular scenario I am passing "AT+ROLE1\r\n" but in the debug window I can only see the charter 'A'.

    In C, an array reference always "decays" to a pointer to the first element of the array. An array in a function call will be passed to the function as a pointer. In your case, that pointer can point to only one char at a time.
     
    #6
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11395
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 11:01:49 (permalink)
    +3 (3)
    You can't "pass an array" to a function.  You can only pass a pointer to the first element of the array to a function.  And this:
     
    bool bleCommand(char command[50], char result[50], uint16_t waitTime)

     
    doesn't do what you probably think it does.  The [50] is ignored by the compiler and and "command" and "result" are just  treated as char *.
     
     
    #7
    1and0
    Access is Denied
    • Total Posts : 9864
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 11:08:27 (permalink)
    +2 (2)
    One more thing, the function does not know the size of the array; so you'll have to pass it to the function as well.
    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 11:17:13 (permalink)
    +1 (1)
    I do not know about current compilers but I have seen the char command[50] be interpreted as char *, But give a warning if it is not pointing at a 50 char array.   I do not know what GCC does by default, or will any options for this syntax.
    #9
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11395
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 11:26:36 (permalink)
    +2 (2)
    the function does not know the size of the array

     
    Maybe not since everything appears to be null terminated, but we haven't seen all of the code.
    #10
    gehan_s
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2013/01/18 23:52:52
    • Location: 0
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 21:17:04 (permalink)
    0
     
    Hi everyone,

    Thank you for all your replies. The debug window result had me chasing down the wrong rabbit hole! It was indeed pointing to the 1st element of the array so the data is getting passed fine and even getting into the HM10 module. Upon further investigation I think I have narrowed it down to a while loop which is not getting terminated.
     
    bool readCompareBLE(char result[50], uint16_t waitTime)
    {
        char serialIn = '\0';
        char reply[50];
        uint16_t i = 0;
        bool replyMatch = false;
        uint16_t startTime = getTime();
        IO_RA10_SetHigh();
        while(elapsedTime(startTime) < waitTime << 2)
        {
            if(UART2_is_rx_ready() == true)
            {
                serialIn = UART2_Read();
                if(serialIn != '\n' && serialIn != '\r')
                {
                    reply[i] = serialIn;
                    i = i + 1;
                    UART1_Write(serialIn);
                }
            }
        }
        IO_RA10_SetLow();
        reply[i] = '\0';
        uartWrite(reply, terminal);
        
        if(strcmp(reply, result) == 0)
        {
            replyMatch = true;
        }
        else
        {
            replyMatch = false;
            uartWrite(resendCommand, terminal);
        }
        uartWrite(reply, terminal);
        uartFlush(HM10);
        return replyMatch;
    }

     
    At the moment the RA10 LED doesn't turn off which means the while loop was not exiting. I commented out everything inside the while loop which made the RA10 led turn off so I know the timer function works fine and the while loop exits was ok. Furthermore by printing out the serialIn variable I was able to get my expected output which is "OK+Set:1".
     
    So I went in and did the same in the UART2_read() function which also has a while loop. Here the RA9 LED does not turn off.
     
    uint8_t UART2_Read(void)
    {
        while(!(U2STAbits.URXDA == 1))
        {
            IO_RA9_SetHigh();
        }
        IO_RA9_SetLow();
        if ((U2STAbits.OERR == 1))
        {
            U2STAbits.OERR = 0;
        }
        return U2RXREG;
    }

     
    It looks like the UART2 module reads all the data fine but doesn't exit after reading all the data. Any thoughts on this one?
     
    regards
    G
    #11
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/06 22:08:10 (permalink)
    0
    That function will wait forever until a character arrives. Is that your intention?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    JPortici
    Super Member
    • Total Posts : 812
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: online
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/07 00:01:18 (permalink)
    0
    NKurzman
    I do not know about current compilers but I have seen the char command[50] be interpreted as char *, But give a warning if it is not pointing at a 50 char array.   I do not know what GCC does by default, or will any options for this syntax.




    i would like to say.. implementation defined? i seem to remember that you pass the size of multisize arrays so that the compiler can do pointer math appropriately
    #13
    lamdaelectronics
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2010/07/23 04:04:41
    • Location: 0
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/07 01:14:15 (permalink)
    0
    I would suggest to re-write your UART routine, transferring it to Interrupts.
    And one thing more: change the optimisation to 0, if you want to use the debugger stepping everything.
    #14
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11395
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/07 12:07:14 (permalink)
    +1 (1)
    i would like to say.. implementation defined? i seem to remember that you pass the size of multisize arrays so that the compiler can do pointer math appropriately

     
    It's not implementation defined.  When using char x[some constant] in a function declaration, the "[some constant]" part always becomes *, and the constant is ignored.
     
    Multi-dimension arrays are different in that you have to tell the compiler all but one of the dimensions, e.g.,
    void func(int (*p)[3]);

    #15
    1and0
    Access is Denied
    • Total Posts : 9864
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/07 12:25:57 (permalink)
    +1 (1)
    jtemples
    Multi-dimension arrays are different in that you have to tell the compiler all but one of the dimensions, e.g.,
    void func(int (*p)[3]);


    Multi-dimension arrays are different in that you have to tell the compiler all but the first dimension.
    #16
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11395
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Passing a char array to a function but only 1st element getting sent 2019/11/07 12:41:27 (permalink)
    0
     all but the first dimension

     
    Yes, that's a more precise way of putting it.
    #17
    Jump to:
    © 2019 APG vNext Commercial Version 4.5