RT-Thread 自定义AT指令

RT-Thread的AT组件,可以很方便的对AT指令进行解析。
如EC200x模块,使用AT+CMGR=1指令,可以读取index=1的短信内容:
在这里插入图片描述
通过AT命令可以看到接收到的数据,因为这里开启了回传模式,所以收输入的AT+CMGR=1一并显示出来了:
前两行是空行,第三行是指令回复,第四行是数据内容,第五行是空行,第六行是OK。

======== Welcome to using RT-Thread AT command client cli ========
Cli will forward your command to server port(uart2). Press 'ESC' to exit.
AT+CMGR=1


+CMGR: 1,,157
0891683110802105F0640AA001969055990008027081016002238B060804C9DC0201301075188083519C884C301160A867094E007B148BDD8D394F1860E0674376CA53735C065230671FFF018BF7767B5F55519C884C638C94F6FF0C5B8C62104EFB610F4E007B14515A8D39300175358D3930014E2496697F348D39540E537353EF988653D667009AD80032003051438BDD8D39595652B1FF0C00310030003000254E2D5956

OK

示例程序如下:

/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-08-26     Shine       the first version
 */
/*
 * 程序清单:AT Client 发送命令并接收响应例程
 */

#include <rtthread.h>
#include <at.h>   /* AT 组件头文件 */

#define DBG_TAG "app.sms"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
int test(int argc, char**argv)
{
    
    
    at_response_t resp = RT_NULL;

    /* 创建响应结构体,设置最大支持响应数据长度为 512 字节,响应数据行数无限制,超时时间为 5 秒 */
    resp = at_create_resp(512, 0, rt_tick_from_millisecond(5000));
    if (!resp)
    {
    
    
        LOG_E("No memory for response structure!");
        return -RT_ENOMEM;
    }

    /* 发送 AT 命令并接收 AT Server 响应数据,数据及信息存放在 resp 结构体中 */
    if (at_exec_cmd(resp, "AT+CMGR=1") != RT_EOK)
    {
    
    
        LOG_E("AT client send commands failed, response error or timeout !");
        return 0;
    }

    LOG_D("line_num=%d", resp->line_counts);
    for (int i = 0; i < (int) resp->line_counts; i++)
    {
    
    
        LOG_D("%s", at_resp_get_line(resp, i + 1));
    }

    /* 删除响应结构体 */
    at_delete_resp(resp);

    return RT_EOK;
}

/* 输出 at_Client_send 函数到 msh 中 */
MSH_CMD_EXPORT(test, AT Client send commands to AT Server and get response data);

打印效果如下:

msh />test
msh />[27720] D/app.sms: line_num=6
[27720] D/app.sms: 
[27720] D/app.sms: 
[27720] D/app.sms: +CMGR: 1,,157
[27720] D/app.sms: 0891683110802105F0640AA001969055990008027081016002238B060804C9DC0201301075188083519C884C301160A867094E007B148BDD8D394F1860E0674376CA53735C065230671FFF018BF7767B5F55519C884C638C94F6FF0C5B8C62104EFB610F4E007B14515A8D39300175358D39300
[27720] D/app.sms: 
[27720] D/app.sms: OK

猜你喜欢

转载自blog.csdn.net/qq_27508477/article/details/108261180
今日推荐