问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

STM32 + 移远EC800 4G通信模块数传

创作时间:
作者:
@小白创作中心

STM32 + 移远EC800 4G通信模块数传

引用
CSDN
1.
https://m.blog.csdn.net/m0_74823452/article/details/144697163

本文将详细介绍如何使用STM32微控制器与移远EC800 4G通信模块进行数据传输。内容包括模块特性介绍、串口配置、模块初始化、TCP连接建立以及数据发送的完整示例。此外,还介绍了使用花生壳进行端口映射的方法,以实现远程访问。

4G模块简述

EC800M-CN 是移远通信(Quectel)推出的一款高性能、超小尺寸的 LTE Cat 1 无线通信模块,专为 M2M(机器对机器)和 IoT(物联网)应用设计。它具有以下主要特点:

  • 通信速率

  • 最大下行速率:10 Mbps

  • 最大上行速率:5 Mbps

  • 封装与设计

  • 超小尺寸:紧凑的设计使其适用于空间有限的应用。

  • 镭雕工艺:提供更高的外观质量和耐用性。镭雕工艺有助于改善模块的散热性能,且信息不易被抹除,适合自动化需求。

  • 兼容性

  • 封装兼容性:EC800M-CN 在封装上兼容多个系列模块,包括 LTE Standard EC800E-CN、EC800G-CN、EC800N-CN、EC800K-CN 和 EG800K 系列,这使得它在多种设计中具有很高的灵活性。

  • 网络协议和接口

  • 内置网络协议:支持多种工业标准的网络协议。

  • 接口:集成多个工业标准接口,确保与不同系统的兼容性。

  • 驱动支持:提供多种操作系统的 USB 虚拟串口驱动,包括 Windows(7/8/8.1/10/11)、Linux 和 Android,扩展了应用范围。

  • 应用领域

  • M2M 和 IoT:广泛应用于如 OTT(Over-the-Top)、跟踪器(Tracker)、POS 终端、数据卡、智能安全、工业级 PDA 等领域。

串口配置(stm32f103c8t6 UART2)

