1

我正在尝试绘制一些通过串行端口读取的传感器数据。我找到了 2D 绘图仪的处理示例代码,我正在尝试对其进行更改以适用于我的应用程序。

不过,编程有点过头了。我可以看到它正在从三个方程类中获取三行的数据。我想用变量或指向最新传感器更新的东西替换读取 mouseX 的部分。传感器数据在 voidDraw()循环中不断更新。

/**
 * RollingGraph
 * This sketch makes ise of the RollingLine2DTrace object to
 * draw a dynamically updated plot.
 */

import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;

class eq implements ILine2DEquation{
    public double computePoint(double x,int pos) {
        return mouseX;
    }
}

class eq2 implements ILine2DEquation{
    public double computePoint(double x,int pos) {
        return mouseY;
    }
}

class eq3 implements ILine2DEquation{
public double computePoint(double x,int pos) {
    if(mousePressed)
        return 400;
    else
        return 0;
    }
}

RollingLine2DTrace r,r2,r3;
Graph2D g;

void setup(){
    size(600,300);

    r  = new RollingLine2DTrace(new eq() ,100,0.1f);
    r.setTraceColour(0, 255, 0);

    r2 = new RollingLine2DTrace(new eq2(),100,0.1f);
    r2.setTraceColour(255, 0, 0);

    r3 = new RollingLine2DTrace(new eq3(),100,0.1f);
    r3.setTraceColour(0, 0, 255);

    g = new Graph2D(this, 400, 200, false);
    g.setYAxisMax(600);
    g.addTrace(r);
    g.addTrace(r2);
    g.addTrace(r3);
    g.position.y = 50;
    g.position.x = 100;
    g.setYAxisTickSpacing(100);
    g.setXAxisMax(5f);
}

void draw(){
    background(200);
    g.draw();
}
4

1 回答 1

2

不完全确定您拥有的代码是否是您所需要的,但这里有一个简单的程序,它接受串行输入并制作折线图。希望这可以帮助。

    import processing.serial.*;
    Serial myPort;
    int x = 0;


    void setup() {
      size(600, 400);
      println(Serial.list());  //list of available serial ports
      String portName = Serial.list()[0]; //replace 0 with whatever port you want to use.
      myPort = new Serial(this, portName, 9600);
    }


    void draw() {
    }


    void serialEvent(Serial myPort) {
      int inByte = myPort.read();
      println(inByte);
      stroke(90, 76, 99);
      //vertical line with height varying according to input
      line(x, height, x, height - inByte); 
      if (x >=width) {
        x=0;
        background(0);
      }
      x++;
    }
于 2012-12-09T18:24:26.807 回答