Quantcast
Channel: Processing Forum
Viewing all 1768 articles
Browse latest View live

Re : Maximum Number of Items in an Array


how to save serial data received from arduino

$
0
0
hello everyone, i apologize for my bad english yet, hope you can understand what my problem is:
i'm using arduino to take voltage values, then i send them to the serial port in this form:
  1. Serial.println(float,float,float,int,int);
data i send are stored in some array in arduino memory thus this instruction is a part of loop, so i send to the serial port multiple lines of the same type as this:
...
float,float,float,int,int
float,float,float,int,int
float,float,float,int,int
float,float,float,int,int
... and so on

i'd like processing to take these data from the serial port and then save them in some arrays in order analyze and plot.
so i wrote this code for processing:
  1. void serialEvent(Serial port) {
  2.   measure = commPort.readStringUntil('\n');

  3.   int i = 0;
  4.   if (measure != null) {

  5.     String[] valori_misura = new String[5];
  6.     valori_misura = split(measure, ",");

  7.     ps_voltage = parseFloat(valori_misura[0].trim());
  8.     PS_VOLTAGE[i]=ps_voltage;

  9.     c_voltage = parseFloat(valori_misura[1].trim());
  10.     C_VOLTAGE[i] = c_voltage;

  11.     current = parseFloat(valori_misura[2].trim());
  12.     CURRENT[i] = current;

  13.     ps_time = parseInt(valori_misura[3].trim());
  14.     PS_TIME[i] = ps_time;

  15.     c_time = parseInt(valori_misura[4].trim());
  16.     C_TIME[i]=c_time;
  17.      /*println(ps_voltage);
  18.      println(c_voltage);
  19.      println(current);
  20.      println(ps_time);
  21.      println(c_time+"\n");*/
  22.     i++;
  23.   }}
  hoping you've understood the code and what i want to do, i've a problem becouse i'm not able to print values of one of the arrays(eg.PS_VOLTAGE) ;

it's wrong how i save data? where do i have to put the for loop to print data? 

Re : Minim- "Unexpected token: \"

$
0
0
Maybe just download the latest stable release (1.5.1.) and check if the minim examples work there! 

Re : Question for a Newbie

$
0
0
Hello hawk_08,
Try with this one. 

The upper case on Serial should be lower case. Be aware, Processing is CASE SENSITIVE. 


// Example by Tom Igoe
import processing.serial.*;

// The serial port:
Serial myPort;       

// List all the available serial ports:
println(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
  
// Send a capital A out the serial port:
myPort.write(65); 


All the best

Re : Question for a Newbie

$
0
0
Thanks for your answer!

I still get an error message tough...

ArrayIndexOutOfBoundsException: 0

WARNING:  RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 0
    at sketch_121212c.setup(sketch_121212c.java:32)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:662)

Re : Question for a Newbie

$
0
0
Are you you have a Serial Port connected? 

2.x PGraphics thread crash

$
0
0
So I create a buffer to do some work in a separate thread so it does not delay the normal draw. This worked fine in 1.x.  Any thoughts on why this now crashes and how to get around it?

  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. PGraphics buffer;
  4. boolean runthread = false;
  5. ExecutorService executor;
  6. void setup() {
  7.   size(width, height, P3D);
  8.   buffer = createGraphics(width, height, P3D);
  9.   executor = Executors.newFixedThreadPool(1);
  10. }
  11. void draw() {
  12.   //do stuff
  13.   stroke(random(0,255),random(0,255),random(0,255));
  14.   line(random(0,width),random(0,height),random(0,width),random(0,height));
  15.   if (!runthread) {
  16.     runthread = true;
  17.     executor.submit(new RunLoadNodes());
  18.   }
  19. }
  20. void loadbuffer() {
  21.   println("ya");
  22.   buffer.beginDraw();
  23.   //do stuff
  24.   buffer.endDraw();
  25. }
  26. public class RunLoadNodes implements Runnable {
  27.   public RunLoadNodes() {
  28.   }
  29.   public void run() {
  30.       loadbuffer();
  31.   }
  32. }
