国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - 樹莓派.GPRS.短信接收器

樹莓派.GPRS.短信接收器

2021-01-27 11:27幻河 JAVA教程

這篇文章主要介紹了樹莓派.GPRS.短信接收器的相關知識,需要的朋友可以參考下

起因

樹莓派.GPRS.短信接收器

曾經用過西門子出的短信貓, 好處是直接有sdk開發包, 不會硬件開發也能直接使用

缺點也是明顯的, 就是只支持windows系統, 另外就是在windows下工作很不穩定, 隔開幾天就會出現收不到短信的毛病, 要斷電重啟設備才有機會恢復(還不是必然恢復)

樹莓派.GPRS.短信接收器

后來在地府(dfrobot)發現了新品"gravity: uart a6 gsm & gprs 無線通信模塊",買來試了一下發現可用(不過不清楚地府的a6和外面常見的sim800系列、sim900系列有什么不同), 而且可以自己寫驅動支持linux下運行,完美

期間也碰到一些小坑, 記錄一下。 

需求清單

 - 自動初始化gprs模塊

 - 接收短信并能解析出重要元素(包括:來電號碼/時間/短信內容)

 - 把解析到的短信內容上傳到服務器保存

 - 清除已閱短信

 - 支持linux系統

 - 開發語言:java

硬件清單

 - 樹莓派2代b型(3代串口使用上有區別,需要另外的方法處理)
 - gravity: uart a6 gsm & gprs 無線通信模塊
 - usb無線網卡(可選)
 - usb電源適配器2個(重要,為什么要2個后面會說明)
 - 16gb tf卡一張
 - 可以接收短信的手機卡1張(必須是移動或聯通的卡, 電信的不支持)

樹莓派.GPRS.短信接收器

接線方法

(樹莓派40pin引腳圖)

樹莓派.GPRS.短信接收器

(a6引腳說明)

樹莓派     a6
--------------------------------------

gpio15    rx

gpio16    tx

gnd       gnd

重要: a6模塊的電源需要單獨供電!!!

a6模塊不能直接從樹莓派上的gpio 5v針腳接電,因為電流不足!

最開始的時候, 我是從樹莓派上取電供給a6, 結果串口怎么都無法通信,剛開始還以為是波特率的問題,結果折騰了半天后, 留意到a6上有個藍燈(上面寫著sleep)有明暗變化, 不穩定,感覺像是電壓不穩定一樣, 果斷試了一下把a6外接電源,然后a6才正常工作! 可以從藍燈看得出來,亮度較高,且穩定(不閃爍)

資料準備

 - 樹莓派系統(用noobs或raspbian都可以)

 - pi4j (java支持包)

 - at指令知識

樹莓派系統安裝方法可以自行搜索,或看我之前發過的文章

系統裝好后還涉及到如何把gpio15(tx)和gpio16(rx)啟用的問題, 見這篇文章:《兩個樹莓派通過串口通信》

pi4j是個能讓java訪問樹莓派40個gpio的支持包, 可以上官網下載安裝,傳送門>>>

最重要和容易掉坑的是關于接收短信的at指令部分,下面要詳細講解

這里需要做的功能是利用a6接收短信,涉及到以下指令

* 第一步:初始化gprs.模塊

* at      握手 / sim卡檢測等
* at+cpin?   查詢是否檢測到sim卡
* at+csq    信號質量測試,值為0-31,31表示最好
* at+ccid    讀取sim的ccid(sim卡背面20位數字),可以檢測是否有sim卡或者是否接觸良好
* at+creg?    檢測是否注冊網絡

------以上指令用于初始化模塊,一般接線沒問題,波特率設置沒問題的話都是比較容易調通

* 第二步:初始化gprs.設置短信模式及短信接收參數

* at+cmgf=1              0-pdu, 1-文本格式
* at+csdh=1
* at+cpms="sm","sm","sm"      將信息保存在sim卡中, sm-表示存在sim卡上
* at+cnmi=2,1,0,1,1         收接通知,并存在指定位置(與at+cpms設置有關)

極易掉坑系列,逐個講解:

設置短信格式指令:at+cmgf=1

分2種短信格式: 0-pdu, 1-文本格式

如果設置的是pdu模式, 那么你收到的短信就是這樣 的:

+ciev: "message",1

