yan zhang
/
Nucleo401_SIM808_GPS
gps_test
main.cpp@0:23342e031341, 2017-06-24 (annotated)
- Committer:
- donghuoyinzi
- Date:
- Sat Jun 24 09:55:03 2017 +0000
- Revision:
- 0:23342e031341
gps_test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donghuoyinzi | 0:23342e031341 | 1 | #include "mbed.h" |
donghuoyinzi | 0:23342e031341 | 2 | #include <string> |
donghuoyinzi | 0:23342e031341 | 3 | |
donghuoyinzi | 0:23342e031341 | 4 | #define buf_max 256 |
donghuoyinzi | 0:23342e031341 | 5 | |
donghuoyinzi | 0:23342e031341 | 6 | Serial pc(SERIAL_TX, SERIAL_RX); |
donghuoyinzi | 0:23342e031341 | 7 | Serial sim808(PA_9, PA_10); |
donghuoyinzi | 0:23342e031341 | 8 | DigitalOut sim_power(D4);//PB_5 //复位SIM808 |
donghuoyinzi | 0:23342e031341 | 9 | |
donghuoyinzi | 0:23342e031341 | 10 | int rec_cnt = 0; //receive count |
donghuoyinzi | 0:23342e031341 | 11 | char result[buf_max]; |
donghuoyinzi | 0:23342e031341 | 12 | |
donghuoyinzi | 0:23342e031341 | 13 | void sim_callback() |
donghuoyinzi | 0:23342e031341 | 14 | { |
donghuoyinzi | 0:23342e031341 | 15 | char x; |
donghuoyinzi | 0:23342e031341 | 16 | x = sim808.getc(); |
donghuoyinzi | 0:23342e031341 | 17 | result[rec_cnt++]= x; |
donghuoyinzi | 0:23342e031341 | 18 | pc.putc(x); |
donghuoyinzi | 0:23342e031341 | 19 | if(rec_cnt>=buf_max-1)rec_cnt=0; |
donghuoyinzi | 0:23342e031341 | 20 | } |
donghuoyinzi | 0:23342e031341 | 21 | bool sim_wait(int x) |
donghuoyinzi | 0:23342e031341 | 22 | { |
donghuoyinzi | 0:23342e031341 | 23 | char *p; |
donghuoyinzi | 0:23342e031341 | 24 | rec_cnt = 0; |
donghuoyinzi | 0:23342e031341 | 25 | memset(result,0,buf_max*sizeof(char)); |
donghuoyinzi | 0:23342e031341 | 26 | while(x) |
donghuoyinzi | 0:23342e031341 | 27 | { |
donghuoyinzi | 0:23342e031341 | 28 | wait(1); |
donghuoyinzi | 0:23342e031341 | 29 | p=strstr(result,"OK"); |
donghuoyinzi | 0:23342e031341 | 30 | if(p) |
donghuoyinzi | 0:23342e031341 | 31 | { |
donghuoyinzi | 0:23342e031341 | 32 | return true; |
donghuoyinzi | 0:23342e031341 | 33 | } |
donghuoyinzi | 0:23342e031341 | 34 | x--; |
donghuoyinzi | 0:23342e031341 | 35 | } |
donghuoyinzi | 0:23342e031341 | 36 | return false; |
donghuoyinzi | 0:23342e031341 | 37 | } |
donghuoyinzi | 0:23342e031341 | 38 | |
donghuoyinzi | 0:23342e031341 | 39 | int main() |
donghuoyinzi | 0:23342e031341 | 40 | { |
donghuoyinzi | 0:23342e031341 | 41 | pc.baud(115200); |
donghuoyinzi | 0:23342e031341 | 42 | sim808.baud(115200); |
donghuoyinzi | 0:23342e031341 | 43 | sim808.attach(&sim_callback,SerialBase::RxIrq); |
donghuoyinzi | 0:23342e031341 | 44 | |
donghuoyinzi | 0:23342e031341 | 45 | pc.printf("AT+CGNSPWR=1\r\n"); //打开GPS |
donghuoyinzi | 0:23342e031341 | 46 | sim808.printf("AT+CGNSPWR=1\r\n"); |
donghuoyinzi | 0:23342e031341 | 47 | |
donghuoyinzi | 0:23342e031341 | 48 | if(sim_wait(1)){ |
donghuoyinzi | 0:23342e031341 | 49 | pc.printf("GPS Open Success\r\n"); |
donghuoyinzi | 0:23342e031341 | 50 | } |
donghuoyinzi | 0:23342e031341 | 51 | else{ |
donghuoyinzi | 0:23342e031341 | 52 | pc.printf("GPS Open Failed\r\n"); |
donghuoyinzi | 0:23342e031341 | 53 | } |
donghuoyinzi | 0:23342e031341 | 54 | pc.printf("AT+CGNSURC=1\r\n"); //每秒发送一次GPS定位信息 |
donghuoyinzi | 0:23342e031341 | 55 | sim808.printf("AT+CGNSURC=1\r\n"); |
donghuoyinzi | 0:23342e031341 | 56 | wait(10); |
donghuoyinzi | 0:23342e031341 | 57 | pc.printf("AT+CGNSPWR=0\r\n"); //关闭GPS |
donghuoyinzi | 0:23342e031341 | 58 | sim808.printf("AT+CGNSPWR=0\r\n"); |
donghuoyinzi | 0:23342e031341 | 59 | } |