高级
30 分钟

使用 LEXI-R10401D 和 PIC18LF4515 实现适用于美洲地区的 LTE Cat 1bis 通信

面向美洲物联网解决方案的 LTE Cat 1bis 连接,集成地理定位功能

4G LTE 3 Click (for North America) with EasyPIC v7

已发布 6月 18, 2025

点击板

4G LTE 3 Click (for North America)

开发板

EasyPIC v7

编译器

NECTO Studio

微控制器单元

PIC18LF4515

支持室内定位的 LTE Cat 1bis 连接,适用于北美地区的资产追踪、车载远程信息处理和可穿戴设备应用

A

A

硬件概览

它是如何工作的?

4G LTE 3 Click(适用于北美)基于 u-blox 的 LEXI-R10401D 单模 LTE Cat 1bis 模块,专为在美国地区需要可靠连接、中等数据速率和广泛网络覆盖的应用而设计。该模块符合专业级标准,遵循 u-blox 的质量认证政策并满足 AEC-Q104 标准。它支持多个 LTE FDD 频段,包括 2、4、5、12、13、14、66 和 71,可在多个地区网络中提供广泛兼容性和出色性能。此外,LEXI-R10401D 通过了 PTCRB、GCF、FCC、ISED、AT\&T、Verizon 和 FirstNet 等认证,可在各支持市场中部署。除了基本的 LTE 功能外,模块还集成了嵌入式 Wi-Fi 扫描功能,可用于定位附近 Wi-Fi 热点,从而增强室内定位能力,并支持 u-blox 的 CellLocate® 地理定位服务。该模块体积紧凑、集成度高,支持中等速率的数据连接,下载速度最高可达 10Mbps,上传速度可达 5Mbps,同时保持极低的功耗,特别适用于注重成本控制但仍需稳定数据连接的场景,如资产追踪、远程信息处理、医疗设备和可穿戴设备等。模块通过 UART 接口与主控 MCU 通信,

使用标准 UART RX 和 TX 引脚,以及硬件流控引脚(CTS/RTS/RI)以实现高效数据传输,默认通信速率为 115200bps,支持基于 AT 指令的数据交互。板上还配备了一个 USB Type-C 接口,用于供电和数据传输,符合 USB 2.0 标准,最大数据速率为 480Mbit/s(仅外围设备模式)。此外,板背面带有一个名为 USB BOOT 的 USB 固件升级开关,可通过将开关拨至位置 1 来启用固件升级功能,位置 0 则为正常工作状态,确保升级过程简单直观。4G LTE 3 Click 板还提供多项增强功能以提高可用性与控制能力。板载 PWR 按钮可实现模块的手动开关机,RESET 按钮可用于快速重置模块,这些操作也可通过 mikroBUS™ 插针上的 PWR 与 RST 信号进行数字控制,从而提供更多灵活性。板载未焊接的 6 个 GPIO 扩展针脚为用户提供额外功能配置空间,同时板上还预留了 TP1 和 TP2 两个测试点,便于诊断使用,尤其适用于同时使用 USB 与主 UART 接口的场景。板上配有两个状态指示灯用于实时显示模块状态。红色 NET LED 表示

网络状态,当 LED 慢速闪烁时表示已注册至网络,正常闪烁表示尚未注册网络,快速闪烁表示正在进行数据传输;当该指示灯熄灭时表示模块已关机或处于省电模式(PSM)。黄色 STAT LED 表示模块电源状态,模块关闭时指示灯熄灭,模块上电或固件启动完成时指示灯点亮。通信天线方面,该板配有一个 u.Fl 天线连接器,可连接 MIKROE 提供的 LTE 扁平旋转天线,并通过 IPEX-SMA 线缆实现灵活的天线连接。另配有 micro SIM 卡座,兼容 1.8V 与 3.0V 的 uSIM 卡,用户可根据实际需求选择合适的运营商服务。此 Click 板支持 3.3V 与 5V 逻辑电平,可通过 VCC SEL 跳线选择。由于 LEXI-R10401D 工作电压为 3.8V,板上使用了 TXB0106 逻辑电平转换器以实现电平兼容,从而确保与不同逻辑电平的 MCU 正确通信。该 Click 板还配有配套的软件库与示例代码,提供易用的函数接口,可供进一步开发参考使用。

4G LTE 3 Click (for North America) hardware overview image

功能概述

开发板

EasyPIC v7 是第七代 PIC 开发板,专为快速开发嵌入式应用而设计。它支持 Microchip 的广泛 8 位 PIC 微控制器,并具备一系列独特功能,如强大的板载 mikroProg 程序员和通过 USB-B 的在线电路调试器。开发板布局合理,设计周到,确保最终用户在一个地方可以找到所有必需的元素,如开关、按钮、指示器、连接器等。EasyPIC v7 提供四种不同的连接器用于每个端口,使您能够比以往更高效地连接附件板、传感器和自定义电子设备。EasyPIC v7 开发板的每个

部分都包含了使同一板块运行最高效的必要组件。一个集成的 mikroProg、一个快速的 USB 2.0 程序器带有 mikroICD 硬件在线电路调试器,提供许多宝贵的编程/调试选项并与 Mikroe 软件环境无缝集成。此外,它还包括一个为开发板提供的干净且调节过的电源模块。它可以使用各种外部电源,包括外部 12V 电源、7-23V 交流或 9-32V 直流通过 DC 连接器/螺丝端子,以及通过 USB Type-B(USB-B)连接器的电源。通信选项如 USB-UART 和 RS-232 也包括在

内,还有广受好评的 mikroBUS™ 标准、三种显示选项(七段、图形和基于字符的 LCD),以及几种不同的 DIP 插座。这些插座覆盖了广泛的 8 位 PIC MCU,包括 PIC10F, PIC12F, PIC16F, PIC16Enh, PIC18F, PIC18FJ, 和 PIC18FK 系列。EasyPIC v7 是 Mikroe 快速开发生态系统的一个重要组成部分,由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作和开发的许多方面。

EasyPIC v7 horizontal image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC

MCU 内存 (KB)

48

硅供应商

Microchip

引脚数

40

RAM (字节)

3968

你完善了我!

配件

LTE Flat Rotation Antenna 是增强 3G/4G LTE 设备性能的多功能选择。凭借 700-2700MHz 的宽频率范围,它确保在全球主要蜂窝频段上的最佳连接。该平板天线采用 SMA 公头连接器,便于直接连接到设备或 SMA 模块连接器。其亮点之一是可调角度,可按 45⁰ 增量(0⁰/45⁰/90⁰)设置,允许您微调天线的方向以获得最佳信号接收。具有 50Ω 阻抗和 <2.0:1 的电压驻波比 (VSWR),此天线确保可靠高效的连接。其 5dB 增益、垂直极化和全向辐射图形增强了信号强度,适用于各种应用。天线长度为 196mm,宽度为 38mm,提供紧凑但有效的解决方案来改善您的连接。最大输入功率为 50W,能够满足各种设备的需求。

4G LTE 3 Click (for North America) accessories 1 image

IPEX-SMA 电缆是一种射频 (RF) 电缆组件。"IPEX" 指的是 IPEX 连接器,这是一种常用于小型电子设备的微型同轴连接器。"SMA" 代表 SubMiniature Version A,是另一种常用于射频应用的同轴连接器。IPEX-SMA 电缆组件的一端是 IPEX 连接器,另一端是 SMA 连接器,使其能够连接使用这些特定连接器的设备或组件。这些电缆常用于 WiFi 或蜂窝天线、GPS 模块以及其他需要可靠且低损耗连接的射频通信系统。

4G LTE 3 Click (for North America) accessories 2 image

使用的MCU引脚

mikroBUS™映射器

Module Power-ON
RA2
AN
Reset / ID SEL
RE1
RST
UART RTS / ID COMM
RE0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
RC0
PWM
UART CTS
RB0
INT
UART TX
RC6
TX
UART RX
RC7
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

4G LTE 3 Click (for North America) Schematic schematic

一步一步来

项目组装

EasyPIC v7 front image hardware assembly

从选择您的开发板和Click板™开始。以EasyPIC v7作为您的开发板开始。

EasyPIC v7 front image hardware assembly
GNSS2 Click front image hardware assembly
MCU DIP 40 hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyPIC v7 Access MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto DIP image step 7 hardware assembly
EasyPIC PRO v7a Display Selection Necto Step hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image 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”替换为要显示的参数。

软件支持

库描述