If it helps, here is the crash info:
Application Specific Information:
Java information:
 Exception type: Bus Error (0xa) at pc=00000000956d6ea4
 
 Java VM: Java HotSpot(TM) Client VM (20.12-b01-434 mixed mode macosx-x86)
 
Current thread (0000000002450400):  JavaThread "pool-1-thread-1" [_thread_in_native, id=-1314140160, stack(00000000b19bd000,00000000b1abd000)]
Stack: [00000000b19bd000,00000000b1abd000]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  jogamp.opengl.gl4.GL4bcImpl.dispatch_glGetError1(J)I+0
j  jogamp.opengl.gl4.GL4bcImpl.glGetError()I+33
j  processing.opengl.PGL.getError()I+3
j  processing.opengl.PGraphicsOpenGL.report(Ljava/lang/String;)V+13
j  processing.opengl.PGraphicsOpenGL.beginDraw()V+3
j  sketch_Dec12a.loadbuffer()V+9

j  sketch_Dec12a$RunLoadNodes.run()V+4
j  java.util.concurrent.Executors$RunnableAdapter.call()Ljava/lang/Object;+4
j  java.util.concurrent.FutureTask$Sync.innerRun()V+30
j  java.util.concurrent.FutureTask.run()V+4
j  java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Ljava/lang/Runnable;)V+59
j  java.util.concurrent.ThreadPoolExecutor$Worker.run()V+28
j  java.lang.Thread.run()V+11
v  ~StubRoutines::call_stub

Re : 2.x PGraphics thread crash

$
0
0
A P2D/P3D buffer is OpenGL-backed now (the old software-based P3D is gone in 2.0). OpenGL cannot be used from a thread different from the main thread that created the GL context.

Re : 2.x PGraphics thread crash

$
0
0
Andres, any way to create another GL context separate from the one being used to draw?  I use this a lot for color picking.  Or perhaps, is their a suitable replacement that may not depend on the GL for this process?

Re : how to save serial data received from arduino

$
0
0
It looks like if you only save values to the first index of your arrays, because "i" is set to "0" at the beginning of the serialEvent()-function. You are not describing what exactly is your problem, do you get wrong values, any eror-messages?

And maybe it would be a good idea to use ArrayLists instead of arrays, so you are more flexible when the amount of values changes.

Re : 2.x PGraphics thread crash

$
0
0
I got this to work... not sure how safe it is though....
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;

  3. PGraphics buffer;
  4. boolean runthread = false;
  5. ExecutorService executor;
  6. PGraphicsOpenGL pgl;
  7. PGL pglx;

  8. void setup() {
  9.   size(width, height, P3D);
  10.   buffer = createGraphics(width, height, P3D);
  11.   executor = Executors.newFixedThreadPool(1);
  12.   pgl = (PGraphicsOpenGL) g;
  13.   pglx = pgl.beginPGL();
  14.    //pglx.context.setGLDebugSynchronous(true);
  15. }
  16. void draw() {
  17.   //do stuff
  18.   stroke(random(0,255),random(0,255),random(0,255));
  19.   line(random(0,width),random(0,height),random(0,width),random(0,height));
  20.   if (!runthread) {
  21.     runthread = true;
  22.     executor.submit(new RunLoadNodes());
  23.   }
  24. }
  25. void loadbuffer() {
  26.   println("ya");
  27.   pglx.context.makeCurrent();
  28.   buffer.beginDraw();
  29.   //do stuff
  30.   buffer.endDraw();
  31.   pglx.context.release();
  32. }
  33. public class RunLoadNodes implements Runnable {
  34.   public RunLoadNodes() {
  35.   }
  36.   public void run() {
  37.       loadbuffer();
  38.   }
  39. }

Strange XML structrue after Facebook call.

$
0
0
I am trying to access profile data via the Facebook Graph Api like described here:

I have to say. I am very unexperienced with XML. 
So this might be a real noob question.


