2010年11月24日 星期三

在Linux系統讓Qt 讀取經由 Bluetooth spp傳進來的資料流

以Ubuntu為例

1. 先以 bluez 配對。

2. 下 hcitool scan 指令,取得欲連線設備的 BD Address (本例為 00:0E:00:05:63:20)。

3. 下 rfcomm connect 0 00:0E:00:05:63:20 1 指令連結設備的spp profile,連結成功即會在/dev中產生一個device file,名為 rfcomm0。

4. 連結成功後可用
  • cat /dev/rfcomm0 od -c 或
  • hexdump /dev/rfcomm0

測試是否可讀取。

5. 之後即可在Qt中使用qextserialport做處理。

6. rfcomm release 0 指令可釋放 rfcomm0 device 。

ps. 之前介紹的方法太過繁雜,此方法較為簡單。

在VirtualBox的Ubuntu掛載檔案分享

1. 先在Windows裡的VirtualBox設定欲分享的資料夾(本例為share)
2. 在Ubuntu的桌面建立新資料夾(本例為/home/denver/桌面/share)
3. 下指令:sudo mount -t vboxsf 分享資料夾名稱 掛載路徑 。
例:sudo mount -t vboxsf share /home/denver/桌面/share。
4. 之後即可在兩個os中使用此資料夾互通檔案。

2010年11月18日 星期四

qextserialport的用法(Linux)

1. 至sourceforge下載 source code (qextserialport-1.2win-alpha.zip),並解壓縮。

2. 將
  • posix_qextserialport.h
  • posix_qextserialport.cpp
  • qextserialbase.h
  • qextserialbase.cpp

四個檔案加入專案中。

3. 在專案主程式標頭檔中加入 #include posix_qextserialport.h

4. 之後即可在主程式中使用qextserialport函式。

EX:

1. 在主程式標頭檔中的 private: 中加入宣告

  • Posix_QextSerialPort *myCom;

2. 在主程式中加入Serial Port控制設定:
  • myCom = new Posix_QextSerialPort(SerialPort, QextSerialBase::Polling);
  • myCom -> open(QIODevice::ReadWrite);
  • myCom -> setBaudRate(BAUD9600);
  • myCom -> setDataBits(DATA_8);
  • myCom -> setParity(PAR_NONE);
  • myCom -> setStopBits(STOP_1);
  • myCom -> setFlowControl(FLOW_OFF);
  • myCom -> setTimeout(10);

3. 宣告 QByteArray message = myCom -> readAll().toHex(); 讀進來之數據會存於message變數中,可依需求作處理。

※Linux只能使用Polling的方式。

※SerialPort為/dev/中Serial Port的device file name

※以Linux為例:

  • pl2303 USB轉RS232之device file name為ttyUSB0、ttyUSB1、ttyUSB2.....
  • bluetooth spp之device file name為rfcomm0、rfcomm1、rfcomm2.....

※timeout要設短一點,如:myCom->setTimeout(10);

在windows 7下手動安裝qt 4.7.1

為了使用 static compile,所以 Qt 使用 MinGW 自行 make,Qt Creator IDE 則使用官網下載的 windows binary 安裝,再設定好 qmake 的位置即可。

1. 下載各個所需的檔案:

  • mingw-get-inst-20101030.exe
  • qt-everywhere-opensource-src-4.7.1.tar.gz
  • qt-creator-win-opensource-2.0.1.exe

2. 先安裝好 MinGW 與 Qt Creator IDE。

3. 在 windows 的環境變數中加入使用者變數 PATH,值為:C:\MinGW\bin;C:\MinGW\include;C:\MinGW\lib;D:\Qt\4.7.1\bin (請依自己系統環境設定)

4. 將 qt-everywhere-opensource-src-4.7.1.tar.gz 解壓縮到欲安裝的位置 (本例為 D:\Qt\4.7.1\bin ),並進入此資料夾。

5. 下指令 configure -debug-and-release -opensource -static -platform win32-g++ (參數請依需自行加入、設定)

6. configure 完成後下指令 mingw32-make。

7. make 完成後打開 Qt Creator,在 Tools → Options → Qt4 → Qt Versions 中加入自行 make 的 qmake位置路徑,完成後即可使用。