// 初始化IO 串2
// bound: 波特率
void uart2_init(u32 bound)
{
    // GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  // 使能,GPIOA时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // USART2
    USART_DeInit(USART2);  // 复位串口2
    // USART2_TX   PA.2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PA.2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  // 复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化PA2
    
    // USART2_RX    PA.3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);  // 初始化PA3
    // Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;       // 子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;          // IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);  // 根据指定的参数初始化VIC寄存器
    
    // USART 初始化设置
    USART_InitStructure.USART_BaudRate = bound; // 115200
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    USART_Init(USART2, &USART_InitStructure); // 初始化串口
    
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启中断
    USART_Cmd(USART2, ENABLE);                    // 使能串口
}
// 串口2接收函数
void USART2_IRQHandler(void)                            
{
    if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
    {
        ec200x_receive_process_event(USART_ReceiveData(USART2));
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
    if(USART_GetFlagStatus(USART2, USART_FLAG_ORE) == SET)
    {
        ec200x_receive_process_event(USART_ReceiveData(USART2));
        USART_ClearFlag(USART2, USART_FLAG_ORE);
    }
}

4G模块

4G模块初始化代码

/*****************************************************
下面就是需要修改的地方,修改服务器的IP地址和端口号
*****************************************************/
#define SERVERIP "IP地址"
#define SERVERPORT  端口号
/*****************************************************
初始化模块 和单片机连接,获取卡号和信号质量
*****************************************************/
void CSTX_4G_Init(void)
{
    // 打印初始化信息
    printf("start init EC800X
");
    // 发第一个命令 ATE1
    Uart2_SendStr("ATE1
"); 
    delay_ms(300);
    printf(buf_uart2.buf);      // 打印串口收到的信息
    strx = strstr((const char*)buf_uart2.buf, (const char*)"OK"); // 返回OK
    Clear_Buffer();    
    while(strx == NULL)
    {
        printf("单片机正在连接模块......
");
        Clear_Buffer();    
        Uart2_SendStr("ATE1
"); 
        delay_ms(300);
        strx = strstr((const char*)buf_uart2.buf, (const char*)"OK"); // 返回OK
    }
    printf("****单片机和模块连接成功*****
");
    Uart2_SendStr("ATI
"); // 获取模块的版本
    delay_ms(300);
    Clear_Buffer();    
    Uart2_SendStr("AT+CIMI
"); // 获取卡号,类似是否存在卡的意思,比较重要。
    delay_ms(300);
    strx = strstr((const char*)buf_uart2.buf, (const char*)"460"); // 返460,表明识别到卡了
    while(strx == NULL)
    {
        Clear_Buffer();    
        Uart2_SendStr("AT+CIMI
"); // 获取卡号,类似是否存在卡的意思,比较重要。
        delay_ms(300);
        strx = strstr((const char*)buf_uart2.buf, (const char*)"460"); // 返回OK,说明卡是存在的
    } 
    printf("我的卡号是 : %s 
", buf_uart2.buf + 8);
    Clear_Buffer();    
    Uart2_SendStr("AT+CGATT?
"); // 查询激活状态
    delay_ms(300);
    strx = strstr((const char*)buf_uart2.buf, (const char*)"+CGATT: 1"); // 返1
    Clear_Buffer();    
    while(strx == NULL)
    {
        Clear_Buffer();    
        Uart2_SendStr("AT+CGATT?
"); // 获取激活状态
        delay_ms(300);
        strx = strstr((const char*)buf_uart2.buf, (const char*)"+CGATT: 1"); // 返回1,表明注网成功
    }
    Clear_Buffer();    
    Uart2_SendStr("AT+CSQ
"); // 查看获取CSQ值
    delay_ms(300);
    strx = strstr((const char*)buf_uart2.buf, (const char*)"+CSQ:"); // 返回CSQ
    if(strx)
    {
        printf("信号质量是:%s 注意:信号最大值是31 
", buf_uart2.buf + 14);      
    }
    IWDG_Feed(); // 喂狗
}

建立TCP链接

void CSTX_4G_CreateTCPSokcet(void) // 创建sokcet
{
    memset(ATSTR, 0, BUFLEN);
    sprintf(ATSTR, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,1
", SERVERIP, SERVERPORT);
    Uart2_SendStr(ATSTR); // 创建连接TCP,输入IP以及服务器端口号码 
    delay_ms(300);
    strx = strstr((const char*)buf_uart2.buf, (const char*)"+QIOPEN: 0,0"); // 检查是否登陆成功
    errcount = 0;
    while(strx == NULL)
    {
        IWDG_Feed(); // 喂狗
        errcount++;
        strx = strstr((const char*)buf_uart2.buf, (const char*)"+QIOPEN: 0,0"); // 检查是否登陆成功
        delay_ms(100);
        if(errcount > 100)     // 超时退出死循环 表示服务器连接失败
        {
            errcount = 0;
            break;
        }
    }  
    Clear_Buffer();    
}

4G模块发送数据

void CSTX_4G_Senddata(uint8_t *len, uint8_t *data) {
    memset(ATSTR, 0, BUFLEN);
    sprintf(ATSTR, "AT+QISEND=0,%u
", *len);
    Uart2_SendStr(ATSTR);  // 发送 AT 命令
    delay_ms(300);  // 等待模块反馈 >
    // 等待模块反馈 >
    strx = strstr((const char*)buf_uart2.buf, ">");
    while (strx == NULL) {
        errcount++;
        strx = strstr((const char*)buf_uart2.buf, ">");
        if (errcount > 100) {  // 防止死循环
            errcount = 0;
            break;
        }
    }
    Uart2_SendData(data, *len);  // 发送真正的二进制数据
    delay_ms(300);  // 等待发送完成
    // 检查是否发送成功
    strx = strstr((const char*)buf_uart2.buf, "SEND OK");
    errcount = 0;
    while (strx == NULL) {
        errcount++;
        strx = strstr((const char*)buf_uart2.buf, "SEND OK");
        delay_ms(100);
        if (errcount > 100) {  // 超时退出死循环
            errcount = 0;
            break;
        }
    }
    Clear_Buffer();  // 清空缓冲区
}

使用花生壳透传

  1. 利用花生壳进行端口映射
  2. 开启网络助手
  3. 打开串口工具

结果展示

通过4G透传指定的IP和端口号发送报文数据。整体来说不复杂,但是需要点时间调试。完整工程源码可丝。


© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号