2013-05-26 62 views
0

我正在arduino和openframeworks之間進行串行程序。但是,arduino發送了奇怪的數據給openframeworks程序。我無法修復它,請幫助。Arduino在串口上發送 376(中斷數據?)

(Arduino的代碼)

void setup(){ 
    Serial.begin(9600); 
} 

void loop(){ 
    Serial.write('a'); 
    delay(100); 
} 

(在Mac上opneframeworks代碼)

#include "testApp.h" 

ofSerial mySerial; 

//-------------------------------------------------------------- 
void testApp::setup(){ 
    mySerial.setup(0, 9600); 

} 

//-------------------------------------------------------------- 
void testApp::update(){ 

    unsigned char myByte = 0; 
    myByte = mySerial.readByte(); 

    if(myByte == OF_SERIAL_NO_DATA){ 
     cout << "no data was read"; 
    }else if(myByte == OF_SERIAL_ERROR){ 
     cout << "an error occurred"; 
    }else{ 
     cout << "myByte is " << myByte << "\n"; 
    } 

} 

(在Xcode的控制檯)

... 
myByte is \376 
myByte is \376 
myByte is a 
myByte is \376 
myByte is \376 
myByte is \376 
myByte is \376 
... 

似乎在Mac上了openFrameworks了 「\ 376」 時的Arduino沒不發送任何數據。
我的環境是,

  • 的Mac OS獅子
  • 的Xcode V4.3.3 SDK10.6
  • 了openFrameworks v0.7.4的Mac
  • Arduino的IDE v1.0.4
  • 的Arduino UNO(ATMEGA328P)
  • Arduino和我的PC用USB電纜連接

回答

1

認爲這是您的串行緩衝區在您嘗試讀取時不完整的問題 - 當我收到處理或openFrameworks中的奇數數據時,這是10次中的9次,這就是問題所在。

試圖改變自己的update()方法:

void testApp::update(){ 

    unsigned char myByte = 0; 
    if(myByte.available > 8 { 
     myByte = mySerial.readByte(); 
    } 

    if(myByte == OF_SERIAL_NO_DATA){ 
     cout << "no data was read"; 
    }else if(myByte == OF_SERIAL_ERROR){ 
     cout << "an error occurred"; 
    }else{ 
     cout << "myByte is " << myByte << "\n"; 
    } 

} 

由於每oF Serial Class Documentation