We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Update .xml file with sensor data
Page Index Toggle Pages: 1
Update .xml file with sensor data (Read 721 times)
Update .xml file with sensor data
Apr 2nd, 2010, 1:29am
 
Hi all, hoping for a little help here.

I am trying to continously update a .xml file with some sensordata from my Arduino - the xml is to be parsed by Unity3d for joystick input at a later stage.

I only need the latest data (joystick position), so I am looking for a way to update my .xml, overwriting the data from the last read. Right now I am able to read the desired data and print it to the file, appending a new line for each new read.

Any help is greatly appreciated  Smiley

/Esben

Processing code:
Code:
import processing.serial.*;

Serial mySerial;
PrintWriter output;

void setup() {
  mySerial = new Serial(this, Serial.list()[0], 9600);
  output = createWriter("data.xml");
}

void draw() {
 if (mySerial.available() > 0) {
        String value = mySerial.readString();
        output.print(value);
        output.flush();
        delay(100);
  }
}


Arduino code:
Code:
int pullPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int pullValue = 0;       // variable to store the value coming from the sensor
int valNew = 0;

void setup() {
 pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
 Serial.begin(9600);      // open the serial port at 9600 bps:
 loop();
}

int treatValue(int data) {
 return (data * 9 / 1024) + 48;
}

void loop() {
 pullValue = analogRead(pullPin);    // read the value from the sensor
 Serial.print("<");
 Serial.print(pullValue);
 Serial.println(">");
}
Re: Update .xml file with sensor data
Reply #1 - Apr 2nd, 2010, 3:06am
 
Close the writer and open a new one in draw() ?

I am not sure that exporting a XML file on each draw() call is efficient. XML is great for sharing data, but updating an XML file so often looks a bit weird to me.

Don't you need on-demand XML export only?
Re: Update .xml file with sensor data
Reply #2 - Apr 2nd, 2010, 10:06am
 
Hi antiplastik

Thanks for your reply - I tried your suggestion, and I think you are right, it is not possible reach good results by updating an xml file.

I am not using a regular joystick but a Gametrak device, that lets med output 3 variables, one for each axis, and I want to feed that information to the Unity3d game engine as often as possible, so that I can get a position in a 3D space.

This means, that an on-demand XML export option would not be suitable, yes?

Thanks again for your reply. Ideas for other methods to pass the data would be very welcome Smiley

/Esben
Page Index Toggle Pages: 1