标题: A6 GPRS+GSM模块51单片机TCP通信及发送短信实例程序 [打印本页]

作者: Eanain    时间: 2017-5-24 20:38
标题: A6 GPRS+GSM模块51单片机TCP通信及发送短信实例程序

01.STC89C52拨打电话
02.STC89C52发送英文短信
03.STC89C52 GPRS TCP通信
04.STC15W GPS解析

A6模块接线方式:
1.准备一个STC89C52最小系统板

2.烧录代码(先烧录代码后接线,防止接线后下载不了代码)
3.给模块供电,给模块开机
4.接线:
    STC89C52        A6&A7
    GND        ->        GND
    TXD/P3.1->        U_RXD
    RXD/P3.0->        U_TXD

单片机源程序如下:
  1. /*********************************************************************
  2.                  作者:神秘藏宝室
  3. *********************************************************************/

  4. #include "main.h"
  5. #include "uart.h"

  6. //常量
  7. #define Success 1U
  8. #define Failure 0U

  9. //定义变量
  10. unsigned long  Time_Cont = 0;       //定时器计数器

  11. code char phoneNumber[] = "17719228082";                //替换成需要被拨打电话的号码
  12. code char msg[] = "ILoveMCU.taobao.com";                //短信内容

  13. code char TCPServer[] = "122.228.19.57";                //TCP服务器地址
  14. code char Port[] = "30396";                                                //端口

  15. unsigned int count = 0;
  16.        

  17. //****************************************************
  18. //主函数
  19. //****************************************************
  20. void main()
  21. {
  22.     Uart_Init();

  23.         if (sendCommand("AT+RST\r\n", "OK\r\n", 3000, 10) == Success);
  24.         else errorLog();
  25.         delay_ms(10);

  26.         if (sendCommand("AT\r\n", "OK\r\n", 3000, 10) == Success);
  27.         else errorLog();
  28.         delay_ms(10);

  29.         if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
  30.         else errorLog();
  31.         delay_ms(10);

  32.         if (sendCommand("AT+CREG?\r\n", "CREG: 1", 1000, 10) == Success);
  33.         else errorLog();
  34.         delay_ms(10);

  35.         if (sendCommand("AT+CGATT=1\r\n", "OK\r\n", 1000, 10) == Success);
  36.         else errorLog();
  37.         delay_ms(10);

  38.         if (sendCommand("AT+CGDCONT=1,\"IP\",\"CMNET\"\r\n", "OK\r\n", 1000, 10) == Success);
  39.         else errorLog();
  40.         delay_ms(10);


  41.         if (sendCommand("AT+CGACT=1,1\r\n","OK\r\n", 1000, 10) == Success);
  42.         else errorLog();
  43.         delay_ms(10);

  44.        

  45.         while(1)
  46.         {       
  47.                  char xdata send_buf[100] = {0};
  48.                 memset(send_buf, 0, 100);    //清空
  49.                 strcpy(send_buf, "AT+CIPSTART=\"TCP\",\"");
  50.                 strcat(send_buf, TCPServer);
  51.                 strcat(send_buf, "\",");
  52.                 strcat(send_buf, Port);
  53.                 strcat(send_buf, "\r\n");
  54.                 if (sendCommand(send_buf, "CONNECT", 10000, 5) == Success);
  55.                 else errorLog();

  56.                 //发送数据
  57.                 if (sendCommand("AT+CIPSEND\r\n", ">", 3000, 5) == Success);
  58.                 else errorLog();

  59.                 memset(send_buf, 0, 100);    //清空
  60.                 sprintf(send_buf,"ILoveMCU.taobao.com %d\r\n",count);
  61.                 count++;
  62.                

  63.                 if (sendCommand(send_buf, send_buf, 3000, 1) == Success);
  64.                 else errorLog();
  65.                 delay_ms(100);

  66.                 memset(send_buf, 0, 100);    //清空
  67.                 send_buf[0] = 0x1a;
  68.                 if (sendCommand(send_buf, send_buf, 3000, 5) == Success);
  69.                 else errorLog();
  70.                 delay_ms(100);

  71.                 if (sendCommand("AT+CIPCLOSE\r\n", "OK\r\n", 3000, 10) == Success);
  72.                 else errorLog();

  73.                 delay_ms(1000);
  74.         }
  75. }

  76. void sendMessage(char *number,char *msg)
  77. {
  78.         xdata char send_buf[20] = {0};
  79.         memset(send_buf, 0, 20);    //清空
  80.         strcpy(send_buf, "AT+CMGS=\"");
  81.         strcat(send_buf, number);
  82.         strcat(send_buf, "\"\r\n");
  83.         if (sendCommand(send_buf, ">", 3000, 10) == Success);
  84.         else errorLog();

  85.         SendString(msg);

  86.         SendData(0x1A);
  87. }

  88. void phone(char *number)
  89. {
  90.         char send_buf[20] = {0};
  91.         memset(send_buf, 0, 20);    //清空
  92.         strcpy(send_buf, "ATD");
  93.         strcat(send_buf, number);
  94.         strcat(send_buf, ";\r\n");

  95.         if (sendCommand(send_buf, "SOUNDER", 10000, 10) == Success);
  96.         else errorLog();
  97. }

  98. void errorLog()
  99. {
  100.         while (1)
  101.         {
  102.                   if (sendCommand("AT\r\n", "OK", 100, 10) == Success)
  103.                 {
  104.                         soft_reset();
  105.                 }
  106.                 delay_ms(200);
  107.         }
  108. }

  109. void soft_reset(void)         //制造重启命令
  110. {
  111.    ((void (code *) (void)) 0x0000) ();
  112. }

  113. unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
  114. {
  115.         unsigned char n;
  116.         CLR_Buf();
  117.         for (n = 0; n < Retry; n++)
  118.         {
  119.                 SendString(Command);                 //发送GPRS指令

  120.                 Time_Cont = 0;
  121.                 while (Time_Cont < Timeout)
  122.                 {
  123.                         delay_ms(100);
  124.                         Time_Cont += 100;
  125.                         if (strstr(Rec_Buf, Response) != NULL)
  126.                         {
  127. ……………………

  128. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
6.51单片机例程.rar (298.3 KB, 下载次数: 511)



作者: rongwinzip    时间: 2017-5-28 11:09
恢复加点积分
作者: hiya    时间: 2017-5-31 14:14
好想下载啊
作者: 1130555300    时间: 2017-5-31 21:31
有没有电路图?
作者: zxy2266    时间: 2017-6-13 23:02
现在研究GPRS可惜没有黑币
作者: 909169697    时间: 2017-6-30 19:46
楼主在不在啊
作者: 909169697    时间: 2017-6-30 19:47
楼主在不啊,审核好麻烦
作者: 909169697    时间: 2017-6-30 21:17
头文件不对劲啊,不能编译啊
compiling gsmC.c...
gsmC.c(18): warning C318: can't open file 'main.h'
gsmC.c(19): warning C318: can't open file 'uart.h'
GSMC.C(37): warning C206: 'Uart_Init': missing function-prototype
GSMC.C(39): warning C206: 'sendCommand': missing function-prototype
GSMC.C(39): error C267: 'sendCommand': requires ANSI-style prototype
gsmC.c - 1 Error(s), 4 Warning(s).
作者: 15728001162    时间: 2017-7-10 10:57
学习了!!!!!!
作者: heeyon    时间: 2017-7-29 16:21
分数不够怎么办啊?想下载看看
作者: 非黄腾达    时间: 2017-8-17 17:54
压缩包的程序和直接给的程序都不一样,很怀疑楼主是怎么使用的????
作者: 敲代码时间    时间: 2017-9-5 10:58
入门级
作者: zjw12694    时间: 2017-10-18 22:01
很有用,喜欢
作者: ryanycy    时间: 2017-10-30 13:42
能不能加点分
作者: ryanycy    时间: 2017-10-30 13:42
试试看
作者: qweqwertyuiop    时间: 2017-11-17 17:46
非常有用
作者: 2100598336    时间: 2017-11-26 17:21
看看 学习一下
作者: 袁洁栋    时间: 2017-12-7 19:11
谢谢分享,下载了
作者: 小潘哥    时间: 2017-12-7 20:13
谢谢楼主无私分享,学习学习
作者: 852200    时间: 2017-12-30 10:21
分数不够
作者: 1126469277    时间: 2018-2-8 11:53
909169697 发表于 2017-6-30 21:17
头文件不对劲啊,不能编译啊
compiling gsmC.c...
gsmC.c(18): warning C318: can't open file 'main.h'
...

他这个少了一个头文件,51单片机的没有定义头文件
作者: 几许轻唱    时间: 2018-4-8 17:07
看看 学习一下
作者: DoveMyZ    时间: 2018-4-19 19:56
学习学习
作者: DoveMyZ    时间: 2018-4-19 21:51
涨知识了
作者: haikuc    时间: 2018-5-7 11:41
学习一下谢谢大神
作者: xxd123456    时间: 2018-5-22 17:41
下载了是哪一个呢???

作者: 笑看    时间: 2018-5-22 18:14
学习了!!!!
作者: fish_yu    时间: 2018-6-27 21:15
很想看一下,可惜没有黑币
作者: 沧澜钓翁    时间: 2018-7-1 08:20
谢谢楼主分享,很棒!!!
作者: 李志康的    时间: 2018-7-2 15:23
想下载看看  就是没黑币
作者: 两航。    时间: 2018-7-19 09:07
gsm模块加单片机 很好的应用
作者: yuanhaidong    时间: 2018-7-21 21:05
谢谢楼主分享
作者: yuanhaidong    时间: 2018-7-21 21:05
感谢楼主分享,蹭个热度。很好用
作者: m123    时间: 2018-8-28 20:26
感谢分享。。。。。。。。
作者: a965718891    时间: 2018-8-29 11:07
谢谢大佬们,,,
作者: xiaogua    时间: 2018-8-30 11:38
想看下接入网络部分的代码.
作者: brcsss    时间: 2018-9-5 16:25
STC89C52 GPRS TCP通信
作者: 奇虎楠夏    时间: 2018-12-2 20:52
你好,这个接线直接三根不行啊,能告诉一下我怎么接么
作者: G_5748    时间: 2019-3-7 21:55
所有的GPRS模块都是通用的吗
作者: songkdksljf    时间: 2019-3-15 21:33
我好想下载,但是没有积分
作者: 山高水远啊    时间: 2019-3-30 16:07
厉害                        
作者: 罪恶王冠    时间: 2019-4-5 17:36
还不错把,就是有些难懂。
作者: xxxdcr    时间: 2019-7-4 16:13
这个是真的写得很不错,结构清晰明了,内容简单易懂,适合我这种学板子新手
作者: wx1831    时间: 2019-7-6 15:09
可以   
作者: 守护00    时间: 2019-7-14 19:29
我试了为什么没有用




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1