Arduino /处理串行通信时出错

时间:2015-03-13 23:22:05

标签: arduino processing

我对Arduino和Processing都比较陌生,我一直在研究利用两者之间串行通信的代码。我的Arduino代码是从压电传感器读取和打印值,然后将值发送到Processing,后者根据值绘制某些形状。代码以前有效,但由于某种原因,它已不再有效。所有内容都会编译,但是当我运行Processing代码时,草图窗口为空并保持为空。几次“错误,禁用serialEvent()”出现了,但我刚刚拔掉了我的Arduino板,关闭了程序,并重新启动了一切。错误不再显示,但我的处理草图仍未显示在屏幕上。有人可以让我知道我的代码有什么问题吗?我非常感谢你的帮助。

Arduino代码:

int ledPin = 13;      
int knockSensor = A0; 
byte val = 0;         
int statePin = LOW;   
int THRESHOLD = 5;  
int sensorReading = 0;

void setup() {
 pinMode(ledPin, OUTPUT); 
 Serial.begin(9600);       
}

void loop() {
  sensorReading = analogRead(knockSensor);    

  if(sensorReading > 0)
  {
    Serial.println(sensorReading, DEC);
  }

  if (sensorReading != 0)
    Serial.println(sensorReading);
  delay(100);  
}

处理代码:

import processing.serial.*;

Serial port;

int centerX = 550;
int centerY = 400;

float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;

void setup(){
  background(255);
  size(1100,800); 
  frameRate(10);
  smooth();

  String portname = "/dev/tty.usbmodem1411"; 

  //String portname = Serial.list()[0];
  port = new Serial(this, portname, 9600);
  println(Serial.list());
  //port.bufferUntil('\n');
}

void drawEllipse(float val)
{
  if(val > 0 && val < 50)
  {
    ellipseX = random(540,560); 
    ellipseY = random(390,410);
    ellipseW = val + 10;
    ellipseH = val + 10;

    stroke(0);
    fill(random(255), random(200,255));
  }
}

void drawLines(float val)
{
  if(val > 50 && val < 70)
  {
    lineX = random(500, 600);
    lineY = random(360, 440);
    lineXX = random(500, 600);
    lineYY = random(360, 440);

    stroke(0);
  }
}

void drawEllipse2(float val)
{
  if(val > 70 && val < 120)
  {
    ellipseXX = random(460, 640); 
    ellipseYY = random(330, 470);
    ellipseWW = val + random(20);
    ellipseHH = val + 10;

    stroke(0);
    fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
  }
}

void serialEvent(Serial port) 
{
  String inString = port.readStringUntil('\n');

  if (inString != null)
  {
      val = Float.parseFloat(inString);
  }

  drawEllipse(val);
  drawLines(val);
  drawEllipse2(val);
  println(val);
}

1 个答案:

答案 0 :(得分:0)

也许使用Serial.write()会更好。所以代码看起来像这样。

Arduino代码:

int ledPin = 13;      
int knockSensor = A0; 
byte val = 0;         
int statePin = LOW;   
int THRESHOLD = 5;  
int sensorReading = 0;

void setup() {
    pinMode(ledPin, OUTPUT); 
    Serial.begin(9600);       
}

void loop() {
    sensorReading = analogRead(knockSensor);    

    if(sensorReading > 0)
    {
        Serial.println(sensorReading, DEC);
    }

    if (sensorReading != 0)
        Serial.write(map(sensorReading, 0, 1023, 0, 255));
    delay(100);  
}

处理代码:

import processing.serial.*;

Serial port;

int centerX = 550;
int centerY = 400;

float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;

void setup(){
    background(255);
    size(1100,800); 
    frameRate(10);
    smooth();

    String portname = "/dev/tty.usbmodem1411"; 

    //String portname = Serial.list()[0];
    port = new Serial(this, portname, 9600);
    println(Serial.list());
    //port.bufferUntil('\n');
}

void drawEllipse(float val)
{
   if(val > 0 && val < 50)
   {
        ellipseX = random(540,560); 
        ellipseY = random(390,410);
        ellipseW = val + 10;
        ellipseH = val + 10;

        stroke(0);
        fill(random(255), random(200,255));
    }
}

void drawLines(float val)
{
    if(val > 50 && val < 70)
    {
        lineX = random(500, 600);
        lineY = random(360, 440);
        lineXX = random(500, 600);
        lineYY = random(360, 440);

        stroke(0);
    }
}

void drawEllipse2(float val)
{
    if(val > 70 && val < 120)
    {
        ellipseXX = random(460, 640); 
        ellipseYY = random(330, 470);
        ellipseWW = val + random(20);
        ellipseHH = val + 10;

        stroke(0);
        fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
    }
}

void serialEvent(Serial port) 
{
    if (0 < port.available()) {
        val = map(port.read(), 0, 255, 0, 1023);
    }

    if (val > 0) {
        drawEllipse(val);
        drawLines(val);
        drawEllipse2(val);
        println(val);
    }
}