处理来自Arduino的串行读取导致Java PApplet错误

时间:2015-04-07 15:22:26

标签: java arduino processing

我正在阅读arduino草图中的序列数据,我上传到一个青少年2.0 /这也发生在运行常规arduino时。然后我将分裂字符串并转换为具有Processing的int()函数的整数数组,以便能够触发声音。 arduino草图正在读取电容传感器数据。它运行良好约10-15秒,然后开始抛出以下错误。


 Exception in thread "Animation Thread" java.lang.NullPointerException
        at processing.core.PApplet.parseInt(PApplet.java:9127)
        at processing.core.PApplet.parseInt(PApplet.java:9113)
        at NightGames_Tree.draw(NightGames_Tree.java:59)
        at processing.core.PApplet.handleDraw(PApplet.java:2266)
        at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
        at processing.core.PApplet.run(PApplet.java:2140)
        at java.lang.Thread.run(Thread.java:695)

我的代码如下: 在Arduino:

 #include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        

void setup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
}

void loop()                    
{
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    

    Serial.print(total1);                  // print sensor output 1
    Serial.println("\t");

    delay(100);                            
}

处理中:

    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;

    import processing.serial.*;

    Minim minim;
    AudioPlayer tree1;
    AudioPlayer tree2;

    Serial myPort;
    String val;

void setup(){

    String portName = Serial.list()[12]; // calls on the port which teensy is on
    myPort = new Serial(this, portName, 9600);
    println(Serial.list()); // prints list of ports that processing can access

    //setup sound

    minim = new Minim(this);
    tree1 = minim.loadFile("tree1.aif");

}

void draw()
{


    if (myPort.available() > 0) {
        val = myPort.readStringUntil('\n');
        int[] list = int(split(val, '\t')); // splits string into list based on tab after
    int sum = 0;
    int sum1 = 0;
 //  print(list.length);

      sum = sum + list[0];
      sum1 = sum1 + list[1];

    print(sum);
    print('\t');
    print(sum1);
    print('\t');
    print('\n');

   //print(val);

    if (sum1 > 500) {
      tree1.play(0);
    } else if (sum1 <500){
    }

  }
}

运行一段时间后出现错误时,Processing会突出显示这行代码

int[] list = int(split(val, '\t'));

提前感谢您的帮助。我在Mac上运行它。

2 个答案:

答案 0 :(得分:0)

我使用Processing 3.0a4和Arduino 1.6.3在Windows机器上运行它 在Arduino上,我评论了与电容式传感器库有关的所有内容,因为我没有。 `

//#include <CapacitiveSensor.h>
//CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        
void setup()                    
{
// cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     
// turn off autocalibrate on channel 1 - just as an example

Serial.begin(9600);     }

void loop()                    
{
long start = millis();
// long total1 =  cs_4_2.capacitiveSensor(30);

Serial.print(millis() - start);        
// check on performance in milliseconds
Serial.print("\t");                    

//Serial.print(total1);                  // print sensor output 1
Serial.println("\t");

delay(100);                            
}

在处理中,我用声音文件注释掉了这一行。 我得到的错误指向这一行:

int [] list = int(split(val,&#39; \ t&#39;));

似乎存在一种类型不匹配,它允许列表流一小段但是它们会阻塞处理器。

答案 1 :(得分:0)

结果是处理检测到空对象,因此添加对空对象的检查解决了问题。精简代码是:

import processing.serial.*;

Serial myPort;
String val;

void setup(){
  String portName = Serial.list()[12]; // calls on the port which teensy is on
  myPort = new Serial(this, portName, 9600);
} 

void draw()
{
  if (myPort.available() > 0) {
    val = myPort.readStringUntil('\n');
    if (val!=null){
    int[] list = int(split(val, '\t')); // splits string into list based on tab 

    int sum = 0;
    int sum1 = 0;
    int sum2 = 0;

    sum = sum + list[0];
    sum1 = sum1 + list[1];
    sum2 = sum2 + list[2];

    print(sum);
    print('\t');
    print(sum1);
    print('\t');
    print(sum2);
    print('\t');
    print('\n');
    }
  }
}