GPRS模块SIM300的使用

简介:

GPRS模块使用的说明

使用GPRS首先需要打开串口,设置好串口的各项参数。

一、AT+CMGL 读取所有短信

A、读取所有的信息:

第一步:at+cmgf=1 如果返回ok则继续。

说明:AT+CMGF  SELECT SMS MESSAGE FORMAT

第二步:at+cmgl=”all”

成功返回OK否则ERROR

+CMGL: 1,"REC READ","+8613702936376",,"05/12/16,09:04:43+00"

test

OK

B、读取未读的信息

第一步:at+cmgf=1

如果返回ok则继续。

第二步:at+cmgl=” rec unread”

成功返回OK否则ERROR

C、读取已读的信息

第一步:at+cmgf=1

如果返回ok则继续。

第二步:at+cmgl=” rec read”

成功返回OK否则ERROR

AT+CMGL参数:

1) If text mode:

  "REC UNREAD"    Received unread messages (default)

  "REC READ"          Received read messages

  "STO UNSENT"     Stored unsent messages

  "STO SENT"           Stored sent messages

  "ALL"                      All messages

2) If PDU mode:

  0         Received unread messages (default)

  1         Received read messages

  2         Stored unsent messages

  3         Stored sent messages

  4         All messages

二、AT+CMGR Read SMS message

X存储区读短消息:

       第一步:at+cmgf=1 如果返回ok则继续。

       第二步:at+cmgl=x

三、AT+CMGD Delete SMS message

删除信息:

       第一步:at+cmgf=1

                     如果返回ok则继续。

       第二步:at+cmgd= 数字号

成功返回OK否则ERROR

四、AT+CSCA SMS service centre address(设置短信中心号码)

