初学者
10 分钟

使用GL865-QUAD和PIC32MZ2048EFH100体验无界限的通信

GSM蜂窝网络通信解决方案

GSM Click with Flip&Click PIC32MZ

已发布 6月 24, 2024

点击板

GSM Click

开发板

Flip&Click PIC32MZ

编译器

NECTO Studio

微控制器单元

PIC32MZ2048EFH100

改善设备连接性,采用紧凑型2G蜂窝解决方案,提供全面的通信选项,并通过嵌入式Python脚本解释器实现无缝定制。

A

A

硬件概览

它是如何工作的?

GSM Click 基于 Telit 的 GL865-QUAD GSM/GPRS 嵌入式模块。GL865-QUAD 模块符合 3GPP Release 4 的 GSM/GPRS 协议栈,并符合欧盟 eCall 指令。它支持四频 GSM/GPRS,并覆盖 850/900/1800/1900 MHz 频率,可在全球范围内使用。广泛的通信协议和连接选项,加上嵌入式 Phyton 脚本解释器和 Telit 的简单 AT 命令界面通过 UART 总线,使得这款 Click board™ 成为 M2M 应用广泛的完整解决方案。该模块由几个内部模块或部分组成,例如天线切换部分、射频收发器部分、内存、电源管理,以及最重要的 - 蜂窝基带处理器。Click board™ 背面的微型 SIM 卡插槽用于安装微型 SIM 卡。未安装有效 SIM 卡则无法使用此设备,因为需要连接到蜂窝网络。支持 1.8V 和 3V 的 SIM 卡类型。您还需要一个合适的 SMA 天线,MIKROE 也提供。有两个 

LED 指示灯:TXD 指示传输活动/SIM 卡存在,STAT 指示设备状态,显示几种闪烁模式。关于 RXD  LED,可能需要配置 GPIO 引脚;然而,提供的 click 库提供了初始化模块的函数,并允许与 GSM click 轻松通信。GL865-QUAD 提供广泛的音频功能,包括半速率、全速率、增强全速率和自适应多速率语音编解码器,卓越的回声消除和降噪,多个预编程的音频配置文件,可通过 AT 命令完全配置,以及 DTMF 音调生成。音频部分集成在模块中,只需要少量外部组件。可通过 4 极 3.5 毫米音频插孔连接耳机。如前所述,此模块具有嵌入式 Phyton 脚本解释器。它允许加载用 Phyton 编写的脚本,并为用户脚本提供 1.9 MB 的非易失性内存以及 1 MB 的 RAM 供 Python 引擎使用。GSM Click 通过标准的 2 线 UART 接口与主 MCU 通信,使用常用的 UART RX 和 TX 引脚。此

