I was searching for a way to exchange data between an Arduino/ArduPilot and a Java application. There are already several solutions to do this but none of them really met my requirements. So I decided to write a library that implements a mechanism called serialization/deserialization. The now called SimpleSerialization library allows you to define a data structure (or object) that will be converted into a stream which then can be sent via a serial connection.DownloadsThe distribution: SimpleSerialization-1.0.zipThe project page: code.google.com/p/simpleserialization.What you can do with it- debug an Arduino application in a very convenient way- remote control an Arduino- setup a hardware-in-the-loop simulation- and moreAn example SimpleSerialization application could be to feed an Arduino with GPS data at 5Hz and attitude data at 30Hz. In return the Arduino would send the calculated actuator commands back at a rate of 10Hz. All this data would be exchanged via a single serial connection.Requirements- an Arduino or ArduPilot- a serial connection from a PC to the Arduino- and additional 2KB of flash memory when used with the ArduPilot- the Arduino IDE- a Java IDE (e.g. Processing or Eclipse)


The SimpleSerialization library is part of my UAV Playground project where I explore various aspects of UAVs like microcontrollers, simulators and remote controlled model airplanes.
E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones

Comments

  • The other edit i forgot  above was to change

    #include "WProgram.h"

    to

    #include "Arduino.h"

    at the top of the c++ header file SimpleSerialization.h .

  • PS, if you are using OS X/Linux or similar, you will also need to set the correct serial port in the processing example sketches. eg

    String serialPort = Serial.list()[0];

    instead of

    String serialPort = "COM4";

    N

  • Hi,

    I recently got SimpleSerialization working with the current Arduino1.0.1 .

    I found I needed to edit the file SimpleSerialization.cpp at two places :

    (i) near the top of the file I commented out the lines

    void* operator new(size_t size) { return malloc(size); }
    void operator delete(void* ptr) { free(ptr); }

    as these seem to cause a compile error with Arduino1.0.1 .

    (ii) at line 362 I changed

    port->print(buffer[i]);

    to

    port->write(buffer[i]);

    so that it writes binary bytes, not ascii numerals. (Previously Serial.print() used to send bytes as binary, but in Arduino1.0.1 this has been changed to Serial.write()  .)

    Thanks to Jaron

  • Zachary, there are some files missing. Could you send me the complete sketch (files) by e-mail?
  • Sorry looks like the two includes at the top got a little screwed up one is for the SoftwareSerial, the other is for SimpleSerial

    Also I forgot the cpp file for navDatanavData.cpp
  • Jaron, sorry to keep bothering you but I am continually being hung up by things. Below I have attached some code and the support files. Running this causes the Arduino to reboot every time it gets to the serA.write(&navData); line. What am I doing wrong here ?

    #include
    #include "debugData.h"
    #include "navData.h"
    #include "servoData.h"
    #include "waypointData.h"
    #include

    //used for debugging
    int rxPin = 2;
    int txPin = 3;
    SoftwareSerial debug = SoftwareSerial(rxPin, txPin);

    //packet types
    //debugData debugD;
    navData navD;
    //servoData servoD;
    //waypointData wayD;
    //serial connection
    SerializationSerialConnection serA;

    // navData fields
    char title[32];
    float speeD;
    float lat;
    float lon;
    float heading;
    int alt;
    int sat_num;
    int current_way;
    int dist_toWay;
    int mode;



    void setup()
    {
    //used for debugging
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);

    debug.begin(9600);


    //set update callback
    navD.setUpdateCallback(&navUpdate);
    //wayD.setUpdateCallback(&waypointUpdate);
    //servoD.setUpdateCallback(&servoUpdate);

    // setup serial port
    serA.begin(&Serial,9600);

    serA.addDeserializableData(&navD);
    // serA.addDeserializableData(&wayD);
    debug.println("Program starting");
    }

    void loop() {

    // read in from serial port

    if(Serial.available() > 0)
    {
    debug.println("recieved Data");
    serA.processInput();

    }
    //xbee and ground station
    //serB.processInput(); //ardupilot

    // writing
    speeD = 100.9;
    lat = 88.9;
    lon= 100.23;
    heading = 180.1;
    alt = micros();
    sat_num = 2;
    current_way= 4;
    dist_toWay= 200;
    mode = 0;
    setupNav();
    debug.println("Attempting to send data");
    serA.write(&navD); // causes reboot
    debug.println("Data Sent");
    delay(1000);

    }
    void setupNav()
    {
    navD.setTitle("navD");
    navD.set_Speed(speeD);
    navD.setLat(lat);
    navD.setLon(100.23);
    navD.setHeading(heading);
    navD.setAlt(alt);
    navD.setSatNum(sat_num);
    navD.setCurrentWaypoint(current_way);
    navD.setDisToWaypoint(dist_toWay);
    navD.setMode(mode);
    }
    /* This is the callback function that is called whenever the data is updated.
    */
    void navUpdate(SerializationData *data)
    {
    debug.println("Nav data received");
    if(strcmp(((navData *)data)->getTitle(),"navData") ==0)
    {
    debug.print("Mode set to: ");
    debug.println(((navData *)data)->getMode());
    }
    }SimpleSerialization.hSimpleSerialization.cppnavData.h
  • I didn't realize that you where trying to make an instance of an instance (SimpleSerialization) in your previous post. That's the reason why 1. doesn't work.

    In 2. you are doing it correctly by instantiating the SerializationSerialConnection class. I can compile that without errors in a 0015 and a 0016 version of the Arduino IDE. What version are you using?

    You could recompile the SimpleSerialization library:
    - quit the Arduino IDE
    - delete the file arduino-00xx\hardware\libraries\SimpleSerialization-1.0\SimpleSerialization.o
    - start the Arduino IDE

    If all that doesn't help you could comment out the line 147 of the SimpleSerialization.h file:
    //extern SerializationSerialConnection SimpleSerialization;
    But the you couldn't use the predefined SimpleSerialization object anymore, which is no problem at all if you create your own instances of the SerializationSerialConnection class.
  • Jaron,

    After reviewing the code again I too see no problem however I am still stuck trying to declare multiple instances. Below are two examples that I have tried and the errors that go along with them.

    1. #include "SimpleSerialization.h"
    SimpleSerialization serA;
    error: 'SimpleSerialization' does not name a type In function 'void setup()':

    2. #include "SimpleSerialization.h"
    SerializationSerialConnection serA;
    error: 'SerializationSerialConnection SimpleSerialization()' redeclared as different kind of symbolC:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\build22550.tmp\/SimpleSerialization.h:147: error: previous declaration of 'SerializationSerialConnection SimpleSerialization'

    I can see a reason for either of these errors, maybe you can .....

    Thank You
    Zachary R Long
  • I haven't tried it yet but I don't see any reason why this shouldn't work. I've tried to keep the examples as simple as possible and that's why I didn't show the initialization with a different port.

    It's nice to see people digging into the code and using the undocumented features!
  • Jaron,

    Is it possible to declare multiple instances of the SimpleSerialization each pointing at a separate serial port. Forgive the extra stuff in the code...

    Thanks

    EX:

    SimpleSerialization serA;
    SimpleSerialization serB;

    void setup()
    {

    navD.setUpdateCallback(&navUpdate);
    wayD.setUpdateCallback(&waypointUpdate);
    servoD.setUpdateCallback(&servoUpdate);


    serA.begin(&Serial,9600);
    serB.begin(&Serial1,9600);


    serA.addDeserializableData(&navD);
    serA.addDeserializableData(&wayD);
    serB.addDeserializableData(&navD);
    serB.addDeserializableData(&servoD);


    }

    void loop() {

    // read in from serial port
    serA.processInput(); //xbee
    serB.processInput(); //ardupilot
This reply was deleted.