我正在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” 我的环境是,
答案 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";
}
}