天天看點

GPRS子產品SIM300的使用

使用GPRS首先需要打開序列槽,設定好序列槽的各項參數。

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”

C、讀取已讀的資訊

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

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

從X存儲區讀短消息:

       第一步:at+cmgf=1 如果傳回ok則繼續。

       第二步:at+cmgl=x

删除資訊:

       第一步:at+cmgf=1

                     如果傳回ok則繼續。

       第二步:at+cmgd= 數字号

設定短信中心号碼:

       第二步:<b>at+csca=”</b><b>短信中心号碼</b><b>”</b>  (比如+8613800546500)

                     成功傳回OK否則ERROR

發送資訊:

       第一步:<b>at+cmgf=1</b>

<b></b>

       第二步:<b>AT+CMGS=“+8613xxxxxxxxx”</b>&lt;回車&gt;

<b>&gt;</b><b>輸入短消息。</b><b>Crtl+Z</b><b>結束并發送。</b>

<b>注:此隻能發送英文與數字</b>

查詢裝置:

<b>指令</b>:AT+CGMI&lt;回車&gt;

傳回:SIEMENS

傳回:OK

<b>指令</b>:AT+CGMM&lt;回車&gt;

傳回:MC35i

<b>指令:</b><b>AT+CMEE=1&lt;</b><b>回車</b><b>&gt;</b>

<b>傳回:具體的錯誤代碼</b>

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&amp;W 回車 (儲存已改參數)

輸入PIN碼:AT+CPIN=“xxxx”

<b>指令</b>:at+cmgf=1

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

<b> </b>

<b>指令</b>:AT+CREG?

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

注:( 2脫網, 1注冊, 0狀态不明)

傳回:at+cmgf=1

<b>指令</b>:at+cops?

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

指令:at+cops=?

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

Demonstration              Syntax                  Expect Result

Make a voice call   <b>ATD6241xxxx</b>;      OK  MS makes a voice call

Hang up a call        <b>ATH</b>                            OK  Call dropped

采用sim300發送短信時采用下面的格式

AT+CMGF=1  設定文本格式

AT+CMGS=”13245678912”   注意這塊一定要加上雙引号。否則傳回ERROR

另外如果發送不出去,有可能是短信中心号碼設定的不正确。

// this is a test about GPRS

<b>#include</b> &lt;stdio.h&gt;

<b>#include</b> &lt;string.h&gt;

<b>#include</b> &lt;stdlib.h&gt;

<b>#include</b> &lt;fcntl.h&gt; // open() close()

<b>#include</b> &lt;unistd.h&gt;      // read() write()

<b>#include</b> &lt;termios.h&gt;    // set baud rate

<b>#include</b> &lt;fcntl.h&gt;

<b>#include</b> &lt;sys/select.h&gt;

<b>#include</b> &lt;sys/time.h&gt;

<b>#include</b> &lt;sys/types.h&gt;

<b>#define</b> FUNC_RUN            0

<b>#define</b> FUNC_NOT_RUN          1

<b>#define</b> SIMPLE_TEST              1

<b>#define</b> READ_SIM_CARD_ID 2

<b>#define</b> GSM_GPRS_SIGNAL     3

<b>#define</b> MAKE_A_CALL            4

<b>#define</b> WAIT_A_CALL            5

<b>#define</b> SHORT_MESSAGE              6

<b>#define</b> POWER_MANAGE              7

<b>#define</b> FUNC_QUIT          8

<b>#define</b> SEND_SHORT_MESSAGE           1

<b>#define</b> READ_SHORT_MESSAGE           2

<b>#define</b> CONFIG_SHORT_MESSAGE_ENV        3

<b>#define</b> QUIT_SHORT_MESSAGE            4

<b>#define</b> DEVICE_TTYS "/dev/ttySAC1"

<b>#define</b> MAX_LEN_OF_SHORT_MESSAGE    140

<b>#define</b> RECEIVE_BUF_WAIT_1S 1

<b>#define</b> RECEIVE_BUF_WAIT_2S 2

<b>#define</b> RECEIVE_BUF_WAIT_3S 3

<b>#define</b> RECEIVE_BUF_WAIT_4S 4

<b>#define</b> RECEIVE_BUF_WAIT_5S 5

<b>#define</b> VERSION         "YHPDA_GSM_1.0.0"

<b>void</b> showversion(<b>void</b>)

{

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

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

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

}

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

// succese return 1

// error   return 0

