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 & HelpSyntax Questions › Help Coding- not very good at java
Page Index Toggle Pages: 1
Help Coding- not very good at java (Read 726 times)
Help Coding- not very good at java
Apr 26th, 2008, 5:48am
 
I don't like doing this, but I am in desperate need of some help.  I don't have a lot of experience with java, but the application I am working with needs to be in java.  I want to send the following to a .csv file.  Its the four analog[n] strings.  The following is as far as I have gotten.  Any help would be awesome.  

import xbee.*;
import processing.serial.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

int[] x = new int[0];
int[] y = new int[0];

// Your Serial Port
Serial port;
// Your XBee Reader object
XBeeReader xbee;

void setup() {
       size(200, 200);
        println("Available serial ports:");
        println(Serial.list());
         port = new Serial(this, Serial.list()[1], 9600);
         xbee = new XBeeReader(this,port);

           println("Setting up Xbee");

           xbee.startXBee();
}

         
     public void xBeeEvent(XBeeReader xbee) {
     // Grab a frame of data
 
     XBeeDataFrame data = xbee.getXBeeReading();
 

 if (data.getApiID() == XBeeDataFrame.ZNET_IOPACKET) {

    int addr = data.getAddress16();

   int rssi = data.getRSSI();
   int totalSamples = data.getTotalSamples();

   for (int n = 0; n < totalSamples; n++) {
   
          int[] analog = data.getAnalog(n);  
     
     if ( addr == 26442){
       
          print( "Leg Monitor");
         print("  Voltage: ");
       for (int i = 0; i < 1; i++) {
         print("AD0=" + analog[0]*0.0012 + " ");
         print("AD1=" + analog[1]*0.0012 + " ");
        //saveStrings("lines.csv", analog);
       
     }
         }
     if ( addr == 17946){
       
          print( "Reflex Hammer");
         print("  Voltage: ");
       for (int i = 0; i < 1; i++) {
         print("X=" + analog[2]*.0012 + " ");
         print("Y=" + analog[1]*.0012 + " ");
     }
         }
 
 }
void saveStrings("project.csv", analog);
     println();
   
   }
 }
 else {
   println("Not I/O data: " + data.getApiID());
 }
}

public void draw() {

}
Re: Help Coding- not very good at java
Reply #1 - May 10th, 2008, 6:32pm
 
Here is a simple example showing how to output CSV data to a file.
Code:
int FIELD_NB = 5;
String PATH = "D:/Temp/ProcessingTest.csv";

void setup()
{
PrintWriter output = createWriter(PATH);
for (int l = 0; l < 10; l++) // 10 lines
{
int[] analog = new int[FIELD_NB];
for (int i = 0; i < 5; i++) // 5 fields per line
{
analog[i] = l * 100 + i; // Just example data
}
WriteArray(output, analog);
}
output.flush();
output.close();
}

void WriteArray(PrintWriter output, int[] array)
{
for (int i = 0; i < array.length; i++)
{
output.print(array[i] + (i < array.length - 1 ? "," : ""));
}
output.println();
}
Page Index Toggle Pages: 1