【问题标题】:Porting Arduino Comm Data into .txt将 Arduino 通信数据移植到 .txt
【发布时间】:2015-11-29 08:33:11
【问题描述】:

我正在尝试使用处理脚本将信号数据转储到 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 流库不是很熟悉。任何建议将不胜感激!

【问题讨论】:

    标签: java io arduino processing


    【解决方案1】:

    每次检测到新行时,您都会创建一个新的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);
      }
    }
    

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

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

    这告诉FileWriter 追加数据而不是创建新文件。更多信息在Java API

    【讨论】: