Sending array from arduino to of

Hey Guys,

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.

On arduino I simply send an array like following:

byte serialArrayOne[]  = {1,2,3,4,5,6,7,8};
Serial.write(serialArrayOne,8);

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?

Check out ofxSerial (also requires https://github.com/bakercp/ofxIO).

Particularly check out the https://github.com/bakercp/ofxSerial/tree/master/example_packet_serial (uses COBS or SLIP encoding to make it easier to mark data packet boundaries) https://github.com/bakercp/ofxSerial/tree/master/example_echo (just shows how to write data and get it back) and https://github.com/bakercp/ofxSerial/tree/master/example_buffered_serial_device (shows how to do basic packet boundary marking using new lines) examples. Each has Arduino code that corresponds to it in the example folder. The source files / examples themselves should be pretty well documented.

If you have any questions or issues, you can leave them at the repo or I’m happy to help here.

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

https://github.com/SAIC-ATS/ARTTECH-5010/tree/master/Week_4/SerialFullDuplex

The ofSketch portion will answer your question of reading in and writing arrays from Arduino more directly.

Hey bakercp,

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

Do you have the latest version of Arduino? Could you post your code?

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.

Thanks!

1 Like

Super! Let me know if you have any questions.

Yes, :smile:

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

or maybe you have a better idea to send some values assigned to channels via ofxSerial.

data i want to send:

array[0] = 1024;
array[2] = 1024

and further