Arduino在串口发送\ 376(数据损坏?)

时间:2013-05-26 02:12:27

标签: xcode serial-port arduino openframeworks

我正在arduino和openframeworks之间进行串行程序。但是arduino向openframeworks程序发送了奇怪的数据。我无法解决,请帮助。

(arduino代码)

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

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

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上的控制台) #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"; } }

当Arduino没有发送任何数据时,Mac上的OpenFrameworks似乎得到了“\ 376” 我的环境是,

  • Mac OS Lion
  • Xcode v4.3.3 SDK10.6
  • 适用于Mac的OpenFrameworks v0.7.4
  • Arduino IDE v1.0.4
  • Arduino uno(atmega328p)
  • Arduino和我的电脑用USB线连接

1 个答案:

答案 0 :(得分:1)

认为当您尝试阅读时,这是您的串行缓冲区不完整的问题 - 当我在Processing或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