-1

"I'm using the example code of Mikroe for the GSM click with a few additional lines of code to send a sample text message. But I'm having problems with the 'sendSMSmsg' function where msg is undeclared identifier. I tried to define it on top of the code but it still doesn't work. Any help is appreciated."

"This uses GSM click library developed by mikroelektronika"

/*
Example for GSM Click

    Date          : Jan 2018.
    Author        : MikroE Team

Test configuration PIC32 :

    MCU                : P32MX795F512L
    Dev. Board         : EasyPIC Fusion v7
    PIC32 Compiler ver : v4.0.0.0

---

Description :

The application is composed of three sections :

- System Initialization - Initializes all necessary GPIO pins, UART used for
the communcation with GSM module and UART used for infromation logging
- Application Initialization - Initializes driver, power on module and sends few
command for the default module configuration
- Application Task - running in parallel core state machine and checks for call flag. 
If call detected parser will hang up call.

Additional Functions :

All additional functions such as timer initialization and default handler. 

Notes :

- Echo must be turned off for succefull parsing. ( ATE0 )
- UART polling works much better with HFC enabled.
- In case of usage of other MCUs Timer initialization must be adjusted according to your MCU.

*/

#define __GSM__

#include "Click_GSM_types.h"
#include "Click_GSM_config.h"
#include "Click_GSM_timer.h"
#include <stdbool.h>

static uint8_t callFlag;
char uart2Buf;
bool rc;



void gsm_default_handler( uint8_t *rsp, uint8_t *evArgs )
{
    mikrobus_logWrite( rsp, _LOG_TEXT );

//  SKIP <CR> and <LF>
    if (0 == strncmp("RING", rsp + 2, 4))
    {
        callFlag = 1;
    }
}

void systemInit()    //initialize the mikrobus pins as GPIO and UART pins
{
    callFlag = 0;

    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_AN_PIN, _GPIO_INPUT );              //enable AN pin as input
    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_PWM_PIN, _GPIO_INPUT );             //enable PWM pin as input
    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_INT_PIN, _GPIO_INPUT );             //enable INT pin as input
    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_RST_PIN, _GPIO_OUTPUT );            //enable RST pin as output
    mikrobus_gpioInit( _MIKROBUS1, _MIKROBUS_CS_PIN, _GPIO_OUTPUT );             //enable CS pin as output
    mikrobus_uartInit( _MIKROBUS1, &_GSM_UART_CFG[0] );                          //enable UART communication
    mikrobus_logInit( _LOG_USBUART_B, 9600 );                                    //initialize the log with baud rate
}

void WriteString(char *strBuf)    //Write String
{
    while(strBuf)
    {
     UART2_Write(*strBuf++);
    }
    UART2_Write(0x0D);
}

void sendSMSmsg(char *msg)
{
    gsm_cmdSingle("AT+CMGS=\"+639054186435\"");   //dial it up
    WriteString(msg);                             //send the message
    UART2_Write(0x1A);                            //Ctrl-Z
    UART2_Write(0x0D);                            //CR
    rc = false;                                   //wait for OK

    do
    {
     if (UART2_Data_Ready()==1)
     {
         UART2_Read_Text(uart2Buf,"OK", 255);
         rc = true;
     }
    }while (rc==false);
}

void applicationInit()
{   char msg[];
// TIMER INIT
    gsm_configTimer();

// DRIVER INIT
    gsm_uartDriverInit((T_GSM_P)&_MIKROBUS1_GPIO, (T_GSM_P)&_MIKROBUS1_UART);
    gsm_coreInit( gsm_default_handler, 1500 );

// MODULE POWER ON
    gsm_hfcEnable( true );
    gsm_modulePower( true );

// MODULE INIT
    gsm_cmdSingle( "AT" );            //knock on the door
    gsm_cmdSingle( "AT" );
    gsm_cmdSingle( "AT" );
    gsm_cmdSingle( "ATE0" );          //turn off echo
    gsm_cmdSingle( "AT+CMGF=1" );     //text messaging

//SMS Message
   msg[0] = '\0';
   strcat(msg, "Test message 1");
   strcat(msg, "\r\n");               //add new line (CR+LF)
}



void applicationTask()
{
// CORE STATE MACHINE
    gsm_process();

    if (0 != callFlag)
    {
        gsm_cmdSingle( "ATH" );
        Delay_ms( 3000 );
        sendSMSmsg(msg);

        callFlag = 0;
    }
}

void main()
{
    systemInit();
    applicationInit();

    while (1)
    {
        applicationTask();
    }
}

/* ----------------------

"undeclared identifier 'msg' in expression"

Dorokun192
  • 63
  • 7
  • `char msg[];` ? Think about, what is it supposed to mean? Do you mean to declare an array? A pointer? A character? – KamilCuk Jul 09 '19 at 09:12
  • I think I need to declare a pointer, is using 'char *msg' a good option? I'm sorry, but I'm really new to programming, that is why I make a lot of mistakes – Dorokun192 Jul 10 '19 at 02:27
  • You need to have memory to write the data too. Like 128 characters. Or more, Or less. Depending how much you characters to write. With `strcat(msg, "Test message 1")` you write `strlen("Test message 1")` characters plus _a terminating zero byte_. You could ex. `chat msg[512];`. Please grab a [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and learn about C. stackoverflow is not a learning site. You can find many tutorials, even online, about pointers and arrays in C. – KamilCuk Jul 10 '19 at 05:39

1 Answers1

0

You have this code:

char msg[];

...

//SMS Message
msg[0] = '\0';
strcat(msg, "Test message 1");
strcat(msg, "\r\n");               //add new line (CR+LF)

The first line defines a pointer:

char msg[];

The subsequent lines write in random locations in memory and corrupt it:

msg[0] = '\0';
strcat(msg, "Test message 1");
strcat(msg, "\r\n");               //add new line (CR+LF)

The result of writing to unallocated memory is undefined = anything can happen, desired or undesired, expected or unexpected.


After you fix this, you can continue debugging.

virolino
  • 2,073
  • 5
  • 21
  • I'm sorry. I don't know what that means. I'm trying to learn this but on other GSM modules this is used: `// Compose Status SMS unsigned ComposeMessage(char* Message){ unsigned int adc_value1 = 0; float Celsius_format, Fahrenheit_format; RC1IE_bit = 0; // Disable Rx1 intterupts Message[0] = '\0'; //================================================================ // SMS header strcat(Message, "EasyPIC7 Info:"); strcat(Message, "\r\n"); // Add new line (CR + LF)` – Dorokun192 Jul 10 '19 at 02:37
  • `The first line defines a pointer:` - no, the `chat msg[];` is just invalid and shouldn't compile at all. See ex. [this answer](https://stackoverflow.com/a/2717726/9072753). – KamilCuk Jul 10 '19 at 05:38