?? gsm_gprs.c
字號:
/*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 MAKE_A_CALL 3#define WAIT_A_CALL 4#define SHORT_MESSAGE 5#define FUNC_QUIT 6#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/ttyS1"#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/*----------------- 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; /* fd_set 定義 文件描述符集 */ struct timeval tv; int ret,pos; tv.tv_sec = rcv_wait; /* 等待時間 */ tv.tv_usec = 0; pos = 0; /* point to rceeive buf */ while (1) { FD_ZERO(&rfds); /* 清除文件描述符集rfds */ FD_SET(fd, &rfds); /* 將文件描述符fd 加入到文件描述符集rfds */ /*使用select函數判斷是否接收緩沖區內有數據, 如果在等待了([tv.tv_sec]秒+[tv.tv_usec]微秒)后沒有數據, 則retval=0,出錯時 retval=-1,retval其它情況時表示接收到了數據*/ retval = select(fd+1 , &rfds, NULL, NULL, &tv); /* 函數select用于解決I/O復用,成功返回準備好的文件描述符,錯誤返回’-1’ */ if (retval == -1) { perror("select()"); break; } else if (retval) {/*使用read函數讀出接收緩沖區中的內容,存放在數組rcv_buf[]中, 如果接收正確,ret=接收到的字節數*/ ret = read(fd, rcv_buf+pos, 2048); /**ret返回實際讀得的字符數*/ pos += ret; if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n') {/*當接收到的字符為‘\r’或者‘\n‘時表示接收了一行數據, 但并不代表接收數據完成,當接收到以’\r‘’\n‘為結尾的字符串, 并且再等待了([tv.tv_sec]秒+[tv.tv_usec]微秒)沒有新的數據時, 認為接收一次數據完成*/ FD_ZERO(&rfds); FD_SET(fd, &rfds); retval = select(fd+1 , &rfds, NULL, NULL, &tv); if (!retval) 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){/*將send_buf中的內容發送到fd指向的設備,發送正確 ret返回發送的字節數,發送錯誤ret返回“-1”*/ 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 ---------------------*//*發送一個最簡單的AT 命令,查看GPRS模塊是否連接正確, 嵌入式處理器發送命令 :at 如果模塊工作正常,收到后返回 :ok */void GSM_simple_test(int fd){ char *send_buf="at\r"; /* 這里的字符’\r‘表示AT命令的結束 */ 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 ------------*//*查看SIM卡的ID號: 嵌入式處理器發送命令 :at+ccid 如果SIM卡正確安裝,模塊工作正常,收到后返回SIM卡ID,以OK 結束 */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 : "atd<tel_num>;" to GSM/GPRS MODEM ------*//*----------------- finish call, send cmd: "ath" to GSM MODEM --------*//*呼叫用戶輸入的號碼 嵌入式處理器發送命令 :atd<電話號碼>; 按下回車健,將退出此次呼叫 */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 -------------------*//* 不斷檢查接收緩沖,查看本地號碼是否被呼叫 當檢測到有振鈴信號時,在屏幕顯示RING 檢測一段時間后自動退出 */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 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 -----------------*//* 配置短消息模式為文本模式;短消息服務中心號碼為+8613800100500*/void GSM_Conf_Message(int fd){ char *send_buf="at+cmgf=1\r"; char *send_center_buf="at+csca=\"+8613800100500\"\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(); 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 : Make A Call\n"); printf ("4 : Wait A Call\n"); printf ("5 : Short message\n"); printf ("6 : Quit\n"); printf (">");} /*end print_prompt*/ /*---------------- Control GSM/GPRS MODULE ------------------*/void func_GSM(int fd){/*測試函數入口*/ int flag_func_run; int flag_select_func; /*記錄用戶的選選擇輸入*/ flag_func_run = FUNC_RUN;/*FUNC_RUN 已經定義為 0 */ while (flag_func_run == FUNC_RUN) { print_prompt(); /*顯示選擇菜單信息*/ scanf("%d",&flag_select_func); /*獲取用戶輸入的選擇*/ 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 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 FUNC_QUIT: { flag_func_run = FUNC_NOT_RUN; /*FUNC_NOT_RUN已定義為1*/ 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;/*定義options,主要串口設置*/ bzero( &options, sizeof( options )); /*將options的信息清零*/ cfsetispeed(&options,B9600); /*設置通訊波特率*/ cfsetospeed(&options,B9600); options.c_cflag |= (CRTSCTS | CS8 | CLOCAL | CREAD); /*CRTSCTS有流控制 CS8 8位數據位 1位停止位 CLOCAL 本地連接,無調制調解器控制 CREAD 允許接收數據*/ options.c_iflag = IGNPAR; /*忽略奇偶校驗*/ /*設置等待時間和最小接收字符*/ options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 0; tcflush(fd, TCIFLUSH);/*TCIFLUSH 刷清輸入隊列,溢出數據可以接收,但不讀*/ tcsetattr(fd, TCSANOW, &options);/*啟用新的串口設置,將其屬性賦予串口,TSANOW表示立即生效*/}/*end init_ttyS*//*---------------------- main -----------------------------*/int main(void){ int fd; /*用于記錄文件描述符(端口)*/ printf("\nGSM/GPRS TESTS\n\n"); /*以讀寫方式打開串口 */ fd = open(DEVICE_TTYS, O_RDWR); /*#define DEVICE_TTYS "/dev/ttyS0"前面已經定義*/ if (fd == -1) {/*串口打開失敗*/ printf("open device %s error\n",DEVICE_TTYS); } else {/*串口成功打開*/ init_ttyS(fd); /*初始化串口設備*/ func_GSM(fd); /*進行GSM/GPRS測試*/ /*關閉串口*/ if (close(fd)!=0) /*關閉串口*/ printf("close device %s error",DEVICE_TTYS); } return 0;}/*end main*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -