将Arduino Comm数据移植到.txt中

时间:2015-11-29 08:33:11

标签: java io arduino processing

我正在尝试使用Processing脚本在arduino的通道上转储信号数据。目前我的处理代码通过COM3读取数据,我遇到了处理脚本的问题,因此它只将一行保存到.txt中,并在打开它时不断刷新该行。加速度计/陀螺仪信号和处理脚本的arduino代码可以从Sparkfun的网站上找到:

#include "SparkFunLSM6DS3.h"
#include "Wire.h"
#include "SPI.h"

LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000); //relax...
  Serial.println("Processor came out of reset.\n");
//  Serial.println(value);

  //Call .begin() to configure the IMU
  myIMU.begin();
  //Over-ride default settings if desired
  myIMU.settings.gyroEnabled = 1;  //Can be 0 or 1
  myIMU.settings.gyroRange = 2000;   //Max deg/s.  Can be: 125, 245
  myIMU.settings.gyroSampleRate = 833;   //Hz.  Can be: 13, 26, 52, 104,  208, 416, 833, 1666
  myIMU.settings.gyroBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
  myIMU.settings.gyroFifoEnabled = 1;  //Set to include gyro in FIFO  
  myIMU.settings.gyroFifoDecimation = 1;  //set 1 for on /1

myIMU.settings.accelEnabled = 1;
myIMU.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
myIMU.settings.accelSampleRate = 833;  //Hz.  Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
myIMU.settings.accelBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
myIMU.settings.accelFifoEnabled = 1;  //Set to include accelerometer in the FIFO
myIMU.settings.accelFifoDecimation = 1;  //set 1 for on /1
myIMU.settings.tempEnabled = 1;

//Non-basic mode settings
myIMU.settings.commMode = 1;

//FIFO control settings
myIMU.settings.fifoThreshold = 100;  //Can be 0 to 4096 (16 bit bytes)
myIMU.settings.fifoSampleRate = 50;  //Hz.  Can be: 10, 25, 50, 100, 200, 400, 800, 1600, 3300, 6600
myIMU.settings.fifoModeWord = 6;  //FIFO mode.
//FIFO mode.  Can be:
//  0 (Bypass mode, FIFO off)
//  1 (Stop when full)
//  3 (Continuous during trigger)
//  4 (Bypass until trigger)
//  6 (Continous mode)
}

void loop()
{
  //Get all parameters
  Serial.print("\nAccelerometer:\n");
  Serial.print(" X = ");
  Serial.println(myIMU.readFloatAccelX(), 4);
  Serial.print(" Y = ");
  Serial.println(myIMU.readFloatAccelY(), 4);
  Serial.print(" Z = ");
  Serial.println(myIMU.readFloatAccelZ(), 4);

  Serial.print("\nGyroscope:\n");
  Serial.print(" X = ");
  Serial.println(myIMU.readFloatGyroX(), 4);
  Serial.print(" Y = ");
  Serial.println(myIMU.readFloatGyroY(), 4);
  Serial.print(" Z = ");
  Serial.println(myIMU.readFloatGyroZ(), 4);

  Serial.print("\nThermometer:\n");
  Serial.print(" Degrees C = ");
  Serial.println(myIMU.readTempC(), 4);
  Serial.print(" Degrees F = ");
  Serial.println(myIMU.readTempF(), 4);

  delay(1000);
}

我的处理脚本。

import processing.serial.*;
import java.io.*;
Serial myPort;
String val;
int Dummy = 1;

void setup() {


  size(500,500);
  //String portName = Serial.list()[2];

  myPort = new Serial(this , "COM3", 9600);

}
void draw()
{
  if (myPort.available() > 0)
  {val = myPort.readStringUntil('\n');}
      //while (myPort.available() > 0){
      try {

        FileWriter fw = new FileWriter("Output1.txt");
        PrintWriter pw = new PrintWriter(fw);
        pw.println(val);
        pw.close();

      //ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Output.bin"));
     // os.writeObject(val);
      //while (Dummy > 0) {os.close();}




        }
        catch (IOException e) {
          System.out.println("ERMAC");
       }

  println(val);
}

我是java新手,对I / O流媒体库不是很熟悉。任何建议都将非常感谢!

1 个答案:

答案 0 :(得分:0)

每次检测到新行时,您都会创建新的FileWriterPrintWriter。创建新的FileWriterPrintWriter 会创建一个名为的新空文件。这就是为什么你只看到文件中的一行。

相反,你应该在草图级声明你的作家,然后只在草图的开头创建一次。您可以在setup()函数中执行此操作,例如:

FileWriter fw;
PrintWriter pw;

void setup() {
  size(500,500);
  myPort = new Serial(this , "COM3", 9600);
  FileWriter fw = new FileWriter("Output1.txt");
  PrintWriter pw = new PrintWriter(fw);
}

然后,由于变量是在草图级别声明的,因此您可以像现在一样在draw()函数中使用它们,但不必每次都重新创建它们:

void draw(){
  if (myPort.available() > 0){
    val = myPort.readStringUntil('\n');}

     try {
        pw.println(val);
     }
     catch (IOException e) {
        System.out.println("ERMAC");
     }

     println(val);
  }
}

或者,您可以使用接受FileWriter值的boolean构造函数:

FileWriter fw = new FileWriter("Output1.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(val);
pw.close();

这会告诉FileWriter 追加数据,而不是创建新文件。 Java API

中的更多信息