I am tryng to send an array with values from my Arduino to OF with simple ofSerial functionality and without using firmata.
I am getting data into my app but I am a bit stuck how to handle them.
But how do I handle those bytes in OF? How can i convert those bytes back to an array?
Is there somewhere an example to handle more complex serial data in OF?
Also, in case you want to use the built-in ofSerial with Arduino, I just pushed an example from my class using Arduino / ofSketch (which is just header-only openFrameworks code).
thanks for the fast reply!
I tried the last solution and getting errors while compiling on arduino.
seems the types are not correct. any quick ideas on this?
I tried to convert the String to “const unsigned char” but this didnt workout fine so far.
SerialComSwitch.ino: In function ‘void writeSerialData()’:
SerialComSwitch.ino:59:32: error: invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from SerialComSwitch.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:53:20: error: initializing argument 1 of ‘virtual size_t Print::write(const uint8_t*, size_t)’ [-fpermissive]
virtual size_t write(const uint8_t *buffer, size_t size);
That was the problem. I used the Arduino version of my Distribution. It was to old.
I am now using ofxSerial and the haiku generator. From there I should be able to send the data I like to use.
im am sending a string like following from arduino. Thats works fine:
String index = String(1);
String val = String(random(1024));
Serial.print(index + "," + val);
Serial.println("");
But when spliting this to get the single values and put them into a vector I am having segmentation errors.
Here is the collected OF code:
vector<int> values;
values.resize(100);
for(int i = 0; i < values.size(); i++){
values[i] = 0; // set value using index
}
void ofApp::onSerialBuffer(const ofx::IO::SerialBufferEventArgs& args)
{
// Buffers will show up here when the marker character is found.
//SerialMessage message(args.getBuffer().toString(), "", 500);
vector<string> result = ofSplitString(args.getBuffer().toString(),",");
if(ofToInt(result[0]) < values.size()){
if(ofToInt(result[1]) < 1024){
values[ofToInt(result[0])] = ofToInt(result[1]);
}
}
//cout << values[1] << endl;
//serialMessages.push_back(message);
}