<b>int</b> read_GSM_GPRS_datas(<b>int</b> fd, <b>char</b> *rcv_buf,<b>int</b> rcv_wait)

       <b>int</b> retval;

       fd_set rfds;

       <b>struct</b> timeval tv;

       <b>int</b> ret,pos;

       tv.tv_sec = rcv_wait;      // wait 2.5s

        tv.tv_usec = 0;

       pos = 0; // point to rceeive buf

       <b>while</b> (1)

       {

              FD_ZERO(&amp;rfds);

                FD_SET(fd, &amp;rfds);

              retval = select(fd+1 , &amp;rfds, <b>NULL</b>, <b>NULL</b>, &amp;tv);

                <b>if</b> (retval == -1)

              {

                        perror("select()");

                        <b>break</b>;

                }

              <b>else</b> <b>if</b> (retval)

              {// pan duan shi fou hai you shu ju

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

                        pos += ret;

                        <b>if</b> (rcv_buf[pos-2] == '\r' &amp;&amp; rcv_buf[pos-1] == '\n')

                     {

                                FD_ZERO(&amp;rfds);

                                FD_SET(fd, &amp;rfds);

                                retval = select(fd+1 , &amp;rfds, <b>NULL</b>, <b>NULL</b>, &amp;tv);

                                <b>if</b> (!retval) <b>break</b>;// no datas, break

                        }

              <b>else</b>

                        printf("No data\n");

       }

       <b>return</b> 1;

} // end read_GSM_GPRS_datas

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

<b>int</b> send_GSM_GPRS_cmd(<b>int</b> fd, <b>char</b> *send_buf)

       ssize_t ret;

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

       <b>if</b> (ret == -1)

        {

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

                <b>return</b> -1;

        }

} // end send_GSM_GPRS_cmd

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

<b>void</b> GSM_GPRS_send_cmd_read_result(<b>int</b> fd, <b>char</b> *send_buf, <b>int</b> rcv_wait)

        <b>char</b> rcv_buf[2048];

       <b>if</b>((send_buf==<b>NULL</b>) || (send_GSM_GPRS_cmd(fd,send_buf)))

       {     // send success , then read

              bzero(rcv_buf,<b>sizeof</b>(rcv_buf));

              <b>if</b> (read_GSM_GPRS_datas(fd,rcv_buf,rcv_wait))

                {

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

                <b>else</b>

                        printf ("read error\n");

       <b>else</b>

              printf("write error\n");

} // end GSM_GPRS_send_cmd_read_result

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

<b>void</b> GSM_simple_test(<b>int</b> fd)

       <b>char</b> *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 ------------------------------

<b>void</b> GSM_read_sim_card_id(<b>int</b> fd)

        <b>char</b> *send_buf="at+ccid\r";

} // end GSM_read_sim_card_id

<b>void</b> GSM_gprs_signal(<b>int</b> fd)

        <b>char</b> *send_buf="at+creg?\r";

        send_buf="at+cgreg?\r";

        send_buf="at+csq\r";

        send_buf="at+cops?\r";

//------------------------------------- send cmd : "atd&lt;tel_num&gt;;" to GSM/GPRS MODEM ------------------------

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

