Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 1991|回復: 2
打印 上一主題 下一主題

請教一個 nokia sample 的問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2014-5-18 01:51:59 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
nokia 提供一個WP 透過藍芽的控制arduino的sample http://developer.nokia.com/community/wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth 但這個sample我一直執行不成功﹐不知道是那裏的問題﹐arduino 程式又沒有 try catch exception 之類的語法...不曉得該如何查問題。

我依照程式沒有任何修改﹐透過 Serial.println 大概知道程式的問題在那個地方


  1. void loop() {
  2.   if(btSerial.available()) {
  3.       int commandSize = (int)btSerial.read();
  4. Serial.println("commandSize = ");
  5. Serial.println(commandSize);
  6.       char command[commandSize];
  7.       int commandPos = 0;
  8.       while(commandPos < commandSize) {
  9.         if(btSerial.available()) {
  10. Serial.println("commandPos = ");
  11. Serial.println(commandPos);
  12.           command[commandPos] = (char)btSerial.read();
  13.           commandPos++;
  14.         }
  15.       }
  16.       command[commandPos] = 0;
  17.       processCommand(command);
  18.   }
  19.   
  20.   unsigned long currentTime = millis();
  21.   if((currentTime - time) > periodicMessageFrequency) {
  22.     sendPeriodicMessages();
  23.     time = currentTime;
  24.   }
  25.   
  26. }
複製代碼


透過Serial Monitor 觀察﹐
1.當我在WP手機按下一個 RED 按鍵﹐在arduino 板上 TX 燈有閃一下﹐Serial Monitor 顯示
   commanSize =175﹐
   commandPos =0
   commandPos =1
   然後就停了
2.再按一次WP手機上 RED鍵﹐Serial Monitor
   commandPos =2
   commandPos =3
   commandPos =4
   然後就停了
3.再按一次WP手機上 RED鍵﹐Serial Monitor
   commandPos =5
   commandPos =6
   commandPos =7
   然後就停了
........

觀察起來是在 loop 有兩個地方判斷 if (btSerial.available()) ﹐當由手機第一次RED鍵﹐arduino收到資料﹐第一個btSerial.available() 判斷為true﹐然後進入while迴圈﹐在while 迴圈內的 btSerial.available()一開始判斷也是true﹐但不知道為什麼commandPos只累加了2次while 迴圈中的 btSerial.available 就判斷變成 false﹐但沒有離開 while 迴圈﹐再按一次手機上的RED鍵﹐while 中的 btSerial.available又變成了true﹐但只跑了三次 while 中的 btSerial.available 又變成 false.....

不知道到底是那裏出了問題...
2#
發表於 2014-5-19 03:49:14 | 只看該作者
回復 1# nethawk


   nokia 提供的程式應該沒問題,首先,你要確定你的bluetooth baud rate是設定在9600 bps,
要自己先查詢,


參考:
Config bluetooth baud rate w/ Arduino
http://coopermaa2nd.blogspot.tw/2012/07/config-bluetooth-baud-rate-w-arduino.html
3#
 樓主| 發表於 2014-5-20 01:19:17 | 只看該作者
感謝vegewell 果然是baud rate的問題
我以為在 setup() 中的 Serial.begin(9600) 就是設定baud rate....真是大錯特錯.....
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-9-30 15:34 , Processed in 0.179046 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表