外,GSM Click 还提供 UART 流控 RTS 和 CTS 引脚。UART 接口支持从 300 到 115200bps 的波特率,并具有自动波特率检测功能。电压级别转换由德州仪器的 TXB0106,一个 6 位双向电平移位电压转换器承担。可以通过 RST 引脚重置 GSM 模块。设备上电状态可以通过 PWR 引脚监控。一个很好的功能是干扰检测,可在 GP2 引脚上使用。在关闭 GSM click 时建议遵循特定程序。如果在操作时突然关闭模块,可能会导致数据损坏。应发出停止系统的 AT 命令然后再关闭电源(AT#SYSHALT)。这款 Click board™ 可以通过 I/O Level 跳线选择使用 3.3V 或 5V 逻辑电压级别,这样 3.3V 和 5V 能力的 MCU 都可以正确使用通信线。此外,这款 Click board™ 配备了一个包含易于使用功能的库和示例代码,可用作进一步开发的参考。

GSM Click hardware overview image

功能概述

开发板

Flip&Click PIC32MZ 是一款紧凑型开发板,设计为一套完整的解决方案,它将 Click 板™的灵活性带给您喜爱的微控制器,使其成为实现您想法的完美入门套件。它配备了一款板载 32 位 PIC32MZ 微控制器,Microchip 的 PIC32MZ2048EFH100,四个 mikroBUS™ 插槽用于 Click 板™连接,两个 USB 连接器,LED 指示灯,按钮,调试器/程序员连接器,以及两个与 Arduino-UNO 引脚兼容的头部。得益于创

新的制造技术,它允许您快速构建具有独特功能和特性的小工具。Flip&Click PIC32MZ 开发套件的每个部分都包含了使同一板块运行最高效的必要组件。此外,还可以选择 Flip&Click PIC32MZ 的编程方式,使用 chipKIT 引导程序(Arduino 风格的开发环境)或我们的 USB HID 引导程序,使用 mikroC、mikroBasic 和 mikroPascal for PIC32。该套件包括一个通过 USB 类型-C(USB-C)连接器的干净且调

节过的电源供应模块。所有 mikroBUS™ 本身支持的 通信方法都在这块板上,包括已经建立良好的 mikroBUS™ 插槽、用户可配置的按钮和 LED 指示灯。Flip&Click PIC32MZ 开发套件允许您在几分钟内创建新的应用程序。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作的许多方面。

Flip&Click PIC32MZ double image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

2048

硅供应商

Microchip

引脚数

100

RAM (字节)

524288

你完善了我!

配件

橡胶天线 GSM/GPRS 直角型是我们丰富的 GSM Click boards™ 系列的完美配件。这款专业天线旨在通过令人印象深刻的功能优化您的无线连接。具有广泛的频率范围,覆盖 824-894/1710-1990MHz 或 890-960/1710-1890MHz,它可以处理各种频段,确保无缝且可靠的连接。该天线具有 50 欧姆的阻抗和 2dB 的增益,增强了信号接收和传输。其 70/180MHz 的带宽为多样化的应用提供了灵活性。垂直偏振进一步增强了其性能。该天线的最大输入功率容量为 50W,即使在苛刻条件下也能确保稳健的通信。天线长度为紧凑的 50mm,并配有 SMA 男性连接器,橡胶天线 GSM/GPRS 直角型是您无线通信需求的多功能紧凑解决方案。

GSM Click accessories image

这款标准小型立体声耳机通过其顶级立体声电缆和连接器提供高质量的听觉体验。设计上兼容性广泛,它们能够轻松连接到所有 MIKROE 的 mikromedia 和多媒体板,使其成为您电子项目的理想选择。耳机的额定功率为100mW,可在20Hz至20kHz的宽频范围内提供清晰的音频。耳机拥有100 ± 5dB的灵敏度和32Ω ± 15%的阻抗,确保最佳的音质。Φ15mm的扬声器提供清晰而沉浸的音频体验。这些耳机经济实惠且用途广泛,非常适合测试您的原型设备,为您的项目提供一种负担得起且可靠的音频解决方案。

GSM Click accessories 2 image

使用的MCU引脚

mikroBUS™映射器

Power-On Monitor
RB11
AN
Reset
RE2
RST
UART RTS
RA0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Jammer Detection
RC14
PWM
UART CTS
RD9
INT
UART TX
RE3
TX
UART RX
RG9
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

GSM Click Schematic schematic

一步一步来

项目组装

Flip&Click PIC32MZ front image hardware assembly

从选择您的开发板和Click板™开始。以Flip&Click PIC32MZ作为您的开发板开始。

Flip&Click PIC32MZ front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Flip&Click PIC32MZ MB1 Access - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Flip&Click PIC32MZ MCU step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

这个库包含了GSM Click驱动程序的API。

关键函数:

  • gsm_set_sim_apn - 设置SIM卡的APN。

  • gsm_send_sms_text - 向电话号码发送文本消息。

  • gsm_send_sms_pdu - 以PDU模式向电话号码发送文本消息。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief GSM Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, tests the communication by sending "AT" command, and after that restarts the device.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - GSM_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - GSM_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CREG URC event and then checks
 * the connection status.
 *
 *  - GSM_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
 *
 *  - GSM_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode) or TCP/UDP message.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void gsm_clear_app_buf ( void )
 * - static err_t gsm_process ( void )
 * - static void gsm_error_check( err_t error_flag )
 * - static void gsm_log_app_buf ( void )
 * - static err_t gsm_rsp_check ( uint8_t *rsp )
 * - static err_t gsm_configure_for_connection( void )
 * - static err_t gsm_check_connection( void )
 * - static err_t gsm_configure_for_messages( void )
 * - static err_t gsm_send_message( void )
 *
 * @note
 * In order for the examples to work, user needs to set the APN and SMSC (SMS PDU mode only)
 * of entered SIM card as well as the phone number (SMS mode only) to which he wants to send an SMS.
 * Enter valid values for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "gsm.h"
