03 April 2010

A method for sending OSC messages from Arduino over USB virtual serial port

This is a function for the Arduino compiler in addition to a small terminal program for Mac OS X which together implement a method of acquiring data from the Arduino over USB to be sent as OSC messages from the host computer. The serialOSC program is a simple C/C++ program written using the oscpack library for C++ and code for reading from the serial port by Tod E. Kurt. It can send messages to any port on any host, as specified in the config.txt file, but is currently hard-coded to only send messages named "/arduino/data", with any number of arguments. Due to RS232 limitations, the maximum speed at which messages may be sent without error may vary. In the setup I was testing it with (Arduino Duemillanove at 115.2 kbaud), the maximum error-free rate I could achieve was around 500 Hz for a 4-argument message.

The program has only currently been built and tested for Mac OS X with a PowerPC processor, but should theoretically also compile for Linux/Unix systems. Source requires installation of the oscpack library. I have not written a makefile for release yet, but assuming the standard C libraries are available, and the oscpack library files and headers are installed in a location accessible to the compiler, the source can be compiled using g++ from the source directory with:

g++ -Wall -o serialOSC -loscpack serialOSC.cpp

Also remember to use the proper endianness for your architecture when compiling the oscpack library.

All instructions for usage are in the README.txt file.


serialOSC 0.9 for PowerPC Macs
serialOSC 0.9 Source

The source for the Arduino function is below:


// Function definitions for serialOSC library for Arduino
// For use with serialOSC program
// Nick Donaldson, 2010
#include "WProgram.h"

#ifndef SENDOSC_H
#define SENDOSC_H


void sendOSCData( int* data, int num)
{

//begin message with 2 0xFF followed by comma
Serial.print(0xFF,BYTE);
Serial.print(0xFF,BYTE);
Serial.print(',', BYTE);

// follow by number of arguments
Serial.print(num, BYTE);
Serial.print(',', BYTE);


// arguments
for(int dt=0; dt Serial.print(data[dt]>>8,BYTE);
Serial.print(data[dt],BYTE);
}
Serial.print(',');

// end of message
Serial.print(0xFF,BYTE);
Serial.print(0xFE,BYTE);
Serial.print(',');

return;
};


#endif

No comments:

Post a Comment