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);

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。