Using serial in custom library
in
Library and Tool Development
•
1 year ago
Hi all.
I am creating a custom library, where I need to use the serial library. I am doing it in Eclipse.
What i want to do is:
I want the custom library to handle serial connection, so that part will essentially be invisible to users of the library. The custom library should read serial data, format it and via an event send it to the parent (PApplet - the actual Processing sketch). This means that it should not be necessary to import serial in the main sketch.
The main sketch:
- import dulRadio.*;
- import processing.opengl.*;
- Integer[] res=null;
- void setup() {
- frameRate(60);
- size(screen.width, screen.height,OPENGL);
- //Ani.init(this);
- new DULRadio(this, "COM79", 9600, 13);
- }
- void draw() {
- background(125);
- //text("received: " + inString, 10,50);
- if(res!=null && res.length==4){
- float m=map(res[1],-255,255,0.5,1.5);
- float m2=map(res[2],-255,255,0.5,1.5);
- translate(width/2, height/2);
- pushMatrix();
- rotateZ(PI*m);
- rotateX(PI*m2);
- box(100);
- popMatrix();
- }
- }
- void dulEvent(DULRadio d){
- res=d.getDULData();
- }
My custom library:
- package dulRadio;
- import java.lang.reflect.Method;
- import processing.core.*;
- import processing.serial.*;
- public class DULRadio{
- Method dulRadio;
- PApplet parent;
- Serial s;
- int delimiter;
- Integer[] res=null;
- public DULRadio(PApplet parent, String comport, int baud, int del) {
- this.parent = parent;
- s = new Serial(parent, comport, baud);
- s.setDTR(true);
- s.bufferUntil(del);
- // Register events form parents
- parent.registerDispose(this);
- // Register events from this
- try {
- dulRadio = parent.getClass().getMethod("dulEvent",
- new Class[] { DULRadio.class });
- } catch (Exception e) {
- // no such method, or an error.. which is fine, just ignore
- }
- }
- public Integer[] getDULData(){
- return res;
- }
- void serialEvent(Serial p) {
- res = formatDULData(p.readString());
- makeEvent();
- }
- // then later, to fire that event
- public void makeEvent() {
- if (dulRadio != null) {
- try {
- dulRadio.invoke(parent, new Object[] { this });
- } catch (Exception e) {
- System.err.println("Disabling dulEvent()");
- e.printStackTrace();
- dulRadio = null;
- }
- }
- }
- /**
- * Formatting serial data retrieved from DUL Radio. Returns the integer
- * values from ACC og ADC. To check whether result is ADC(3) og ACC(4),
- * check length of result.
- *
- * IMPORTANT: Messages are not yet implemented (last in array). Can be off,
- * inactive and others. Check case sensitivity!!!
- */
- Integer[] formatDULData(String str) {
- Integer[] result = null;
- String delims = "[:(,) ]+";
- String[] tokens = str.split(delims);
- // ADC
- if (tokens.length == 5) {
- result = new Integer[3];
- result[0] = Integer.parseInt(tokens[0], 10);
- result[1] = Integer.parseInt(tokens[2], 16);
- result[2] = Integer.parseInt(tokens[3], 16);
- }
- // ACC
- if (tokens.length == 6) {
- result = new Integer[4];
- result[0] = Integer.parseInt(tokens[0], 10);
- result[1] = Integer.parseInt(tokens[2], 16);
- result[2] = Integer.parseInt(tokens[3], 16);
- result[3] = Integer.parseInt(tokens[4], 16);
- }
- return result;
- }
- public void dispose() {
- s.stop();
- }
- }
When I try the above I get the following error:
- Exception in thread "Animation Thread" java.lang.NoClassDefFoundError: processing/serial/Serial
- at dulRadio.DULRadio.<init>(Unknown Source)
- at sketch_jan26a.setup(sketch_jan26a.java:33)
- at processing.core.PApplet.handleDraw(Unknown Source)
- at processing.core.PApplet.run(Unknown Source)
- at java.lang.Thread.run(Thread.java:662)
- Caused by: java.lang.ClassNotFoundException: processing.serial.Serial
- at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
- ... 5 more
Any Suggestions?
Regards
1