+cmt: ,32
0891683110200005f0040ba18126601728f00000710102610272230e74747a0e4acf416110bd3ca703

如何讀懂pdu要另外翻閱專業文章, 這里如果你不使用發短信功能的話,建議不要采用pdu格式

pdu格式的好處是可以發中文短信!

好, 如果設置是文本格式,收到的短信就類似下面這樣的:

+ciev: "message",1

+cmt: "test again ,中文也試試

直接就能看到短信內容,中文也一樣可以顯示出來(注意它可以gb2312或gbk編碼)

然后就有問題產生了, 新短信來時是上面這樣的格式, 短信內容是可以獲取了, 但特么為什么看不出是誰(手機號)發來的呢?下面就是入坑的時候:

看不到手機號怎么辦, 你可以試試這個指令:

at+cmgl="all"

它能讀出存在sim卡上的短信(包括已讀和未讀,以及外發時存著的短信),執行后收到的內容大概是這個樣子:

+cmgl: 2,"rec read","106907931100",,"2017/06/02,10:15:21+08",160,134
【小米】[小米移動]您2017年5月共消費0.75元,當前余額99.05元。其中:數據流量費0.05元;語音通信費0.7元;短/彩信費+cmgl: 3,"rec read","106907931100",,"2017/07/02,10:15:19+08",160,54
。查詢賬單 http://10046.mi.com。+cmgl: 4,"rec read","106907931100",,"2017/07/02,10:15:19+08",160,134
【小米】[小米移動]您2017年6月共消費1.32元,當前余額97.88元。其中:數據流量費1.32元;語音通信費0元;短/彩信費0元+cmgl: 5,"rec read","106908761100",,"2017/08/02,10:15:30+08",160,54
。查詢賬單 http://10046.mi.com。+cmgl: 6,"rec read","106908761100",,"2017/08/02,10:15:30+08",160,134
【小米】[小米移動]您2017年7月共消費0.81元,當前余額97.01元。其中:數據流量費0.81元;語音通信費0元;短/彩信費0元at+cmgd=2


好! 很明顯你要的信息都有了, 來電號碼/時間/短信內容, 似乎可以用了喔!

但是這時你會發現剛收到的信息不一定在這個清單里! 這是怎么回事呢? 我反正查閱了很多資料,費了大量的時間也不知怎么回事

后來才發現這個指令(查看sim卡內存情況):

at+cpms?

執行后你可能會看到這個結果:

+cpms: "mt",0,50,"sm",1,50,"me",0,50

ok

重點關注"sm"后面第1個數字"1"代表當前存了多少條短信, 第2個數字"50"代表存儲上限

"sm"表示sim卡, 其它2個一個代表手機設備, 另一個是手機內存

然后你給a6發個新短信, 有可能發現這個"1"不會增加! 為什么收到的新短信不存到sim卡上呢?

然后就找到這個指令

at+cnmi=<mode>,<mt>,<bm>,<ds>,<bfr>

這個指令比較復雜, 它負責設置收到新短信后的處理機制, 下面是參數含義

    <mode>控制通知te的方式.
    0 - 先將通知緩存起來,再按照<mt>的值進行發送.
    1 - 在數據線空閑的情況下,通知te,否則,不通知te.
    2 - 數據線空閑時,直接通知te;否則先將通知緩存起來,待數據線空閑時再行發送.
    3 - 直接通知te.在數據線被占用的情況下,通知te的消息將混合在數據中一起傳輸.
    <mt>設置短消息存儲和通知te的內容.
    0 - 接受的短消息存儲到默認的內存位置(包括class 3),不通知te.
    1 - 接收的短消息儲存到默認的內存位置,并且向te發出通知(包括class 3).通知的形式為:+cmti:"sm",<index>
    2 - 對于class 2短消息,儲存到sim卡,并且向te發出通知;對于其他class,直接將短消息轉發到te:+cmt:[<alpha>],<length><cr><lf><pdu>(pdu模式)
         或者+cmt:<oa>,[<alpha>,]<scts>[,<tooa>,<fo>,<pid>,<dcs>,<sca>,<tosca>,<length>]<cr><lf><data>(text模式)
    3 - 對于class 3短消息,直接轉發到te,同<mt>=2;對于其他class,同<mt>=1. 
    <bm>設置小區廣播
    0 - 小區廣播不通知
    2 - 新的小區廣播通知,返回+cbm:;length;;cr;;lf;;pdu;
    3 - class3格式的小區廣播通知,使用bm=2格式
    <ds>狀態報告
    0 - 狀態報告不通知
    1 - 新的狀態報告通知,返回:+cds:;length;;cr;;lf;;pdu;
    2 - 如果新的狀態報告存儲到me,則返回:+cdsi:;mem;,;index;