<b>void</b> GSM_call(<b>int</b> fd)

       <b>char</b> send_buf[17];

       <b>char</b> *send_cmd_ath = "ath\r";

       <b>int</b> i;

        // input telephone number

        bzero(send_buf,<b>sizeof</b>(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;

        <b>while</b> (1)

                send_buf[i]=getchar();

                <b>if</b> (send_buf[i]=='\n') <b>break</b>;

                i++;

        send_buf[i]=';';

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

       // end input telphone number

       // send cmd

        //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 ---------------------------------------

<b>void</b> GSM_wait_call(<b>int</b> fd)

       <b>char</b> rcv_buf[2048];

        <b>int</b> wait_RING;

        wait_RING = 10;

        <b>while</b> (wait_RING!=0)

        {    

              <b>if</b> (read_GSM_GPRS_datas(fd,rcv_buf,RECEIVE_BUF_WAIT_1S))

              wait_RING--;

        printf("quit wait_call\n");

} // end GSM_wait_call

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

<b>void</b> GSM_Power_Manage(<b>int</b> fd)

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

} // end GSM_Conf_Message

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

<b>void</b> GSM_Send_Message(<b>int</b> fd)

        <b>char</b> cmd_buf[23];

        <b>char</b> short_message_buf[MAX_LEN_OF_SHORT_MESSAGE];

        <b>int</b>  i;

        <b>char</b> rcv_buf;

        bzero(cmd_buf,<b>sizeof</b>(cmd_buf));

        bzero(short_message_buf,<b>sizeof</b>(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;

                cmd_buf[i]=getchar();

                <b>if</b> (cmd_buf[i]=='\n') <b>break</b>;

       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;

        <b>while</b>(i &lt; MAX_LEN_OF_SHORT_MESSAGE-2)

                short_message_buf[i] = getchar();

                <b>if</b> (short_message_buf[i]=='\n') <b>break</b>;

        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 -------------------------------------

<b>void</b> GSM_Read_Message(<b>int</b> fd)

       <b>char</b> *send_buf="at+cmgl=\"ALL\"\r";

        GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_3S);

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

} // end GSM_Read_Message

<b>void</b> GSM_Conf_Message(<b>int</b> fd)

       <b>char</b> *send_buf="at+cmgf=1\r";

       <b>char</b> *send_center_buf="at+csca=\"+8613800571500\"\r";

        // 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");

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

<b>void</b> GSM_short_mesg(<b>int</b> fd)

        <b>int</b> flag_sm_run, flag_sm_select;

        flag_sm_run = FUNC_RUN;

        <b>while</b> (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 ("&gt;");

                scanf("%d",&amp;flag_sm_select);

                getchar();

              // temp

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

              // end temp

                <b>switch</b> (flag_sm_select)

                        <b>case</b> SEND_SHORT_MESSAGE         :       { GSM_Send_Message(fd);         <b>break</b>; }

                        <b>case</b> READ_SHORT_MESSAGE         :       { GSM_Read_Message(fd);         <b>break</b>; }

                        <b>case</b> CONFIG_SHORT_MESSAGE_ENV   :       { GSM_Conf_Message(fd);         <b>break</b>; }

                        <b>case</b> QUIT_SHORT_MESSAGE         :       { flag_sm_run = FUNC_NOT_RUN;       <b>break</b>; }

                        <b>default</b> :

                                {

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

                                }

        printf ("\n");

} // end GSM_send_mesg

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

<b>void</b> print_prompt(<b>void</b>)

        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 ("&gt;");

} // end print_prompt

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

<b>void</b> func_GSM(<b>int</b> fd)

        <b>int</b> flag_func_run;

        <b>int</b> flag_select_func;

        ssize_t ret;

        flag_func_run = FUNC_RUN;

        <b>while</b> (flag_func_run == FUNC_RUN)

                print_prompt();                    // print select functions

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

                <b>switch</b>(flag_select_func)

                        <b>case</b> SIMPLE_TEST        : {GSM_simple_test(fd);         <b>break</b>;}

                        <b>case</b> READ_SIM_CARD_ID   : {GSM_read_sim_card_id(fd);    <b>break</b>;}

                        <b>case</b> GSM_GPRS_SIGNAL    : {GSM_gprs_signal(fd);         <b>break</b>;}

                        <b>case</b> MAKE_A_CALL        : {GSM_call(fd);                <b>break</b>;}

                        <b>case</b> WAIT_A_CALL        : {GSM_wait_call(fd);           <b>break</b>;}

                        <b>case</b> SHORT_MESSAGE      : {GSM_short_mesg(fd);          <b>break</b>;}

                        <b>case</b> POWER_MANAGE       : {GSM_Power_Manage(fd);          <b>break</b>;}

                        <b>case</b> FUNC_QUIT          :

                                                {

                                                        flag_func_run = FUNC_NOT_RUN;

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

                                                        <b>break</b>;

                                                }

                        {

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

}// end func_GPRS

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

<b>void</b> init_ttyS(<b>int</b> fd)

       <b>struct</b> termios options;

       bzero(&amp;options, <b>sizeof</b>(options));       // clear options

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

       cfsetospeed(&amp;options,B115200);

       options.c_cflag |= (CS8 | CREAD);

       options.c_iflag = IGNPAR;

       tcflush(fd, TCIFLUSH);

       tcsetattr(fd, TCSANOW, &amp;options);

}//end init_ttyS

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

<b>int</b> main(<b>void</b>)

        <b>int</b> fd;

       showversion();

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

        // open seriel port

        fd = open(DEVICE_TTYS, O_RDWR);

        <b>if</b> (fd == -1)

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

        <b>else</b>

              init_ttyS(fd);  // init device

              func_GSM(fd);      // GSM/GPRS functions

              // close ttyS0

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

        <b>return</b> 0;

}// end main

該程式目前的問題如下,在接受短信的時候無法顯示短信的内容,而隻是顯示一個“~p”,誰有這個問題的解決方法,歡迎與我探讨。

<a href="http://images.cnblogs.com/cnblogs_com/feisky/WindowsLiveWriter/GPRSSIM300_D9CA/clip_image001_2.jpg"></a>

本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2010/05/21/1740956.html,如需轉載請自行聯系原作者