4G LTE 3 Click (for North America) 演示应用程序使用 NECTO Studio开发,确保与 mikroSDK 的开源库和工具兼容。该演示设计为即插即用,可与所有具有 mikroBUS™ 插座的 开发板、入门板和 mikromedia 板完全兼容,用于快速实现和测试。

示例描述
本应用示例展示了设备通过标准 "AT" 命令连接至网络并发送短信或 TCP/UDP 消息的能力。

关键功能:

  • c4glte3na_cfg_setup - 初始化 Click 配置结构体。

  • c4glte3na_init - 初始化所有必要的引脚与外设。

  • c4glte3na_set_sim_apn - 设置 SIM 卡的 APN 接入点名称。

  • c4glte3na_send_sms_text - 向指定手机号发送短信文本。

  • c4glte3na_cmd_run - 向模块发送指定的 AT 命令。

应用初始化
初始化驱动程序与日志记录器。

应用任务
任务流程被分为多个阶段:

  • C4GLTE3NA_POWER_UP: 上电模块,执行出厂重置,并读取系统信息。

  • C4GLTE3NA_CONFIG_CONNECTION: 配置模块以便连接至网络。

  • C4GLTE3NA_CHECK_CONNECTION: 等待模块通过 CEREG 命令注册到网络,并检测信号强度。

  • C4GLTE3NA_CONFIG_EXAMPLE: 为所选示例配置模块。

  • C4GLTE3NA_EXAMPLE: 根据所选演示模式,发送短信(PDU 或 TXT 模式)或 TCP/UDP 消息,默认设置为 TCP/UDP 示例。

开源

代码示例

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

/*!
 * @file main.c
 * @brief 4G LTE 3 NA 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 and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - C4GLTE3NA_POWER_UP:
 * Powers up the device, performs a factory reset and reads system information.
 *
 *  - C4GLTE3NA_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - C4GLTE3NA_CHECK_CONNECTION:
 * Waits for the network registration indicated via CEREG command and then checks 
 * the signal quality report.
 *
 *  - C4GLTE3NA_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - C4GLTE3NA_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 c4glte3na_clear_app_buf ( void )
 * - static void c4glte3na_log_app_buf ( void )
 * - static err_t c4glte3na_process ( c4glte3na_t *ctx )
 * - static err_t c4glte3na_read_response ( c4glte3na_t *ctx, uint8_t *rsp )
 * - static err_t c4glte3na_power_up ( c4glte3na_t *ctx )
 * - static err_t c4glte3na_config_connection ( c4glte3na_t *ctx )
 * - static err_t c4glte3na_check_connection ( c4glte3na_t *ctx )
 * - static err_t c4glte3na_config_example ( c4glte3na_t *ctx )
 * - static err_t c4glte3na_example ( c4glte3na_t *ctx )
 *
 * @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.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "c4glte3na.h"
#include "generic_pointer.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                        ""              // 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                     "4G LTE 3 NA 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
{
    C4GLTE3NA_POWER_UP = 1,
    C4GLTE3NA_CONFIG_CONNECTION,
    C4GLTE3NA_CHECK_CONNECTION,
    C4GLTE3NA_CONFIG_EXAMPLE,
    C4GLTE3NA_EXAMPLE

} c4glte3na_app_state_t;

/**
 * @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 c4glte3na_app_state_t app_state = C4GLTE3NA_POWER_UP;

static c4glte3na_t c4glte3na;
static log_t logger;

/**
 * @brief 4G LTE 3 NA clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void c4glte3na_clear_app_buf ( void );

/**
 * @brief 4G LTE 3 NA log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void c4glte3na_log_app_buf ( void );

/**
 * @brief 4G LTE 3 NA data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_process ( c4glte3na_t *ctx );

/**
 * @brief 4G LTE 3 NA read response function.
 * @details This function waits for a response message, reads and displays it on the USB UART.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @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.
 * @note None.
 */
static err_t c4glte3na_read_response ( c4glte3na_t *ctx, uint8_t *rsp );

