【问题标题】:Send SMS GSM Click发送短信 GSM 点击
【发布时间】:2019-07-09 09:08:14
【问题描述】:

“我正在使用 Mikroe 的示例代码进行 GSM 点击,并添加了几行代码来发送示例文本消息。但是我遇到了“sendSMSmsg”函数的问题,其中 msg 是未声明的标识符。我试图在代码之上定义它,但它仍然不起作用。感谢任何帮助。"

"这里使用了 mikroelektronika 开发的 GSM 点击库"

/*
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();
    }
}

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

“表达式中未声明的标识符‘msg’”

【问题讨论】:

  • char msg[]; ?想一想,这应该是什么意思?你的意思是声明一个数组?指针?一个角色?
  • 我想我需要声明一个指针,使用 'char *msg' 是一个不错的选择吗?对不起,我真的是编程新手,所以我犯了很多错误
  • 您也需要有内存才能写入数据。像 128 个字符。或更多,或更少。取决于你要写多少字符。使用strcat(msg, "Test message 1") 你写strlen("Test message 1") 字符加上一个终止零字节。你可以前。 chat msg[512];。请获取good C book 并了解 C。stackoverflow 不是学习网站。您可以在网上找到很多关于 C 中的指针和数组的教程。

标签: c pic32 mikroc


【解决方案1】:

你有这个代码:

char msg[];

...

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

第一行定义了一个指针:

char msg[];

随后的行写入内存中的随机位置并破坏它:

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

写入未分配内存的结果未定义 = 任何事情都可能发生,无论是期望的还是不期望的、预期的还是意外的。


修复此问题后,您可以继续调试。

【讨论】:

  • 对不起。我不知道那是什么意思。我正在尝试学习这一点,但在其他 GSM 模块上使用:// 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)
  • The first line defines a pointer: - 不,chat msg[]; 无效,根本不应该编译。见前。 this answer.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
相关资源
最近更新 更多