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 › Serial Port Issues when using Java structure
Page Index Toggle Pages: 1
Serial Port Issues when using Java structure (Read 1143 times)
Serial Port Issues when using Java structure
Feb 17th, 2007, 4:58am
 
Having some issues with this piece of code I've designed to handle input from a Wiring Board:

// Luke George
// 3rd Year Music Informatics Bsc
// January - March 2007

//Import necessary packages
import processing.serial.*;

public class SerialIn extends PApplet{

 //Declare variables
 float[] data;
 Serial wiringPort;
 String wiringIn = null;
 

 public SerialIn() {
   
   //Create new float array for storing input values
   data = new float[3];
   
   //Print Connections to terminal
   println(Serial.list()[0]);
   
   //Create a new Serial connection to retreive data from
   //the board through
   wiringPort = new Serial(this, Serial.list()[0], 9600);

   //clear the buffer ready for data
   wiringPort.clear();
   wiringIn = wiringPort.readString();
   wiringIn = null;
   readData();
 }
 
 //Looping method which takes data from serial
 //port and places it into the data array.
 void readData(){
   while(wiringPort.available() > 0){
     wiringIn = wiringPort.readString();
     String[] list = new String[6];
     list = split(wiringIn);
     for(int i = 0; i < list.length; i = i + 1){

       if(list[i].equals("sens0:") && i <(list.length-1)){
         float value = float(list[i+1]);
         data[0] = value;
         delay(10);
       }

       if(list[i].equals("sens1:") && i <(list.length-1)){
         float value = float(list[i+1]);
         data[1] = value;
         delay(10);
       }

       if(list[i].equals("sens2:") && i <(list.length-1)){
         float value = float(list[i+1]);
         data[2] = value;
         delay(10);
       }
       
     }
   }
 }
 
 //Retrieve values from data Array
 float[] getSensorData(){
   return data;
 }

}

This runs as an inner class to another Tab called Manager which creates an instance of this class at runtime.

I'm basically getting this at runtime:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7

RXTX Warning:  Removing stale lock file. /var/lock/LCK..ttyUSB0

/dev/ttyUSB0

java.lang.NullPointerException

at processing.core.PApplet.registerNoArgs(PApplet.java:758)

at processing.core.PApplet.registerDispose(PApplet.java:749)

at processing.serial.Serial.<init>(Serial.java:156)

at processing.serial.Serial.<init>(Serial.java:102)

at Temporary_9905_9086$SerialIn.<init>(Temporary_9905_9086.java:217)

java.lang.RuntimeException: Could not register dispose + () for processing.serial.Serial@1d9fd51

at processing.core.PApplet.die(PApplet.java:2336)

at processing.core.PApplet.die(PApplet.java:2354)

at processing.core.PApplet.registerNoArgs(PApplet.java:761)

at processing.core.PApplet.registerDispose(PApplet.java:749)

at processing.serial.Serial.<init>(Serial.java:156)

at processing.serial.Serial.<init>(Serial.java:102)

at Temporary_9905_9086$SerialIn.<init>(Temporary_9905_9086.java:217)

at Temporary_9905_9086.setup(Temporary_9905_9086.java:16)

at processing.core.PApplet.handleDisplay(PApplet.java:1281)

at processing.core.PGraphics.requestDisplay(PGraphics.java:564)

at processing.core.PApplet.run(PApplet.java:1450)

at java.lang.Thread.run(Unknown Source)


Its falling over at:

wiringPort = new Serial(this, Serial.list()[0], 9600);


Anyone offer a solution please? =)
Re: Serial Port Issues when using Java structure
Reply #1 - Feb 17th, 2007, 5:29pm
 
you can't create inner classes of type PApplet, that's gonna make a mess.
Re: Serial Port Issues when using Java structure
Reply #2 - Feb 18th, 2007, 6:52am
 
Ok yeah sorry that was pretty n00bish.  I've done some more reading; my ultimate goal is to get the part of my program that collects data from the serial port to run in a separate thread, to that end i've created the following.  Is there any chance someone can help me out a little here? I think my main problem is which PApplet i should be referencing in creating the Serial Object 'wiringPort'...........

Apologies if this is elementary.

Code as follows:

// Luke George
// 3rd Year Music Informatics Bsc
// January - March 2007

//Import necessary packages
import processing.serial.*;
import processing.core.*;

public class SerialIn {

 //Declare variables
 float[] data;
 Serial wiringPort;
 String wiringIn = null;
 PApplet parent;
 

 public SerialIn() {
   
   //Create new float array for storing input values
   data = new float[3];
   
   parent = new PApplet();
   
   System.out.println(Serial.list()[0]);
   
   //Create a new Serial connection to retreive data from
   //the board through
   wiringPort = new Serial(parent, Serial.list()[0], 9600);

   //clear the buffer ready for data
   wiringPort.clear();
   wiringIn = wiringPort.readString();
   wiringIn = null;
   readData();
 }

 //Looping method which takes data from serial
 //port and places it into the data array.
 void readData(){
   while(wiringPort.available() > 0){
     wiringIn = wiringPort.readString();
     String[] list = new String[6];
     list = wiringIn.split("");
     for(int i = 0; i < list.length; i = i + 1){

       if(list[i].equals("sens0:") && i < (list.length - 1)){
         float value = (float) new Integer(Integer.parseInt(list[i+1])).floatValue();
         data[0] = value;
         //delay(10);
       }

       if(list[i].equals("sens1:") && i < (list.length - 1)){
         float value = (float) new Integer(Integer.parseInt(list[i+1])).floatValue();
         data[1] = value;
         //delay(10);
       }

       if(list[i].equals("sens2:") && i < (list.length - 1)){
         float value = (float) new Integer(Integer.parseInt(list[i+1])).floatValue();
         data[2] = value;
         //delay(10);
       }
       
     }
   }
 }
 
 //Retrieve values from data Array
 float[] getSensorData(){
   return data;
 }

}



Error upon execution:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path

at java.lang.ClassLoader.loadLibrary(Unknown Source)

at java.lang.Runtime.loadLibrary0(Unknown Source)

at java.lang.System.loadLibrary(Unknown Source)

at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)

java.lang.RuntimeException: Error inside Serial.ports()

at processing.serial.Serial.errorMessage(Serial.java:575)

at processing.serial.Serial.list(Serial.java:556)

at SerialIn.<init>(SerialIn.java:25)

at Temporary_5142_9826.setup(Temporary_5142_9826.java:17)

at processing.core.PApplet.handleDisplay(PApplet.java:1281)

at processing.core.PGraphics.requestDisplay(PGraphics.java:564)

at processing.core.PApplet.run(PApplet.java:1450)

at java.lang.Thread.run(Unknown Source)


Thanks in advance!


Re: Serial Port Issues when using Java structure
Reply #3 - Feb 18th, 2007, 12:09pm
 
The PApplet you should be using to create the Serial object is the main one, which shoudl be the only one.

So you probbaly need your SerialIn class to take a PApplet in the constructor, which you then pass to the Serial.

e.g.
Code:

//main bit of sketch
SerialIn SI;

void setup()
{
size(200,200);
SI=new SerialIn(this);
}
//later in your stuff

class SerialIn { //doesn't need the public, everything is
//public in processing
SerialIn(PApplet p)
{
wiringPort=new Serial(p,Serial.list()[0],9600);
}
}
Page Index Toggle Pages: 1