#include "conversions.h"

// Example selection macros
#define EXAMPLE_TCP_UDP                     0               // Example of sending messages to a TCP/UDP echo server
#define EXAMPLE_SMS                         1               // Example of sending SMS to a phone number
#define DEMO_EXAMPLE                        EXAMPLE_TCP_UDP // Example selection macro

// SIM APN config
#define SIM_APN                             "internet"      // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER_TO_MESSAGE             ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

// TCP/UDP example parameters
#define REMOTE_IP                           "77.46.162.162" // TCP/UDP echo server IP address
#define REMOTE_PORT                         "51111"         // TCP/UDP echo server port

// Message content
#define MESSAGE_CONTENT                     "GSM click board - demo example."

// Application buffer size
#define APP_BUFFER_SIZE                     256
#define PROCESS_BUFFER_SIZE                 256

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    GSM_CONFIGURE_FOR_NETWORK = 1,
    GSM_WAIT_FOR_CONNECTION,
    GSM_CONFIGURE_FOR_EXAMPLE,
    GSM_EXAMPLE

} gsm_example_state_t;

static gsm_t gsm;
static log_t logger;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static err_t error_flag;
static gsm_example_state_t example_state;

/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application
 * buffer and reset its length and counter.
 */
static void gsm_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and
 * appends it to the application buffer.
 * @return @li @c  0 - Some data is read.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_process ( void );

/**
 * @brief Check for errors.
 * @details This function checks for different types of
 * errors and logs them on UART or logs the response if no errors occured.
 * @param[in] error_flag  Error flag to check.
 */
static void gsm_error_check ( err_t error_flag );

/**
 * @brief Logs application buffer.
 * @details This function logs data from application buffer.
 */
static void gsm_log_app_buf ( void );

