Test the GSM/GPRS module of Linux and Android platforms

Source: Internet
Author: User
Tags define definition

Here we record whether the driver of the modem module works properly on a common Linux or Android smartphone platform in a quick way. Note that the program running depends on the cross compiler version. It must be a compiler with a serial library function, such as 2008q3. After compilation is completed, a series of options to be entered are displayed for testing projects.

(1) Main Function

// This is a test about GPRS # include <stdio. h> # include <string. h> # include <stdlib. h> # include <fcntl. h> # include <unistd. h> # include <termios. h> // set baud rate, which must be included in the library # include <fcntl. h> # include <sys/select. h> # include <sys/time. h> # include <sys/types. h> # define func_run0 # define func_not_run1 // Test Project # define simple_test 1 # define read_sim_card_id 2 # define gsm_upls_signal3 # define make_a_call 4 # define Wai T_a_call 5 # define short_message 6 # define power_manage 7 # define func_quit 8 // Short Message test subproject # define send_short_message1 # define definition # define Definition 3 # define definition # define device_ttys "/dev/ s3c2410_serial1 "// UART port allocated by modem # define region # define receive_buf_wait_1s 1 # define receive_buf_wait_2s 2 # define receive_buf_wait_3s 3 # define receive_buf_wa It_4s 4 # define receive_buf_wait_5s 5int main (void) {int FD; printf ("\ ngsm/GPRS tests \ n"); FD = open (device_ttys, o_rdwr ); // enable the tty device if (FD =-1) {printf ("open device % s error \ n", device_ttys);} else {init_ttys (FD ); // init device func_gsm (FD); // GSM/GPRS functions if (close (FD )! = 0) printf ("Close device % s error", device_ttys) ;}return 0 ;}

Configure tty Functions

void init_ttyS(int fd){struct termios options;bzero(&options, sizeof(options));       // clear optionscfsetispeed(&options,B115200);            // setup baud ratecfsetospeed(&options,B115200);options.c_cflag |= (CS8 | CREAD);options.c_iflag = IGNPAR;tcflush(fd, TCIFLUSH);tcsetattr(fd, TCSANOW, &options);}

Modem Test Module main function

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 TXT 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: {fuse (FD); break;} case gsm_upls_signal: {gsm_upls_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 ;} // break exits the switch, but does not exit while 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 ");}}}}

Text prompt options:

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

(2) control details of each test sub-option at command: Case simple_test

void GSM_simple_test(int fd){char *send_buf="at\r";GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);} 

Screen ECHO:

At
OK

(3) public functions used

Send at command and read 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 readbzero(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");}} 

Send at command

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

Read at Response Results

Int read_gsm_upls_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.5 s TV. TV _usec = 0; Pos = 0; // point to rceeive bufwhile (1) {fd_zero (& rfds); fd_set (FD, & rfds); retval = select (FD + 1, & rfds, null, null, & TV); If (retval =-1) {// timeout perror ("select ()"); break;} else if (retval) {// determine whether there is data ret = read (FD, rcv_buf + POs, 2048 ); POS + = ret; // read data length if (rcv_buf [pos-2] = '\ R' & rcv_buf [pos-1] =' \ n ') {// if you read the fd_zero (& rfds); fd_set (FD, & rfds); retval = select (FD + 1, & rfds, null, null, & TV) at the end of the string ); if (! Retval) break; // No datas, break} else {printf ("no data \ n"); break ;}} return 1 ;}

(4) 2, read SIM card EF-CCID, Case read_sim_card_id

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);} 

Screen ECHO:

At + CCID
89860076190753501064

(5) obtain the network parameter case gsm_upls_signal.