相信看完你已經蒙圈, 我就是, 如果你看得懂, 那恭喜了!

我在這里采用的參數是

at+cnmi=2,1,0,1,1        收接通知,并存在指定位置

這時再測試一下發條新短信給a6

+ciev: "message",1

+cmti: "sm",0

現在不顯示短信內容了(反正顯示也沒用, 因為沒來電號碼), 但"sm"后面仍然是0!

這時再用at+cmgl="all" 你會發現短信依然沒存到卡上, 結果當然也沒法看到短信內容及來電號碼等信息啦

這是怎么回事裂

后來發現這個at+cnmi跟剛才說的指令(at+cpms)息息相關,再來查一下:

at+cpms?
+cpms: "mt",0,50,"sm",1,50,"me",0,50

ok

注意看, 如果你看到的和上面差不多, 會發現有"mt"和"me"存在, 這時收到短信雖然在cnmi告訴a6收到短信要存下來啊! 但是a6找不到"mt"和"me",結果存儲失敗!

我們現在是希望它收到短信后能存在sim卡上, 所以要設置一下:

at+cpms="sm","sm","sm"

 再發條短信試試效果:

+ciev: "message",1

+cmti: "sm",1

現在看到"sm"后面是1了! 后面這個1表示的是短信存儲的sim卡內存的位置

然后可以用指令查看短信內容了, 這里有2種方法

方法1,單條讀取(at+cmgr=index)

at+cmgr=1

+cmgr: "rec unread","18620671820",,"2017/10/26,11:37:03+08",161,17,0,0,"+8613010200500",145,25
test again ,中文也試試

方法2,全部讀取(at+cmgl="all")

+cmgl: 2,"rec read","106907931100",,"2017/06/02,10:15:21+08",160,134
【小米】[小米移動]您2017年5月共消費0.75元,當前余額99.05元。其中:數據流量費0.05元;語音通信費0.7元;短/彩信費+cmgl: 3,"rec read","106907931100",,"2017/07/02,10:15:19+08",160,54
。查詢賬單http://10046.mi.com 。+cmgl: 4,"rec read","106907931100",,"2017/07/02,10:15:19+08",160,134
【小米】[小米移動]您2017年6月共消費1.32元,當前余額97.88元。其中:數據流量費1.32元;語音通信費0元;短/彩信費0元+cmgl: 5,"rec read","106908761100",,"2017/08/02,10:15:30+08",160,54
。查詢賬單 http://10046.mi.com。+cmgl: 6,"rec read","106908761100",,"2017/08/02,10:15:30+08",160,134
【小米】[小米移動]您2017年7月共消費0.81元,當前余額97.01元。其中:數據流量費0.81元;語音通信費0元;短/彩信費0元at+cmgd=2

小結一下正確獲取短信的姿勢(流程):

at+cmgf=1
at+csdh=1
at+cpms="sm","sm","sm"
at+cnmi=2,1,0,1,1

ps: 其中有一條指令沒解釋:at+csdh=1

這個留給大家查資料

好, 接下來只需要寫出java代碼分析短信內容即可.

程序部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import java.io.ioexception;
import java.util.date;
import com.common.datetimeutil;
import com.common.stringutil;
import com.pi4j.io.serial.baud;
import com.pi4j.io.serial.databits;
import com.pi4j.io.serial.flowcontrol;
import com.pi4j.io.serial.parity;
import com.pi4j.io.serial.serial;
import com.pi4j.io.serial.serialconfig;
import com.pi4j.io.serial.serialfactory;
import com.pi4j.io.serial.serialport;
import com.pi4j.io.serial.stopbits;
import com.pi4j.util.commandargumentparser;
import com.pi4j.util.console;
/**
 * this example code demonstrates how to perform serial communications using the raspberry pi.
 *
 * @author robert savage
 */