/**
 * @brief Response check.
 * @details This function checks for response and
 * returns the status of response.
 * @param[in] rsp  Expected response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_rsp_check ( uint8_t *rsp );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable
 * connection to the specified network.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_configure_for_network ( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_check_connection ( void );

/**
 * @brief Configure device for example.
 * @details Configure device for the specified example.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_configure_for_example ( void );

/**
 * @brief Execute example.
 * @details This function executes SMS or TCP/UDP example depending on the DEMO_EXAMPLE macro.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm_example ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    gsm_cfg_t gsm_cfg;  /**< Click config object. */

    /**
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX
     * are defined as HAL_PIN_NC, you will
     * need to define them manually for log to work.
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    gsm_cfg_setup( &gsm_cfg );
    GSM_MAP_MIKROBUS( gsm_cfg, MIKROBUS_1 );
    if ( UART_ERROR == gsm_init( &gsm, &gsm_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    
    gsm_process( );
    gsm_clear_app_buf( );

    // Check communication
    gsm_send_cmd( &gsm, GSM_CMD_AT );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    gsm_error_check( error_flag );
    
    // Restart device
    #define RESTART_DEVICE "1,1"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_CFUN, RESTART_DEVICE );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    gsm_error_check( error_flag );
    
    log_info( &logger, " Application Task " );
    example_state = GSM_CONFIGURE_FOR_NETWORK;
}

void application_task ( void )
{
    switch ( example_state )
    {
        case GSM_CONFIGURE_FOR_NETWORK:
        {
            if ( GSM_OK == gsm_configure_for_network( ) )
            {
                example_state = GSM_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case GSM_WAIT_FOR_CONNECTION:
        {
            if ( GSM_OK == gsm_check_connection( ) )
            {
                example_state = GSM_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case GSM_CONFIGURE_FOR_EXAMPLE:
        {
            if ( GSM_OK == gsm_configure_for_example( ) )
            {
                example_state = GSM_EXAMPLE;
            }
            break;
        }
        case GSM_EXAMPLE:
        {
            gsm_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example state." );
            break;
        }
    }
}

int main ( void ) 
{
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void gsm_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static err_t gsm_process ( void )
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t rx_size = 0;
    rx_size = gsm_generic_read( &gsm, rx_buf, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = app_buf_len;
        if ( ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) && ( app_buf_len > 0 ) ) 
        {
            buf_cnt = APP_BUFFER_SIZE - ( ( app_buf_len + rx_size ) - APP_BUFFER_SIZE );
            memmove ( app_buf, &app_buf[ APP_BUFFER_SIZE - buf_cnt ], buf_cnt );
        }
        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ buf_cnt++ ] = rx_buf[ rx_cnt ];
                if ( app_buf_len < APP_BUFFER_SIZE )
                {
                    app_buf_len++;
                }
            }
        }
        return GSM_OK;
    }
    return GSM_ERROR;
}

static err_t gsm_rsp_check ( uint8_t *rsp )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    gsm_clear_app_buf( );
    gsm_process( );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, GSM_RSP_ERROR ) ) )
    {
        gsm_process( );
        if ( timeout_cnt++ > timeout )
        {
            gsm_clear_app_buf( );
            return GSM_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms( 100 );
    gsm_process( );
    if ( strstr( app_buf, rsp ) )
    {
        return GSM_OK;
    }
    else if ( strstr( app_buf, GSM_RSP_ERROR ) )
    {
        return GSM_ERROR_CMD;
    }
    else
    {
        return GSM_ERROR_UNKNOWN;
    }
}

static void gsm_error_check ( err_t error_flag )
{
    switch ( error_flag )
    {
        case GSM_OK:
        {
            gsm_log_app_buf( );
            break;
        }
        case GSM_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case GSM_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case GSM_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case GSM_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    Delay_ms( 500 );
}

static void gsm_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
}

static err_t gsm_configure_for_network ( void )
{
    err_t func_error = GSM_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    Delay_ms ( 5000 );
    // Deregister from network
    #define DEREGISTER_FROM_NETWORK "2"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_COPS, DEREGISTER_FROM_NETWORK );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Set SIM APN
    gsm_set_sim_apn( &gsm, SIM_APN );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Enable network registartion
    #define ENABLE_REG "2"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_CREG, ENABLE_REG );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Automatic registration
    #define AUTOMATIC_REGISTRATION "0"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_COPS, AUTOMATIC_REGISTRATION );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    Delay_ms ( 3000 );
#endif
    return func_error;
}

static err_t gsm_check_connection ( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    #define CONNECTED "+CREG: 2,1"
    gsm_send_cmd_check ( &gsm, GSM_CMD_CREG );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    gsm_error_check( error_flag );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms( 100 );
        // Check signal quality
        gsm_send_cmd( &gsm, GSM_CMD_CSQ );
        error_flag = gsm_rsp_check( GSM_RSP_OK );
        gsm_error_check( error_flag );
        #define NO_SIGNAL "99,99"
        if ( !strstr( app_buf, NO_SIGNAL ) )
        {
            Delay_ms ( 1000 );
            return error_flag;
        }
    }
    Delay_ms ( 1000 );
    return GSM_ERROR;
#endif
    return GSM_OK;
}

static err_t gsm_configure_for_example ( void )
{
    err_t func_error = GSM_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ACTIVATE_PDP_CONTEXT "1,1"
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SGACT, ACTIVATE_PDP_CONTEXT );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    gsm_send_cmd_with_par( &gsm, GSM_CMD_CMGF, SMS_MODE );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    Delay_ms ( 1000 );
    return func_error;
}

static err_t gsm_example ( void )
{
    err_t func_error = GSM_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    uint8_t tcp_socket_num[ 2 ] = { '1', 0 };
    uint8_t udp_socket_num[ 2 ] = { '2', 0 };

    // Socket settings
    #define SOCKET_CLOSURE_TYPE     "255"
    #define SOCKET_LOCAL_PORT       "1111"
    #define SOCKET_CONN_MODE_CMD    "1"
    
    // Open TCP socket.
    #define TCP_PROTOCOL "0"
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_PROTOCOL );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_CLOSURE_TYPE );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_LOCAL_PORT );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_CONN_MODE_CMD );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SD, cmd_buf );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Open UDP socket.
    #define UDP_PROTOCOL "1"
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_PROTOCOL );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_CLOSURE_TYPE );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_LOCAL_PORT );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, SOCKET_CONN_MODE_CMD );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SD, cmd_buf );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Get message length
    uint8_t message_len_buf[ 10 ] = { 0 };
    uint16_t message_len = strlen( MESSAGE_CONTENT );
    uint16_to_str( message_len, message_len_buf );
    l_trim( message_len_buf );
    r_trim( message_len_buf );

    // Write message to TCP socket
    uint8_t ctrl_z = 0x1A;
    strcpy( cmd_buf, tcp_socket_num );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SSEND, cmd_buf );
    error_flag = gsm_rsp_check( ">" );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    gsm_generic_write ( &gsm, MESSAGE_CONTENT, message_len );
    gsm_generic_write ( &gsm, &ctrl_z, 1 );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Wait for a response message URC
    #define RESPONSE_URC "SRING: "
    strcpy( cmd_buf, RESPONSE_URC );
    strcat( cmd_buf, tcp_socket_num );
    error_flag = gsm_rsp_check( cmd_buf );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Read response message
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SRECV, cmd_buf );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Write message to UDP socket
    strcpy( cmd_buf, udp_socket_num );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SSEND, cmd_buf );
    error_flag = gsm_rsp_check( ">" );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    gsm_generic_write ( &gsm, MESSAGE_CONTENT, message_len );
    gsm_generic_write ( &gsm, &ctrl_z, 1 );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Wait for a response message URC
    strcpy( cmd_buf, RESPONSE_URC );
    strcat( cmd_buf, udp_socket_num );
    error_flag = gsm_rsp_check( cmd_buf );
    func_error |= error_flag;
    gsm_error_check( error_flag );

    // Read response message
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SRECV, cmd_buf );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Close TCP socket
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SH, tcp_socket_num );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    
    // Close UDP socket
    gsm_send_cmd_with_par( &gsm, GSM_CMD_SH, udp_socket_num );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    Delay_ms( 5000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    gsm_send_cmd_check( &gsm, GSM_CMD_CMGF );
    error_flag = gsm_rsp_check( GSM_RSP_OK );
    func_error |= error_flag;
    gsm_error_check( error_flag );
    Delay_ms( 1000 );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        gsm_send_sms_pdu( &gsm, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = gsm_rsp_check( GSM_RSP_OK );
        func_error |= error_flag;
        gsm_error_check( error_flag );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        gsm_send_sms_text ( &gsm, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = gsm_rsp_check( GSM_RSP_OK );
        func_error |= error_flag;
        gsm_error_check( error_flag );
    }
    Delay_ms( 10000 );
    Delay_ms( 10000 );
    Delay_ms( 10000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。