Void gsm_upls_signal (int fd) {char * send_buf = "at + CREG? \ R "; // The Network registers gsm_upls_send_assist_read_result (FD, send_buf, receive_buf_wait_1s); send_buf =" at + cgreg? \ R "; Evaluate (FD, send_buf, receive_buf_wait_1s); send_buf =" at + CSQ \ r "; // signal quality measures (FD, send_buf, receive_buf_wait_1s ); send_buf = "at + cops? \ R "; // carrier information gsm_upls_send_assist_read_result (FD, send_buf, receive_buf_wait_1s );}

Screen ECHO:

At + CREG?
+ CREG: 0, 1

OK

At + cgreg?
+ Cgreg: 0, 1

OK

At + CSQ
+ CSQ: 25, 0

OK

At + cops?
+ Cops: 0, 0, "China Mobile"

OK

(6) dialing case make_a_call

Void gsm_call (int fd) {char send_buf [17]; // The sending buffer char * send_wash_ath = "ath \ r"; // The Hanging command 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 (); // obtain the input if (send_buf [I] = '\ n') break from the standard input device "keyboard; I ++;} send_buf [I] = '; // Add the symbol after sending the command; send_buf [I + 1] =' \ R '; // end input telphone number // send cmdgsm_upls_send_assist_read_result (FD, send_buf, receive_buf_wait_1s); // dial // quit call printf ("Press ENTER for quit:"); getchar (); // press any key to exit dialing // send cmdgsm_upls_send_assist_read_result (FD, send_assist_ath, receive_buf_wait_1s); // host up}

Screen ECHO:

Please input telephone number: 112
112
Atd112;
OK
(7) wait for the call case wait_a_call

Void gsm_wait_call (int fd) {char rcv_buf [2048]; char * send_wash_ath = "ath \ r"; // host int wait_ring; wait_ring = 10; // wait 10 times while (wait_ring! = 0) {bzero (rcv_buf, sizeof (rcv_buf); If (read_gsm_upls_datas (FD, rcv_buf, callback) {printf ("% s \ n", rcv_buf ); // wait for a response} else {printf ("read error \ n");} wait_ring --;} gsm_upls_send_assist_read_result (FD, send_assist_ath, receive_buf_wait_1s ); printf ("Quit wait_call \ n ");}

The screen ECHO is as follows: if a phone is dial in, the ring is displayed.

No data

Ring

No data

No data

No data

Ring

No data

No data

No data

Ring

ATH
OK

Quit wait_call

(8) Short Message Service case 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 "); // short message center printf (" 4: Quit \ n "); printf ("> "); scanf (" % d ", & flag_sm_select); // enter the select option getchar (); // wait for the keyboard to enter 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

A. Configure the short message center first.

Void gsm_conf_message (int fd) {char * send_buf = "at + cmgf = 1 \ r "; // set the SMS control mode char * send_center_buf = "at + csca = \" + 8613800755500 \ "\ r"; // SMS service center address gsm_upls_send_assist_read_result (FD, send_buf, receive_buf_wait_1s); // set short message center number gsm_upls_send_assist_read_result (FD, send_center_buf, receive_buf_wait_1s); printf ("End config Short Message env \ n ");}

Echo:

At + cmgf = 1
OK

At + csca = "+ 8613800571500"
OK

End config Short Message env

B. send information

Void gsm_send_message (int fd) {char 1__buf [23]; char short_message_buf [empty]; int I; char rcv_buf; bzero (1__buf, sizeof (1__buf); bzero (empty, sizeof (short_message_buf); printf ("send short message: \ n"); pai_buf [0] = 'a'; pai_buf [1] = 'T '; pai_buf [2] = '+'; pai_buf [3] = 'C'; pai_buf [4] = 'M'; pai_buf [5] = 'G '; pai_buf [6] ='s '; pai_buf [7] ='; pai_buf [8] = '"'; // At + cmgs send Short Message command printf ("Please Input Telephone number:"); I = 9; while (1) {pai_buf [I] = getchar (); if (pai_buf [I] = '\ n') break; I ++; // enter the number} else _buf [I] = '"'; pai_buf [I + 1] = '\ R'; pai_buf [I + 2] =' \ 0'; // Add the terminator // send cmd: at + cmgs = "(telephone number)" gsm_upls_send_assist_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 Digest (FD, short_message_buf, receive_buf_wait_4s); printf ("\ nend send Short Message \ n ");}

Echo:

Send Short Message:
Please input telephone number: 13800138000
At + cmgs = "13800138000"
OK

Please input short message: teset
Teset
Teset
OK

C. Short Messages for reading SIM cards

Void gsm_read_message (int fd) {char * send_buf = "at + cmgl = \" all \ "\ r"; // read the short message char rcv_buf from the SIM card [2048]; gsm_upls_send_assist_read_result (FD, send_buf, receive_buf_wait_3s); printf ("End read all short message \ n ");}

Echo:

At + cmgl = "all"
+ Cmgl: 1, "rec read", "+ 8613800138000", "11/03/17,21: 44: 39 + 32" followed by a large amount of information. Complete.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.