public class seriallistensms {
 /**
  * this example program supports the following optional command arguments/options:
  * "--device (device-path)"     [default: /dev/ttyama0]
  * "--baud (baud-rate)"      [default: 38400]
  * "--data-bits (5|6|7|8)"     [default: 8]
  * "--parity (none|odd|even)"     [default: none]
  * "--stop-bits (1|2)"      [default: 1]
  * "--flow-control (none|hardware|software)" [default: none]
  *
  * @param args
  * @throws interruptedexception
  * @throws ioexception
  */
 public static void main(string args[]) throws interruptedexception, ioexception {
  // !! attention !!
  // by default, the serial port is configured as a console port
  // for interacting with the linux os shell. if you want to use
  // the serial port in a software program, you must disable the
  // os from using this port.
  //
  // please see this blog article for instructions on how to disable
  // the os console for this port:
  // https://www.cube-controls.com/2015/11/02/disable-serial-port-terminal-output-on-raspbian/
  // create pi4j console wrapper/helper
  // (this is a utility class to abstract some of the boilerplate code)
  final console console = new console();
  // print program title/header
  console.title("<-- the pi4j project -->", "監聽串口(gpio15-tx / gpio16-rx)數據并寫入memcached中");
  // allow for user to exit program using ctrl-c
  console.promptforexit();
  // create an instance of the serial communications class
  final serial serial = serialfactory.createinstance();
  byte [] data = new byte[1024];  //數據緩沖區 
  try {
   // create serial config object
   serialconfig config = new serialconfig();
   system.out.println(">>>"+serialport.getdefaultport());
   config.device(serialport.getdefaultport())  // "/dev/ttyacm0"
     .baud(baud._115200)
     .databits(databits._8)
     .parity(parity.none)
     .stopbits(stopbits._1)
     .flowcontrol(flowcontrol.none);
   // parse optional command argument options to override the default serial settings.
   if(args.length > 0){
    config = commandargumentparser.getserialconfig(config, args);
   }
   // display connection details
   console.box(" connecting to: " + config.tostring(),
     " data received on serial port will be displayed below.");
   // open the default serial device/port with the configuration settings
   serial.open(config);
   serial.flush();
   system.out.println("serial.isopen():"+serial.isopen());
   /**初始化gprs模塊**/
   boolean isinit = initgprs(serial);
   long trydelay = 2000;
   while(!isinit){
    system.out.println("初始化gprs模塊不成功, 請檢查模塊工作狀態燈, 以及sim卡是否接觸良好..."+trydelay);
    thread.sleep(trydelay+=1000);
    isinit = initgprs(serial);
    if(trydelay>(10*1000)){return;}  //檢測10次都不成功時, 退出程序
   }
   /**初始化短信參數**/
   isinit = initgprs_sms(serial);
   trydelay = 2000;
   while(!isinit){
    system.out.println("初始化短信參數不成功, 請檢查模塊工作狀態燈, 以及sim卡是否接觸良好.");
    thread.sleep(trydelay+=1000);
    isinit = initgprs_sms(serial);
    if(trydelay>(10*1000)){return;}  //檢測10次都不成功時, 退出程序
   }
   //每次開機時嘗試讀取一次存儲卡中的短信
   string res = new string(sendcmd(serial, "at+cmgl=\"all\""), "gbk");
   system.out.println("at+cmgl=\"rec read\".res:"+res);
   if(res.indexof("ok")==-1){
    system.out.println("設置失敗!");
   }
   //下面進入主程序
   system.out.println("進入短信監聽程序:");
   long old_msg_delay = 60000;   //設置舊短信搜索間隔時間(毫秒),在sim卡內存中搜索數據
   long old_msg_count = 0;    //舊短信計時器
   int index = 1;
   data = null;
   while(true){
    system.out.print(".");
    if(!serial.isopen()){
     system.out.println("串口未打開, 退出程序");
     break;
    }
    if(old_msg_count>=old_msg_delay){
     //
     system.out.println("發送獲取sim卡內存中的所有信息的指令");
     sendcmd(serial, "at+cmgl=\"all\"");
     old_msg_count = 0;
    }else{
     old_msg_count+=1000;
     //system.out.println("old_msg_count..."+old_msg_count);
    }
    if(serial.available()>0){
     while(serial.available()>0){
      data=serial.read();    //此處接收到的數據上限是1024
      //system.out.print(new string(serial.read(), "utf-8"));
     }
     serial.flush();
    }
    if(data!=null){
     //接收到數據
     string cc = new string(data, "gbk");  //處理中文
     system.out.println("cc:"+cc);
     if(cc!=null && !cc.trim().equals("")){
      //處理數據
      /**
       * 有新短信時:
       * +ciev: "message",1
       *
       * +cmti: "sm",1
       */
      if(cc.indexof("+cmti")!=-1){
       index = getindexfromnewsms(cc);
       system.out.println("發現新短信.index:"+index);
       sendcmd(serial, "at+cmgr="+index);
      }
      if(cc.indexof("+cmgr")!=-1){
       string[] contents = getcontentfromindex(index, cc);
       system.out.println("[at+cmgr=index]讀取存在卡上的短信內容.分析后:");
       if(contents!=null){
        system.out.println("新短信內容:");
        for(string tt : contents){
         system.out.println(tt);
        }
        //保存讀到的短信 -> 服務器
        if(senddatatoserver(contents)){
         //刪除已讀出的短信
         system.out.println("刪除已讀出的新短信.index:"+contents[0]);
         delsmsbyindex(serial, integer.parseint(contents[0]));
        }
       }else{
        system.out.println("新短信內容:null");
       }
      }
      /**
       * 查詢舊短信時:
       * at+cmgl="all"
       *
       * +cmgl: 1,"rec read","18620671820",,"2017/10/26,11:37:03+08",161,25
       * just because the people11
       * +cmgl: 2,"rec read","18620671820",,"2017/10/26,11:37:03+08",161,25
       * just because the people11
       */
      if(cc.indexof("cmgl:")!=-1){
       //獲取第1條短信
       string[] contents = getcontentfromstoragesms(cc);
       system.out.println("[at+cmgl=\"all\"]存在卡上的短信內容.分析后:");
       for(string tt : contents){
        system.out.println(tt);
       }
       //保存讀到的短信
       if(senddatatoserver(contents)){
        //刪除已讀出的短信
        system.out.println("刪除已讀出的舊短信.index:"+contents[0]);
        delsmsbyindex(serial, integer.parseint(contents[0]));
       }
      }
     }else{
      system.out.println("data:"+new string(data));
      system.out.println("data(byte[]) 轉換成 string時出錯");
     }
    }
    //if(cc!=null && !cc.trim().equals(""))system.out.println(cc);
    data = null;
    thread.sleep(1000);
   }
  }
  catch(ioexception ex) {
   console.println(" ==>> serial setup failed : " + ex.getmessage());
   return;
  }
 }
 /**
  * 把短信上傳到服務器中
  * @param contents 數組
    [0] - 短信位置索引
    [1] - 電話號碼
    [2] - 日期+時間 2017/10/26 11:37:03+08
    [3] - 短信內容
  * @return
  */
 public static boolean senddatatoserver(string[] contents){
  system.out.println("嘗試上傳短信數據");
  try{
   //移除時間中的時區 +08 2017/10/26 12:38:14+08...2017-10-26 12:38:14
   string d = contents[2].substring(0,contents[2].lastindexof("+"));
   d = d.replace("/", "-").replace(" ", "%20");
   stringbuffer url = new stringbuffer("http://192.168.6.2:9080/webservice.do?method=savesmsbank");
   string vno = datetimeutil.datetostring(new date(), "yyyymmdd");
   vno = stringutil.encodepassword(vno, "md5");
   url.append("&vno=").append(vno);
   url.append("&smstype=0");
   url.append("&port=2");
   url.append("&rectime=").append(d);  //need: 2013-12-05%2014:35:20
   url.append("&phone=").append(contents[1]);
   url.append("&serialno=0");
   url.append("&nums=0");
   url.append("&submitport=0");
   url.append("&sendid=").append(contents[1]);
   url.append("&sendtype=0");
   url.append("&sendno=0");
   string xx = new string(contents[3].getbytes(), "utf-8");
   url.append("&txt=").append(java.net.urlencoder.encode(xx, "utf-8"));
   system.out.println("senddatatoserver().url:"+url.tostring());
   string resurl = stringutil.getcontentbyurl2(url.tostring());
   system.out.println("senddatatoserver().resurl:"+resurl);
   if(resurl.trim().equals("200")){
    system.out.println("數據上傳成功!");
    return true;
   }else if(resurl.trim().equals("401")){
    system.out.println("這個電話號碼和短信內容已上傳過, 數據重復!");
    system.out.println("清除sim卡上的短信!");
    return true;
   }
  }catch(exception e){
   e.printstacktrace();
   return false;
  }
  return false;
 }
 /**
  * 解析返回的短信內容
  * @return
  */
 public static string[] getcontentfromindex(int index, string res){
  try{
   system.out.println("嘗試讀取短信...getcontentfromindex.res:"+res);
   if(res.indexof("ok")!=-1){
    system.out.println("獲取短信成功,解析內容...");
    /**
     * +cmgr: "rec read","18620671820",,"2017/10/26,11:37:03+08",161,17,0,0,"+8613010200500",145,25
     * just because the people11
     *
     * +cmgr: "rec read","18620671820",,"2017/10/26,11:37:03+08",161,17,0,0,"+8613010200500",145,25
     * ---------------- ------------- - ---------- ----------- --- -- - - ---------------- --- --
     * [0]    [1]   [2] [3]  [4]   [5] [6][7][8] [9]   [10][11]
     */
    string[] ccs = res.split("\r\n");
    string phone = new string();
    string senddate = new string();
    string content = new string();
    boolean isvalid = false;   //數據獲取成功
    for(int i=0;i<ccs.length;i++){
     if(ccs[i].indexof("cmgr:")!=-1){
      string[] temp1 = ccs[i].split(",");
      phone = temp1[1];
      senddate = temp1[3]+" "+temp1[4];
      content = ccs[i+1];
      isvalid = true;
      break//只處理1條
     }
    }
    if(!isvalid)return null;
    //處理雙引號
    phone = phone.substring(1,phone.length()-1);
    senddate = senddate.substring(1,senddate.length()-1);
    string[] resu = new string[4];
    resu[0] = string.valueof(index);
    resu[1] = phone.trim();
    resu[2] = senddate;
    resu[3] = content;
    return resu;
   }else if(res.indexof("cms error")!=-1){
    //cms error:321 表示所讀取的內存位置出錯,一般是指定位置無短信內容所致
    system.out.println("獲取短信失敗,錯誤內容...");
    return null;
   }
  }catch(exception e){
   e.printstacktrace();
  }
  return null;
 }
 /**
  * 有新短信時,獲取短信內容:
  * +ciev: "message",1
  *
  * +cmti: "sm",1
  *
  * @return index 短信所在的內存位置 index
  */
 public static int getindexfromnewsms(string cc){
  try{
   string[] ccs = cc.split("\r\n");
   for(string v : ccs){
    if(v.indexof("cmti: \"sm\",")!=-1){
     string c = v.substring(v.indexof(",")+1);
     return integer.parseint(c);
    }
   }
  }catch(exception e){
   e.printstacktrace();
  }
  return 0;
 }
 /**
  * 查詢舊短信, 每次只抓1條:
  * +cmgl: 4,"rec read","106907931100",,"2017/07/02,10:15:19+08"
  * -------- ---------- -------------- ----------- ------------
  * [0]  [1]  [2]   [3] [4]  [5]
  【小米】[小米移動]您2017年6月共消費1.32元,當前余額97.88元。其中:數據流量費1.32元;語音通信費0元;短/彩信費0元
  +cmgl: 5,"rec read","106908761100",,"2017/08/02,10:15:30+08"
  。查詢賬單 http://10046.mi.com 。
  +cmgl: 6,"rec read","106908761100",,"2017/08/02,10:15:30+08"
  【小米】[小米移動]您2017年7月共消費0.81元,當前余額97.01元。其中:數據流量費0.81元;語音通信費0元;短/彩信費0元
  ok
  @return 數組
    [0] - 短信位置索引
    [1] - 電話號碼
    [2] - 日期+時間
    [3] - 短信內容
  */
 public static string[] getcontentfromstoragesms(string cc){
  string[] ccs = cc.split("\r\n");
  string smsindex = new string();
  string phone = new string();
  string senddate = new string();
  string content = new string();
  for(int i=0;i<ccs.length;i++){
   if(ccs[i].indexof("cmgl:")!=-1){
    //smsindex = integer.parseint(ccs[i].substring(ccs[i].indexof("cmgl:")+5, ccs[i].indexof(",")));
    smsindex = ccs[i].substring(ccs[i].indexof("cmgl:")+5, ccs[i].indexof(","));
    string[] temp1 = ccs[i].split(",");
    phone = temp1[2];
    senddate = temp1[4]+" "+temp1[5];
    content = ccs[i+1];
    break//只處理1條
   }
  }
  //處理雙引號
  phone = phone.substring(1,phone.length()-1);
  senddate = senddate.substring(1,senddate.length()-1);
  string[] res = new string[4];
  res[0] = smsindex.trim();
  res[1] = phone.trim();
  res[2] = senddate;
  res[3] = content;
  return res;
 }
 /**
  * 刪除指定位置上的短信
  * at+cmgd=4
  * @param index 短信索引位置
  * @return
  */
 public static boolean delsmsbyindex(serial serial, int index){
  string res = new string(sendcmd(serial, "at+cmgd="+index));
  system.out.println("at+cmgd="+index+":"+res);
  //if(res.indexof("ok")==-1){
  // system.out.println("刪除["+index+"]位置的短信失敗!");
  // return false;
  //}
  return true;
 }
 /**
  *
  *   初始化gprs.模塊
  *   at  100ms 握手 / sim卡檢測等
  *   at+cpin? 100ms 查詢是否檢測到sim卡
  *   at+csq 100ms 信號質量測試,值為0-31,31表示最好
  *   at+ccid 100ms 讀取sim的ccid(sim卡背面20位數字),可以檢測是否有sim卡或者是否接觸良好
  *   at+creg? 500ms 檢測是否注冊網絡
  * @return
  */
 public static boolean initgprs(serial serial){
  if(!serial.isopen()){return false;}  //串口未準備好
  byte[] buffs = new byte[128];
  try{
   system.out.println("try send at to module...");
   //char cmd[] = {'a', 't'};
   //byte cmd[] = "at".getbytes();
   //buffs = sendcmd(serial, "at".getbytes());
   system.out.print("\r\ngprs模塊檢測中...");
   buffs = sendcmd(serial, "at");
   string res = new string(buffs);
   if(res.indexof("ok")==-1){
    system.out.println("gprs模塊未準備好, 請檢查電源和串口波特率是否正確!");
    return false;
   }
   system.out.println(" ...[正常]\r\n");
   //system.out.println("at.res:"+res);
   system.out.print("\r\n檢測sim卡...");
   res = new string(sendcmd(serial, "at+cpin?"));
   if(res.indexof("ready")==-1){
    system.out.println("sim卡未準備好!");
    return false;
   }
   system.out.println(" ...[正常]\r\n");
   //system.out.println("at+cpin?.res:"+res);
   system.out.print("\r\n信號質量測試,值為0-31,31表示最好...");
   res = new string(sendcmd(serial, "at+csq"));
   if(res.indexof("error")!=-1){
    system.out.println("信號質量測試檢測失敗");
    return false;
   }
   /**
    * +csq: 24,99
    */
   string[] vs = res.split("\r\n");
   for(string v : vs){
    if(v.indexof(":")!=-1){
     string x = v.substring(v.indexof(":")+1);
     //system.out.println("x:"+x);
     system.out.println(" ...信號強度:["+x.trim()+"]\r\n");
    }
   }
   //system.out.println("at+csq.res:"+res);
   res = new string(sendcmd(serial, "at+ccid"));
   system.out.println("at+ccid.res:"+res);
   res = new string(sendcmd(serial, "at+creg?"));
   system.out.println("at+creg.res:"+res);
  }catch(exception e){
   e.printstacktrace();
   return false;
  }
  return true;
 }
 /**
  *
  * 初始化gprs.設置短信模式及短信接收參數
  *   at+cmgf=1    0-pdu, 1-文本格式
  *  at+csdh=1
  *  at+cpms="sm","sm","sm" 將信息保存在sim卡中, sm-表示存在sim卡上
  *  at+cnmi=2,1,0,1,1  收接通知,并存在指定位置(與at+cpms設置有關)
  *
  * 設置好后, 收到短信:
  *   +ciev: "message",1
  *   +cmti: "sm",1   表示存儲位置index=1
  * @return
  */
 public static boolean initgprs_sms(serial serial){
  if(!serial.isopen()){return false;}  //串口未準備好
  string res = new string();
  try{
   system.out.print("\r\n設置短信格式...");
   res = new string(sendcmd(serial, "at+cmgf=1"));
   if(res.indexof("ok")==-1){
    system.out.println("設置失敗!");
    return false;
   }
   system.out.println(" ...[文本格式]\r\n");
   thread.sleep(100);
   system.out.print("\r\nat+csdh=1...");
   res = new string(sendcmd(serial, "at+csdh=1"));
   if(res.indexof("ok")==-1){
    system.out.println("設置失敗!");
    return false;
   }
   system.out.println(" ...[done]\r\n");
   thread.sleep(100);
   system.out.print("\r\n設置信息保存位置...");
   res = new string(sendcmd(serial, "at+cpms=\"sm\",\"sm\",\"sm\""));
   if(res.indexof("ok")==-1){
    system.out.println("設置失敗!");
    return false;
   }
   system.out.println(" ...[sim卡]\r\n");
   thread.sleep(100);
   system.out.print("\r\n收接通知,并存在指定位置...");
   res = new string(sendcmd(serial, "at+cnmi=2,1,0,1,1"));
   if(res.indexof("ok")==-1){
    system.out.println("設置失敗!");
    return false;
   }
   system.out.println(" ...[done]\r\n");
   thread.sleep(100);
  }catch(exception e){
   e.printstacktrace();
   return false;
  }
  return true;
 }
 //public static byte[] sendcmd(serial serial, byte[] cmd){
 public static byte[] sendcmd(serial serial, string cmd){
  long overtime = 10000//每條指令超時上限 5秒
  long timecount = 0;   //計時器
  byte[] buffs = new byte[128];
  try {
   serial.writeln(cmd+"\r");
   //serial.writeln("at\r");
   timecount = 0;
   while(timecount<overtime){
    //system.out.print(serial.available());
    if(serial.available()>0){
     while(serial.available()>0){
      buffs = serial.read();
      //system.out.print(new string(serial.read()));
      //system.out.print(new string(buffs));
     }
     serial.flush();
     timecount = overtime;  //exit while
    }
    timecount += 100;
    thread.sleep(100);
   }
   //system.out.println("sendcmd:"+new string(buffs));
  } catch (illegalstateexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  return buffs;
 }
}
// end snippet: serial-snippet

程序中的方法: senddatatoserver()

主要是用于上傳保存短信, 大家替換成自己的方式即可

總結

以上所述是小編給大家介紹的樹莓派.gprs.短信接收器,希望對大家有所幫助!

原文鏈接:http://www.cnblogs.com/visionsl/p/7742604.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中国av在线免费观看 | 午夜伦4480yy私人影院 | 中文字幕高清一区 | 日韩欧美不卡 | 国产亚洲精品精品国产亚洲综合 | 久久人人爽爽爽人久久久 | 亚洲一区二区三区免费视频 | 天堂√在线观看一区二区 | 99在线免费视频 | 一区二区在线 | 亚洲国产精品久久人人爱 | 偷拍自拍第一页 | 久久精品国产v日韩v亚洲 | 亚洲在线观看一区二区 | 国产午夜精品一区二区三区免费 | 羞羞视频免费网站 | 一级a毛片| h色视频在线观看 | 日韩中文字幕免费在线播放 | 嫩草影院黄色 | 国产欧美精品一区二区色综合 | 在线成人小视频 | 国产亚洲精品久久久闺蜜 | 国产精品久久久久久一区 | 国产精品免费大片 | 午夜社区 | 成年人在线观看免费视频 | 一区二区视频免费 | 国产精品久久久久久中文字 | 免费在线观看一区二区 | 免费视频二区 | 中文字幕黄色 | 日韩电影免费观看 | 日韩精品一区二区三区在线观看 | www.天天操.com| 91精品国产九九九久久久亚洲 | 亚洲精品视频网站在线观看 | 成人在线播放 | 国产福利在线播放 | 最新免费av网站 | 久久久亚洲精品中文字幕 |