设置短信中心号码:

       第一步:at+cmgf=1

                     如果返回ok则继续。

       第二步:at+csca=”短信中心号码  (比如+8613800546500

                     成功返回OK否则ERROR

五、AT+CMGS Send SMS message (发送信息)

发送信息:

       第一步:at+cmgf=1

                     如果返回ok则继续。

       第二步:AT+CMGS=“+8613xxxxxxxxx”<回车>

>输入短消息。Crtl+Z结束并发送。

注:此只能发送英文与数字

六、查询

查询设备:

命令AT+CGMI<回车>

返回:SIEMENS

返回:OK

命令AT+CGMM<回车>

返回:MC35i

返回:OK

命令:AT+CMEE=1<回车>

返回:具体的错误代码

ATZ;E 回车 Echo OFF

ATZ;E1回车 Echo ON

AT+CREG?回车 回答x(X=2脱网,X=1注册,X=0状态不明)

AT+COPS?回车 表示SIM卡是否被网络接受

AT+COPS=?回车 显示所有可用的网络。

AT+CREG?回车(是否网络注册)

AT+CSQ     SIGNAL QUALITY REPORT

AT+IPR=2400 回车(改变RS232口的速率至2400bps)

AT&W 回车 (保存已改参数)

输入PIN码:AT+CPIN=“xxxx”

七、网络是否注册(AT+CREG?

命令at+cmgf=1

返回:at+cmgf=1 返回:OK

 

命令AT+CREG?

返回:AT+CREG?   返回:+CREG: 0,1

注:( 2脱网, 1注册, 0状态不明)

返回:OK

 

八、SIM卡是否被网络接受(AT+COPS?)

命令at+cmgf=1

返回:at+cmgf=1

返回:OK

 

命令at+cops?

 

返回:+COPS: 0,0,"China Mobile"

 

返回:OK

 

九、显示所有可用的网络(AT+COPS=?)

命令at+cmgf=1

返回:at+cmgf=1

返回:OK

命令:at+cops=?

返回:+COPS: (2,"China Mobile",,"46000"),(3,"China Unicom",,"46001"),,(0-4),(0,2)

返回:OK

十、打电话

Demonstration              Syntax                  Expect Result

Make a voice call   ATD6241xxxx;      OK  MS makes a voice call

Hang up a call        ATH                            OK  Call dropped

 

 

 

问题解决

采用sim300发送短信时采用下面的格式

AT+CMGF=1  设置文本格式

AT+CMGS=”13245678912”   注意这块一定要加上双引号。否则返回ERROR

另外如果发送不出去,有可能是短信中心号码设置的不正确。

完整的程序

// this is a test about GPRS

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

#include <fcntl.h> // open() close()

#include <unistd.h>      // read() write()

 

#include <termios.h>    // set baud rate

 

#include <fcntl.h>

#include <sys/select.h>

#include <sys/time.h>

#include <sys/types.h>

 

#define FUNC_RUN            0

#define FUNC_NOT_RUN          1

 

#define SIMPLE_TEST              1

#define READ_SIM_CARD_ID 2

#define GSM_GPRS_SIGNAL     3

#define MAKE_A_CALL            4

#define WAIT_A_CALL            5

#define SHORT_MESSAGE              6

#define POWER_MANAGE              7

#define FUNC_QUIT          8

 

#define SEND_SHORT_MESSAGE           1

#define READ_SHORT_MESSAGE           2

#define CONFIG_SHORT_MESSAGE_ENV        3

#define QUIT_SHORT_MESSAGE            4

 

#define DEVICE_TTYS "/dev/ttySAC1"

#define MAX_LEN_OF_SHORT_MESSAGE    140

 

 

#define RECEIVE_BUF_WAIT_1S 1

#define RECEIVE_BUF_WAIT_2S 2

#define RECEIVE_BUF_WAIT_3S 3

#define RECEIVE_BUF_WAIT_4S 4

#define RECEIVE_BUF_WAIT_5S 5

 

#define VERSION         "YHPDA_GSM_1.0.0"

 

void showversion(void)

{

        printf("*********************************************\n");

        printf("\t %s \t\n", VERSION);

        printf("*********************************************\n\n");

 

}

 

//------------------------------------- read datas from GSM/GPRS --------------------------------------------

// succese return 1

// error   return 0

int read_GSM_GPRS_datas(int fd, char *rcv_buf,int rcv_wait)

{

       int retval;

       fd_set rfds;

       struct timeval tv;

 

       int ret,pos;

 

       tv.tv_sec = rcv_wait;      // wait 2.5s

        tv.tv_usec = 0;

 

       pos = 0// point to rceeive buf

      

       while (1)

       {

              FD_ZERO(&rfds);

                FD_SET(fd, &rfds);

 

              retval = select(fd+1 , &rfds, NULLNULL, &tv);

 

                if (retval == -1)

              {

                        perror("select()");

                        break;

                }

              else if (retval)

              {// pan duan shi fou hai you shu ju

                        ret = read(fd, rcv_buf+pos, 2048);

                        pos += ret;

                        if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n')

                     {

                                FD_ZERO(&rfds);

                                FD_SET(fd, &rfds);

 

                                retval = select(fd+1 , &rfds, NULLNULL, &tv);

 

                                if (!retval) break;// no datas, break

                        }

                }

              else

              {

                        printf("No data\n");

                        break;

                }

       }

 

       return 1;

// end read_GSM_GPRS_datas

 

 

//------------------------------------- send cmd ------------------------------------------------------------

// succese return 1

// error   return 0

int send_GSM_GPRS_cmd(int fd, char *send_buf)

{

       ssize_t ret;

      

       ret = write(fd,send_buf,strlen(send_buf));

       if (ret == -1)

        {

                printf ("write device %s error\n", DEVICE_TTYS);

                return -1;

        }

 

       return 1;

// end send_GSM_GPRS_cmd

 

//------------------------------------- send cmd and read back result ---------------------------------------

void GSM_GPRS_send_cmd_read_result(int fd, char *send_buf, int rcv_wait)

{

        char rcv_buf[2048];

 

       if((send_buf==NULL) || (send_GSM_GPRS_cmd(fd,send_buf)))

       {     // send success , then read

              bzero(rcv_buf,sizeof(rcv_buf));

              if (read_GSM_GPRS_datas(fd,rcv_buf,rcv_wait))

                {

                        printf ("%s\n",rcv_buf);

                }

                else

                {

                        printf ("read error\n");

                }

 

       }

       else

       {

              printf("write error\n");

       }

// end GSM_GPRS_send_cmd_read_result

 

//------------------------------------- send cmd : "at" to GSM/GPRS MODEM -----------------------------------

void GSM_simple_test(int fd)

{

       char *send_buf="at\r";

      

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

 

// end GSM_simple_test

 

//------------------------------------- send cmd : "at+ccid" to GSM/GPRS MODEM ------------------------------

void GSM_read_sim_card_id(int fd)

{

        char *send_buf="at+ccid\r";

 

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

 

// end GSM_read_sim_card_id

 

//------------------------------------- send cmd : "at+ccid" to GSM/GPRS MODEM ------------------------------

void GSM_gprs_signal(int fd)

{

        char *send_buf="at+creg?\r";

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

        send_buf="at+cgreg?\r";

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

        send_buf="at+csq\r";

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

        send_buf="at+cops?\r";

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

// end GSM_read_sim_card_id

 

//------------------------------------- send cmd : "atd<tel_num>;" to GSM/GPRS MODEM ------------------------

//------------------------------------- finish call, send cmd:  "ath" to GSM MODEM --------------------------

void GSM_call(int fd)

{

       char send_buf[17];

       char *send_cmd_ath = "ath\r";

       int i;

 

        // input telephone number

        bzero(send_buf,sizeof(send_buf));

      

        send_buf[0]='a';

        send_buf[1]='t';

        send_buf[2]='d';

       send_buf[16] = '\0';

 

        printf("please input telephone number:");

 

        i = 3;

        while (1)

        {

                send_buf[i]=getchar();

                if (send_buf[i]=='\n') break;

                i++;

        }

 

        send_buf[i]=';';

        send_buf[i+1]='\r';

       // end input telphone number

 

       // send cmd

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

 

        //quit call

        printf("press ENTER for quit:");

        getchar();

 

        // send cmd

       GSM_GPRS_send_cmd_read_result(fd,send_cmd_ath,RECEIVE_BUF_WAIT_1S);

      

// end GSM_call

 

//------------------------------------- wait for GSM/GPRS call signal ---------------------------------------

void GSM_wait_call(int fd)

{

       char rcv_buf[2048];

       char *send_cmd_ath = "ath\r";

        int wait_RING;

 

        wait_RING = 10;

        while (wait_RING!=0)

        {    

              bzero(rcv_buf,sizeof(rcv_buf));

 

              if (read_GSM_GPRS_datas(fd,rcv_buf,RECEIVE_BUF_WAIT_1S))

                {

                        printf ("%s\n",rcv_buf);

                }

                else

                {

                        printf ("read error\n");

                }

              wait_RING--;

        }

      

       GSM_GPRS_send_cmd_read_result(fd,send_cmd_ath,RECEIVE_BUF_WAIT_1S);

 

        printf("quit wait_call\n");

 

// end GSM_wait_call

 

//------------------------------------- GSM/GPRS Config short message env -----------------------------------

void GSM_Power_Manage(int fd)

{

       char *send_buf="AT+CPOWD\r";// power off

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

// end GSM_Conf_Message

 

//------------------------------------- GSM/GPRS send short message -----------------------------------------

void GSM_Send_Message(int fd)

{

        char cmd_buf[23];

        char short_message_buf[MAX_LEN_OF_SHORT_MESSAGE];

        int  i;

        char rcv_buf;

 

        bzero(cmd_buf,sizeof(cmd_buf));

        bzero(short_message_buf,sizeof(short_message_buf));

 

        printf ("send short message:\n");

 

        cmd_buf[0]='a';

        cmd_buf[1]='t';

        cmd_buf[2]='+';

        cmd_buf[3]='c';

        cmd_buf[4]='m';

        cmd_buf[5]='g';

        cmd_buf[6]='s';

        cmd_buf[7]='=';

        cmd_buf[8]='"';

 

        printf ("please input telephone number:");

 

        i = 9;

        while (1)

        {

                cmd_buf[i]=getchar();

                if (cmd_buf[i]=='\n') break;

                i++;

        }

       cmd_buf[i]='"';

        cmd_buf[i+1]='\r';

       cmd_buf[i+2]='\0';

 

        // send cmd :  at+cmgs="(telephone number)"

       GSM_GPRS_send_cmd_read_result(fd,cmd_buf,RECEIVE_BUF_WAIT_1S);

      

      // input short message

        printf("please input short message:");

 

        i = 0;

        while(i < MAX_LEN_OF_SHORT_MESSAGE-2)

        {

                short_message_buf[i] = getchar();

                if (short_message_buf[i]=='\n') break;

                i++;

        }

        short_message_buf[i] = 0x1A;

        short_message_buf[i+1] = '\r';

       short_message_buf[i+2] = '\0';

 

        // send short message

       GSM_GPRS_send_cmd_read_result(fd, short_message_buf,RECEIVE_BUF_WAIT_4S);

 

        printf("\nend send short message\n");

// end GSM_Send_Message

 

//------------------------------------- GSM/GPRS read all short message -------------------------------------

void GSM_Read_Message(int fd)

{

       char *send_buf="at+cmgl=\"ALL\"\r";

       char rcv_buf[2048];

 

        GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_3S);

 

       printf("end read all short message\n");

 

// end GSM_Read_Message

 

//------------------------------------- GSM/GPRS Config short message env -----------------------------------

void GSM_Conf_Message(int fd)

{

       char *send_buf="at+cmgf=1\r";

       char *send_center_buf="at+csca=\"+8613800571500\"\r";

 

       GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);

 

        // set short message center number

        GSM_GPRS_send_cmd_read_result(fd,send_center_buf,RECEIVE_BUF_WAIT_1S);

 

        printf("end config short message env\n");

// end GSM_Conf_Message

 

 

//------------------------------------- GSM/GPRS short message ----------------------------------------------

void GSM_short_mesg(int fd)

{

        int flag_sm_run, flag_sm_select;

 

        flag_sm_run = FUNC_RUN;

        while (flag_sm_run == FUNC_RUN)

        {

                printf ("\n Select:\n");

                printf ("1 : Send short message \n");

                printf ("2 : Read all short message \n");

                printf ("3 : Config short message env\n");

                printf ("4 : quit\n");

                printf (">");

                scanf("%d",&flag_sm_select);

                getchar();

              // temp

       //     printf ("input select:%d\n",flag_sm_select);

              // end temp

                switch (flag_sm_select)

                {

                        case SEND_SHORT_MESSAGE         :       { GSM_Send_Message(fd);         break; }

                        case READ_SHORT_MESSAGE         :       { GSM_Read_Message(fd);         break; }

                        case CONFIG_SHORT_MESSAGE_ENV   :       { GSM_Conf_Message(fd);         break; }

                        case QUIT_SHORT_MESSAGE         :       { flag_sm_run = FUNC_NOT_RUN;       break; }

                        default :

                                {

                                        printf("please input your select use 1 to 3\n");

                                }

                }

        }

        printf ("\n");

 

// end GSM_send_mesg

 

//------------------------------------- print ---------------------------------------------------------------

void print_prompt(void)

{

        printf ("Select what you want to do:\n");

        printf ("1 : Simple Test\n");

        printf ("2 : Read SIM CARD ID\n");

        printf ("3 : Test GSM/GPRS signal\n");

        printf ("4 : Make A Call\n");

        printf ("5 : Wait A Call\n");

        printf ("6 : Short message\n");

        printf ("7 : Power manage\n");

        printf ("8 : Quit\n");

        printf (">");

// end print_prompt

 

//------------------------------------- Control GSM/GPRS MODULE ---------------------------------------------

void func_GSM(int fd)

{

        int flag_func_run;

        int flag_select_func;

        ssize_t ret;

 

        flag_func_run = FUNC_RUN;

 

        while (flag_func_run == FUNC_RUN)

        {

                print_prompt();                    // print select functions

                scanf("%d",&flag_select_func);    // user input select

                getchar();

 

                switch(flag_select_func)

                {

                        case SIMPLE_TEST        : {GSM_simple_test(fd);         break;}

                        case READ_SIM_CARD_ID   : {GSM_read_sim_card_id(fd);    break;}

                        case GSM_GPRS_SIGNAL    : {GSM_gprs_signal(fd);         break;}

                        case MAKE_A_CALL        : {GSM_call(fd);                break;}

                        case WAIT_A_CALL        : {GSM_wait_call(fd);           break;}

                        case SHORT_MESSAGE      : {GSM_short_mesg(fd);          break;}

                        case POWER_MANAGE       : {GSM_Power_Manage(fd);          break;}

                        case FUNC_QUIT          :

                                                {

                                                        flag_func_run = FUNC_NOT_RUN;

                                                        printf("Quit GSM/GPRS function.  byebye\n");

                                                        break;

                                                }

                        default :

                        {

                                printf("please input your select use 1 to 7\n");

                        }

                }

 

        }

}// end func_GPRS

 

//------------------------------------- init seriel port  ---------------------------------------------------

void init_ttyS(int fd)

{

       struct termios options;

       bzero(&options, sizeof(options));       // clear options

       cfsetispeed(&options,B115200);            // setup baud rate

       cfsetospeed(&options,B115200);

       options.c_cflag |= (CS8 | CREAD);

       options.c_iflag = IGNPAR;

       tcflush(fd, TCIFLUSH);

       tcsetattr(fd, TCSANOW, &options);

}//end init_ttyS

 

 

//------------------------------------- main ----------------------------------------------------------------

int main(void)

{

        int fd;

 

       showversion();

 

        printf("\nGSM/GPRS TESTS\n\n");

 

        // open seriel port

        fd = open(DEVICE_TTYS, O_RDWR);

 

        if (fd == -1)

        {

                printf("open device %s error\n",DEVICE_TTYS);

        }

        else

        {

              init_ttyS(fd);  // init device

              func_GSM(fd);      // GSM/GPRS functions

               

              // close ttyS0

                if (close(fd)!=0) printf("close device %s error",DEVICE_TTYS);

        }

 

        return 0;

}// end main

该程序目前的问题如下,在接受短信的时候无法显示短信的内容,而只是显示一个“~p”,谁有这个问题的解决方法,欢迎与我探讨。

clip_image001


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/05/21/1740956.html,如需转载请自行联系原作者


相关文章
|
存储 Ubuntu 搜索推荐
构建多用户的 Jupyter 服务器 —— 利用 JupyterHub
【8月更文第29天】**摘要** JupyterHub 是一个易于使用的、可伸缩的、多用户的 Jupyter Notebook 服务器。它允许您在一个集中式服务器上托管多个独立的 Jupyter Notebook 会话,非常适合团队协作和教学环境。本文将详细介绍如何安装和配置 JupyterHub,以及如何利用它来构建一个多用户 Jupyter 服务器环境。
4110 0
|
算法
面向高维优化问题的混沌粒子群混合蝴蝶优化算法(Matlab代码实现)
面向高维优化问题的混沌粒子群混合蝴蝶优化算法(Matlab代码实现)
185 0
|
设计模式 存储 安全
一文带你彻底搞懂发布与订阅设计
我们常说的发布订阅设计模式,也叫观察者模式,也就是事件监听机制,观察者模式订阅了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,当这个主题对象发生改变时,会通知所有的观察者对象,使他们能够自动的更新自己!
1159 0
一文带你彻底搞懂发布与订阅设计
|
2天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1084 0
|
11天前
|
人工智能 运维 安全
|
1天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
363 0
|
10天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
2天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
273 0
|
9天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
11天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
776 23