what I get is an XMLElemn which looks like this:

  1. <<?xml version="1.0" encoding="UTF-8"?>
  2. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  3.   <user>
  4.     <uid>1477411208</uid>
  5.     <first_name>Marc</first_name>
  6.     <last_name>Tiedemann</last_name>
  7.   </user>
  8. </Users_getInfo_response>/>

looks good to me. but when I try to access the children, i get null back!
instead everything seems to be root!

  1.         println("childs "+ xml.hasChildren());
  2.         println("root "+ xml.getName());

console output:
  1. childs false
  2. root <?xml version="1.0" encoding="UTF-8"?>
  3. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  4.   <user>
  5.     <uid>1477411208</uid>
  6.     ...


I also read that this method of getting info from the Graph API is kinda outdated. As REST got depriciated.
Does anybody have a running example of how you could do this better?
As said I am very very new to this kind of coding and happy for any hint's in the right direction. 


Re : Strange XML structrue after Facebook call.

$
0
0
How do you initialize the xml variable?
Also, are you using 1.5.1 or 2.0b? The XML handling changes a lot between the two versions. The wiki probably refers to the old method.

Re : Strange XML structrue after Facebook call.

$
0
0
I am using 1.5.1 for now. Would you recon switching to the new one?

I really just did a straight copy and paste from the wiki. So initialization goes like this:

  1.  String xmlResponse = fbCallMethod( new String[] {
  2.                 "method=facebook.Users.getInfo",
  3.                 "uids=" + fbUserIDs,
  4.                 "fields=uid,first_name,last_name", // see link above for more options
  5.                 "format=XML"
  6.         });

  7.  XMLElement xml = new XMLElement( xmlResponse );
  8.         println(xml);

Re : Strange XML structrue after Facebook call.

$
0
0
The page you link to uses: 
usersXml = xml.getChildren( "user" );

So it skips the cruft and jumps straight at the useful data.

Re : Strange XML structrue after Facebook call.

$
0
0
yeah. but then i get null pointer. check out my first entry. there are no childs. just a big root!

Re : Strange XML structrue after Facebook call.

$
0
0
I see the last line:
</Users_getInfo_response>/>
If that's the real data and not a copy / paste error, then the XML is wrong and Processing is confused.

Re : Strange XML structrue after Facebook call.

$
0
0
aha.. he does that when parsing the String to the XMLElement. So the String is right:

  1. <Users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  2.   <user>
  3.     <uid>1477411208</uid>
  4.     <first_name>Marc</first_name>
  5.     <last_name>Tiedemann</last_name>
  6.   </user>
  7. </Users_getInfo_response>


but XMLElement adds the outer brakets:

  1. <<?xml version="1.0" encoding="UTF-8"?>
  2. ...
  3. </Users_getInfo_response>/>

still it shouldn't do this, if i understand right.





      

Re : how to save serial data received from arduino

$
0
0
i kinda solved it with this code:
  1. void serialEvent(Serial port) {
  2.   

  3.   measure = commPort.readStringUntil('\n');


  4.   if (measure != null) {

  5.     String[] valori_misura = new String[5];
  6.     valori_misura = split(measure, ",");

  7.     ps_voltage = parseFloat(valori_misura[0].trim());
  8.     c_voltage = parseFloat(valori_misura[1].trim());
  9.     current = parseFloat(valori_misura[2].trim());
  10.     ps_time = parseInt(valori_misura[3].trim());
  11.     c_time = parseInt(valori_misura[4].trim());

  12.     PS_VOLTAGE = append(PS_VOLTAGE, ps_voltage);
  13.     C_VOLTAGE = append(C_VOLTAGE, c_voltage);
  14.     CURRENT = append(CURRENT, current);
  15.     PS_TIME = append(PS_TIME, ps_time);
  16.     C_TIME = append(C_TIME, c_time);
  17. }}

Re : Strange XML structrue after Facebook call.

$
0
0
Try replacing
XMLElement xml = new XMLElement(xmlResponse);
with
XMLElement xml = XMLElement.parse(xmlResponse);

Viewing all 1768 articles
Browse latest View live