/**
 * @brief 4G LTE 3 NA power up function.
 * @details This function powers up the device, performs a factory reset and reads system information.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_power_up ( c4glte3na_t *ctx );

/**
 * @brief 4G LTE 3 NA config connection function.
 * @details This function configures and enables connection to the specified network.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_config_connection ( c4glte3na_t *ctx );

/**
 * @brief 4G LTE 3 NA check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_check_connection ( c4glte3na_t *ctx );

/**
 * @brief 4G LTE 3 NA config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_config_example ( c4glte3na_t *ctx );

/**
 * @brief 4G LTE 3 NA example function.
 * @details This function executes SMS or TCP/UDP depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #c4glte3na_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t c4glte3na_example ( c4glte3na_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c4glte3na_cfg_t c4glte3na_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.
    c4glte3na_cfg_setup( &c4glte3na_cfg );
    C4GLTE3NA_MAP_MIKROBUS( c4glte3na_cfg, MIKROBUS_1 );
    if ( UART_ERROR == c4glte3na_init( &c4glte3na, &c4glte3na_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

    app_state = C4GLTE3NA_POWER_UP;
    log_printf( &logger, ">>> APP STATE - POWER UP <<<\r\n\n" );
}

void application_task ( void ) 
{
    switch ( app_state )
    {
        case C4GLTE3NA_POWER_UP:
        {
            if ( C4GLTE3NA_OK == c4glte3na_power_up( &c4glte3na ) )
            {
                app_state = C4GLTE3NA_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3NA_CONFIG_CONNECTION:
        {
            if ( C4GLTE3NA_OK == c4glte3na_config_connection( &c4glte3na ) )
            {
                app_state = C4GLTE3NA_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3NA_CHECK_CONNECTION:
        {
            if ( C4GLTE3NA_OK == c4glte3na_check_connection( &c4glte3na ) )
            {
                app_state = C4GLTE3NA_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3NA_CONFIG_EXAMPLE:
        {
            if ( C4GLTE3NA_OK == c4glte3na_config_example( &c4glte3na ) )
            {
                app_state = C4GLTE3NA_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case C4GLTE3NA_EXAMPLE:
        {
            c4glte3na_example( &c4glte3na );
            break;
        }
        default:
        {
            log_error( &logger, " APP STATE." );
            break;
        }
    }
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

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

static void c4glte3na_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 c4glte3na_process ( c4glte3na_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = c4glte3na_generic_read( ctx, rx_buf, PROCESS_BUFFER_SIZE );
    if ( ( rx_size > 0 ) && ( rx_size <= APP_BUFFER_SIZE ) ) 
    {
        if ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) 
        {
            overflow_bytes = ( app_buf_len + rx_size ) - APP_BUFFER_SIZE;
            app_buf_len = APP_BUFFER_SIZE - rx_size;
            memmove ( app_buf, &app_buf[ overflow_bytes ], app_buf_len );
            memset ( &app_buf[ app_buf_len ], 0, overflow_bytes );
        }
        for ( rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ app_buf_len++ ] = rx_buf[ rx_cnt ];
            }
        }
        return C4GLTE3NA_OK;
    }
    return C4GLTE3NA_ERROR;
}

static err_t c4glte3na_read_response ( c4glte3na_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS    120000
    uint32_t timeout_cnt = 0;
    c4glte3na_clear_app_buf ( );
    c4glte3na_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, C4GLTE3NA_RSP_ERROR ) ) )
    {
        c4glte3na_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            c4glte3na_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return C4GLTE3NA_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    c4glte3na_process( ctx );
    c4glte3na_log_app_buf( );
    if ( strstr( app_buf, rsp ) )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        return C4GLTE3NA_OK;
    }
    else if ( strstr( app_buf, C4GLTE3NA_RSP_ERROR ) )
    {
        log_error( &logger, " CMD!" );
        return C4GLTE3NA_ERROR_CMD;
    }
    log_error( &logger, " Unknown!" );
    return C4GLTE3NA_ERROR_UNKNOWN;
}

static err_t c4glte3na_power_up ( c4glte3na_t *ctx )
{
    err_t error_flag = C4GLTE3NA_OK;

    uint8_t power_state = C4GLTE3NA_POWER_STATE_OFF;
    for ( ; ; )
    {
        c4glte3na_process( ctx );
        c4glte3na_log_app_buf ( );
        c4glte3na_clear_app_buf ( );
        // Wake up UART interface
        c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_AT );

        log_printf( &logger, ">>> Check communication.\r\n" );
        c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_AT );
        if ( ( ( C4GLTE3NA_OK == c4glte3na_process( ctx ) ) && strstr( app_buf, C4GLTE3NA_RSP_OK ) ) )
        {
            power_state = C4GLTE3NA_POWER_STATE_ON;
            break;
        }
        else if ( C4GLTE3NA_POWER_STATE_OFF == power_state )
        {
            power_state = C4GLTE3NA_POWER_STATE_ON;
            log_printf( &logger, ">>> Power up device.\r\n" );
            c4glte3na_set_power_state ( ctx, C4GLTE3NA_POWER_STATE_ON );
        }
        else if ( C4GLTE3NA_POWER_STATE_ON == power_state )
        {
            power_state = C4GLTE3NA_POWER_STATE_OFF;
            log_printf( &logger, ">>> Power down device.\r\n" );
            c4glte3na_set_power_state ( ctx, C4GLTE3NA_POWER_STATE_OFF );
        }
    }
    c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_AT );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    
    log_printf( &logger, ">>> Factory reset.\r\n" );
    c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_FACTORY_RESET );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_GET_SW_VERSION );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    c4glte3na_cmd_run( ctx, C4GLTE3NA_CMD_GET_SERIAL_NUM );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    return error_flag;
}

static err_t c4glte3na_config_connection ( c4glte3na_t *ctx )
{
    err_t error_flag = C4GLTE3NA_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Configure network status LED.\r\n" );
    #define NETWORK_STATUS_LED "14,2"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_GPIO_CONFIG, NETWORK_STATUS_LED );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Configure module status LED.\r\n" );
    #define MODULE_STATUS_LED "15,10"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_GPIO_CONFIG, MODULE_STATUS_LED );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    c4glte3na_set_sim_apn( &c4glte3na, SIM_APN );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_SET_MODULE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_EPS_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    c4glte3na_cmd_set( ctx, C4GLTE3NA_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
#endif
    return error_flag;
}

static err_t c4glte3na_check_connection ( c4glte3na_t *ctx )
{
    err_t error_flag = C4GLTE3NA_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    #define CONNECTED "+CEREG: 2,1"
    c4glte3na_cmd_get ( &c4glte3na, C4GLTE3NA_CMD_EPS_NETWORK_REGISTRATION );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        c4glte3na_cmd_run ( &c4glte3na, C4GLTE3NA_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    }
    else
    {
        error_flag = C4GLTE3NA_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t c4glte3na_config_example ( c4glte3na_t *ctx )
{
    err_t error_flag = C4GLTE3NA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    c4glte3na_cmd_set( &c4glte3na, C4GLTE3NA_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    c4glte3na_cmd_set( &c4glte3na, C4GLTE3NA_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    c4glte3na_cmd_set( &c4glte3na, C4GLTE3NA_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
#endif
    return error_flag;
}

static err_t c4glte3na_example ( c4glte3na_t *ctx )
{
    err_t error_flag = C4GLTE3NA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    uint8_t * __generic_ptr socket_num_buf = 0;
    uint8_t tcp_socket_num[ 2 ] = { 0 };
    uint8_t udp_socket_num[ 2 ] = { 0 };
    log_printf( &logger, ">>> Create TCP socket.\r\n" );
    #define TCP_PROTOCOL "6"
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CREATE_SOCKET, TCP_PROTOCOL );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    socket_num_buf = strstr( app_buf, C4GLTE3NA_URC_CREATE_SOCKET ) + strlen ( C4GLTE3NA_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        tcp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Create UDP socket.\r\n" );
    #define UDP_PROTOCOL "17"
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CREATE_SOCKET, UDP_PROTOCOL );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    socket_num_buf = strstr( app_buf, C4GLTE3NA_URC_CREATE_SOCKET ) + strlen ( C4GLTE3NA_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        udp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    // 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 );

    log_printf( &logger, ">>> Write message to TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    c4glte3na_cmd_set( &c4glte3na, C4GLTE3NA_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );

    log_printf( &logger, ">>> Write message to UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    c4glte3na_cmd_set( &c4glte3na, C4GLTE3NA_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CLOSE_SOCKET, tcp_socket_num );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    c4glte3na_cmd_set ( &c4glte3na, C4GLTE3NA_CMD_CLOSE_SOCKET, udp_socket_num );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    c4glte3na_cmd_get( &c4glte3na, C4GLTE3NA_CMD_SELECT_SMS_FORMAT );
    error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        c4glte3na_send_sms_pdu( &c4glte3na, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        c4glte3na_send_sms_text ( &c4glte3na, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= c4glte3na_read_response( ctx, C4GLTE3NA_RSP_OK );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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