M26模组的应用例子,包括接听语音呼叫、模块拨号测试、GSM录音、使用 TEXT 模式发送短信、使用 PDU 模式发送短信、蓝牙传输测试
、TCP连接测试、UDP透传测试、模块基站定位
EasyM26每一个功能实验对应一个函数实现,
当要验证某一功能时, 只需要将主函数 while
循环下该函数前注释删掉.
单片机源程序如下:
- /**
- * @file main.c
- * @author dibo
- * @version 1.0
- * @data 2018-3-18
- * @brief
- * COPYRIGHT(c) 2018 ZERO CHIP
- */
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "clk.h"
- #include "led.h"
- #include "uart2.h"
- #include "m26.h"
- int main(void)
- {
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- led_init();
- MX_USART2_UART_Init();
- open_uart2_rx();
-
- while (1)
- {
- // m26_ring_test(); /* 实验1 GSM接电话 */
- // m26_call_test(); /* 实验2 GSM打电话 */
- // m26_gsm_record(); /* 实验3 GSM录音 */
- // m26_sms_text(); /* 实验4 发短信(TEXT模式) */
- // m26_sms_pdu(); /* 实验5 发短信(PDU模式) */
- // m26_bt_master(); /* 实验6 蓝牙主机模式 */
- // m26_bt_slave(); /* 实验7 蓝牙从机模式 */
- // m26_tcp_test(); /* 实验8 TCP透传 */
- // m26_udp_test(); /* 实验9 UDP透传 */
- // m26_locator_test(); /* 实验10 LBS基站定位 */
- }
- }
- void Error_Handler(void)
- {
- }
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码- /**
- * @file m26.c
- * @author dibo
- * @version 1.0
- * @data 2018-3-18
- * @brief
- * COPYRIGHT(c) 2018 ZERO CHIP
- */
- #include "m26.h"
- #include "uart2.h"
- #include <string.h>
- uint8_t M26_State = 0;
- /*-------------------------------------------------------------------*/
- /* 内部函数原型 -----------------------------------------------------*/
- void m26_poweron(void);
- uint8_t m26_send_cmd(char *cmd, char *ack, uint16_t waittime);
- char* m26_check_cmd(char *str);
- /*-------------------------------------------------------------------*/
- /*-------------------------------------------------------------------*/
- /**
- * @brief M26 模块拨号测试
- */
- void m26_call_test(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- /* 启用命令回显 */
- m26_send_cmd("ATE1\r\n", "OK", 200);
- /* 确保模块工作正常 */
- m26_send_cmd("AT+CPIN?\r\n", "OK", 200); /* 查询SIM卡状态 */
- m26_send_cmd("AT+CSQ\r\n", "OK", 200); /* 信号质量 */
- m26_send_cmd("AT+CREG?\r\n", "OK", 200); /* 网络注册信息 */
- m26_send_cmd("AT+CGREG?\r\n", "OK", 200); /* GPRS网络注册状态 */
- /* 显示被叫识别 */
- m26_send_cmd("AT+COLP=1\r\n", "OK", 200);
- /* 切换音频通道为耳机通道 */
- m26_send_cmd("AT+QAUDCH=1\r\n", "OK", 200);
-
- /* 建立语音呼叫, 拨打 10086 */
- m26_send_cmd("ATD10086;\r\n", "OK", 200);
-
- M26_State++;
- break;
- case 2:
- HAL_Delay(10000);
- M26_State++;
- break;
- case 3:
- m26_send_cmd("ATH\r\n", "OK", 200);
- M26_State++;
- break;
- default:
- break;
- }
- }
- /**
- * @brief 接听语音呼叫
- */
- void m26_ring_test(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- /* 切换音频通道为耳机通道 */
- m26_send_cmd("AT+QAUDCH=1\r\n", "OK", 200);
-
- USART2RxSta = 0;
-
- M26_State++;
- break;
- case 2:
- /* 接收到一次数据 */
- if (USART2RxSta & 0x8000)
- {
- /* 添加结束符 */
- USART2RxBuf[USART2RxSta & 0x7FFF] = 0;
-
- if ( strstr((const char*)USART2RxBuf, (const char*)"RING") )
- {
- M26_State++;
- }
-
- USART2RxSta = 0;
- }
-
- break;
- case 3:
- /* 接听语音呼叫 */
- m26_send_cmd("ATA\r\n", "OK", 200);
-
- M26_State++;
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26 GSM录音
- */
- void m26_gsm_record(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- /* 启用命令回显 */
- m26_send_cmd("ATE1\r\n", "OK", 200);
- /* 切换音频通道为耳机通道 */
- m26_send_cmd("AT+QAUDCH=1\r\n", "OK", 200);
- /* 建立语音呼叫, 拨打 10086 */
- m26_send_cmd("ATD10086;\r\n", "OK", 200);
-
- /* 开始录音 */
- m26_send_cmd("AT+QAUDCH=1,\"test.amr\"\r\n", "OK", 200);
-
- M26_State++;
- break;
- case 2:
- HAL_Delay(10000);
- M26_State++;
- break;
- case 3:
- /* 停止录音 */
- m26_send_cmd("AT+QAUDRD=0\r\n", "OK", 200);
- M26_State++;
- break;
- case 4:
- /* 播放录音 */
- m26_send_cmd("AT+QAUDPLAY=\"test.amr\",0,60\r\n", "OK", 200);
-
- HAL_Delay(10000);
-
- /* 停止录音 */
- m26_send_cmd("AT+QAUDSTOP\r\n", "OK", 200);
- M26_State++;
- default:
- break;
- }
- }
- /**
- * @brief 使用 TEXT 模式发送短信
- */
- void m26_sms_text(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("AT+CMGF=1\r\n", "OK", 200);
- m26_send_cmd("AT+CSCS=\"GSM\"\r\n", "OK", 200);
- m26_send_cmd("AT+CMGS=\"10086\"\r\n", ">", 200);
- m26_send_cmd("1\r\n", "OK", 200);
-
- M26_State++;
- break;
- case 2:
- break;
- default:
- break;
- }
- }
- /**
- * @brief 使用 PDU 模式发送短信
- */
- void m26_sms_pdu(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("AT+CSCS=\"GSM\"\r\n", "OK", 200);
- m26_send_cmd("AT+CMGF=0\r\n", "OK", 200);
- m26_send_cmd("AT+CSCS=\"UCS2\"\r\n", "OK", 200);
- m26_send_cmd("AT+CMGS=39\r\n", ">", 200);
- m26_send_cmd("0891683108501705F011000D91688152613201F800080018\
- 670968A660F376844EBAFF0C547D90FD662F82E67684FF01\r\n", "OK", 200);
-
- M26_State++;
- break;
- case 2:
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26 蓝牙主机模式
- */
- void m26_bt_master(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("AT+QBTPWR=1\r\n", "OK", 200); /* 打开蓝牙模块 */
- m26_send_cmd("AT+QBTUNPAIR=1\r\n", "OK", 200); /* 删除已配对蓝牙设备 */
- m26_send_cmd("AT+QBTSCAN\r\n", "OK", 1000); /* 扫描蓝牙设备, 等待10s */
-
- M26_State++;
- break;
- case 2:
- if (USART2RxSta & 0x8000)
- {
- /* 添加结束符 */
- USART2RxBuf[USART2RxSta & 0x7FFF] = 0;
- if (strstr((char*)USART2RxBuf, (char*)"+QBTSCAN:0"))
- {
- M26_State++;
- }
-
- USART2RxSta = 0;
- }
- break;
- case 3:
- /* 配对, 蓝牙设备列表第一个扫描到设备, 等待10s */
- m26_send_cmd("AT+QBTPAIR=1\r\n", "+QBTPAIR", 1000);
- /* 接受配对 */
- m26_send_cmd("AT+QBTPAIRCNF=1\r\n", "+QBTPAIRCNF", 200);
-
- USART2RxSta = 0; /* 清接收状态 */
-
- M26_State++;
- break;
- case 4:
- if (USART2RxSta & 0x8000)
- {
- /* 添加结束符 */
- USART2RxBuf[USART2RxSta & 0x7FFF] = 0;
- if (strstr((char*)USART2RxBuf, (char*)"+QBTPAIRCNF: 1,1,1"))
- {
- M26_State++;
- }
-
- USART2RxSta = 0;
- }
- break;
- case 5:
- /* 初始化连接, 以透传方式 */
- m26_send_cmd("AT+QBTCONN=1,0,2\r\n", "+QBTCONN", 200);
- M26_State++;
- case 6:
- /* 发送数据到从机(这里以手机为例) */
- m26_send_cmd("EasyM26, BT 主机模式测试.\r\n", "+QBTCONN", 200);
- HAL_Delay(10000);
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26 蓝牙从机模式
- */
- void m26_bt_slave(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("AT+QBTPWR=1\r\n", "OK", 200); /* 打开蓝牙模块 */
- m26_send_cmd("AT+QBTVISB=1\r\n", "OK", 200); /* 设置 M26 蓝牙可见 */
- m26_send_cmd("AT+QBTNAME=\"M26\"\r\n", "OK", 200); /* 蓝牙名称 */
-
- /* 清接收状态, 准备接收配对 */
- USART2RxSta = 0;
-
- M26_State++;
- break;
- case 2:
- if (USART2RxSta & 0x8000)
- {
- /* 添加结束符 */
- USART2RxBuf[USART2RxSta & 0x7FFF] = 0;
-
- if (strstr((char*)USART2RxBuf, (char*)"+QBTIND: \"pair\""))
- {
- /* 接受配对 */
- m26_send_cmd("AT+QBTPAIRCNF=1\r\n", "OK", 200);
- }
- else if (strstr((char*)USART2RxBuf, (char*)"+QBTIND: \"conn\""))
- {
- /* 接受连接 */
- m26_send_cmd("AT+QBTACPT=1,2\r\n", "OK", 200);
-
- M26_State++;
- }
-
- USART2RxSta = 0;
- }
- break;
- case 3:
-
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26 TCP连接测试
- */
- void m26_tcp_test(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("ATE0\r\n", "OK", 200); /* 关闭回显 */
- /* 确保模块工作正常 */
- m26_send_cmd("AT+CPIN?\r\n", "OK", 200); /* 查询SIM卡状态 */
- m26_send_cmd("AT+CSQ\r\n", "OK", 200); /* 信号质量 */
- m26_send_cmd("AT+CREG?\r\n", "OK", 200); /* 网络注册信息 */
- m26_send_cmd("AT+CGREG?\r\n", "OK", 200); /* GPRS网络注册状态 */
- /* TCP设置 */
- m26_send_cmd("AT+QIMODE=0\r\n", "OK", 200); /* 配置TCPIP应用模式为非透传模式 */
- m26_send_cmd("AT+QICSGP=1,\"FXTX.ZB.SD\"\r\n", "OK", 200); /* 设置接入点,联通移动默认CMNET. 由于测试使用的是专网卡,所以这里设置了专用接入点 */
- m26_send_cmd("AT+QIREGAPP\r\n", "OK", 200);
- m26_send_cmd("AT+QICSGP?\r\n", "OK", 200);
- m26_send_cmd("AT+QIACT\r\n", "OK", 200);
- m26_send_cmd("AT+QILOCIP\r\n", "QILOCIP", 200); /* 获取IP */
- /* 连接服务器 */
- m26_send_cmd("ATV1\r\n", "OK", 200);
- m26_send_cmd("AT+QIHEAD=1\r\n", "OK", 200);
- m26_send_cmd("AT+QIDNSIP=1\r\n", "OK", 200);
- m26_send_cmd("AT+QIOPEN=\"TCP\",\"zerochip.in.3322.org\",\"7425\"\r\n", "OK", 200);
- /* 发送数据到远端服务器 */
- m26_send_cmd("AT+QISEND=18\r\n", ">", 200);
- m26_send_cmd("zerochip easy m26.", "SEND OK", 200);
- /* 关闭连接 */
- m26_send_cmd("AT+QICLOSE\r\n", "CLOSE OK", 200);
-
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
-
- M26_State++;
- break;
- case 2:
- HAL_Delay(10000);
- M26_State++;
- break;
- case 3:
-
- // M26_State++;
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26 UDP透传测试
- */
- void m26_udp_test(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("ATE0\r\n", "OK", 200); /* 关闭回显 */
- /* 确保模块工作正常 */
- m26_send_cmd("AT+CPIN?\r\n", "OK", 200); /* 查询SIM卡状态 */
- m26_send_cmd("AT+CSQ\r\n", "OK", 200); /* 信号质量 */
- m26_send_cmd("AT+CREG?\r\n", "OK", 200); /* 网络注册信息 */
- m26_send_cmd("AT+CGREG?\r\n", "OK", 200); /* GPRS网络注册状态 */
- /* TCP设置 */
- m26_send_cmd("AT+QIMODE=0\r\n", "OK", 200); /* 配置TCPIP应用模式为非透传模式 */
- m26_send_cmd("AT+QICSGP=1,\"CMNET\"\r\n", "OK", 200); /* 设置接入点,联通移动默认CMNET */
- m26_send_cmd("AT+QIREGAPP\r\n", "OK", 200);
- m26_send_cmd("AT+QICSGP?\r\n", "OK", 200);
- m26_send_cmd("AT+QIACT\r\n", "OK", 200);
- m26_send_cmd("AT+QILOCIP\r\n", "QILOCIP", 200); /* 获取IP */
- /* 连接服务器 */
- m26_send_cmd("ATV1\r\n", "OK", 200);
- m26_send_cmd("AT+QIHEAD=1\r\n", "OK", 200);
- m26_send_cmd("AT+QIDNSIP=1\r\n", "OK", 200);
- m26_send_cmd("AT+QIOPEN=\"UDP\",\"zerochip.in.3322.org\",\"7425\"\r\n", "OK", 200);
-
- /* 发送数据到远端服务器 */
- m26_send_cmd("AT+QISEND\r\n", ">", 200);
- m26_send_cmd("EasyM26 UDP test.", "SEND OK", 200);
- /* 关闭连接 */
- m26_send_cmd("AT+QICLOSE\r\n", "CLOSE OK", 200);
-
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
-
- M26_State++;
- break;
- case 2:
- HAL_Delay(10000);
- M26_State++;
- break;
- default:
- break;
- }
- }
- /**
- * @brief M26模块基站定位
- */
- void m26_locator_test(void)
- {
- switch(M26_State)
- {
- case 0:
- m26_poweron();
- M26_State++;
- break;
- case 1:
- m26_send_cmd("ATE0\r\n", "OK", 200); /* 关闭回显 */
- /* 确保模块工作正常 */
- m26_send_cmd("AT+CPIN?\r\n", "OK", 200); /* 查询SIM卡状态 */
- m26_send_cmd("AT+CSQ\r\n", "OK", 200); /* 信号质量 */
- m26_send_cmd("AT+CREG?\r\n", "OK", 200); /* 网络注册信息 */
- m26_send_cmd("AT+CGREG?\r\n", "OK", 200); /* GPRS网络注册状态 */
- /* TCP设置 */
- m26_send_cmd("AT+QIMODE=0\r\n", "OK", 200); /* 配置TCPIP应用模式为非透传模式 */
- m26_send_cmd("AT+QIFGCNT=0\r\n", "OK", 200);
- m26_send_cmd("AT+QICSGP=1,\"FXTX.ZB.SD\"\r\n", "OK", 200); /* 设置接入点,联通移动默认CMNET. 由于测试使用的是专网卡,所以这里设置了专用接入点 */
- m26_send_cmd("AT+QIREGAPP\r\n", "OK", 200);
- m26_send_cmd("AT+QICSGP?\r\n", "OK", 200);
- m26_send_cmd("AT+QIACT\r\n", "OK", 200);
- m26_send_cmd("AT+QILOCIP\r\n", "QILOCIP", 200); /* 获取IP */
- /* 获取位置 */
- m26_send_cmd("AT+QCELLLOC=1\r\n", "+QCELLLOC", 200);
-
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
-
- M26_State++;
- break;
- case 2:
- HAL_Delay(10000);
- M26_State++;
- break;
- case 3:
-
- // M26_State++;
- break;
- default:
- break;
- }
- }
- /*-------------------------------------------------------------------*/
- /* 内部函数实现 -----------------------------------------------------*/
- /**
- * @brief M26 模块开机
- */
- void m26_poweron(void)
- {
- /* 手动开机 */
- // M26_ON_L();
- // M26_PWR_L();
- // HAL_Delay(5000);
- // M26_PWR_H();
- // HAL_Delay(1000);
- // M26_ON_H();
- // HAL_Delay(1000);
- // M26_ON_L();
- // HAL_Delay(5000);
-
- /* 上电等待模块自启动 */
- HAL_Delay(10000);
- }
- /**
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
GSM模组M26单片机DEMO例子(基于STM32单片机).7z
(1.13 MB, 下载次数: 293)
|