在处理中保存串行数据

时间:2018-09-12 17:18:59

标签: java serial-port processing

第二次问好,

最初,我一直在寻找一个广泛的答案,但是这个线程因“过于广泛”而被封锁了……所以我别无选择,只能详细介绍。如果再次提出问题,我深表歉意,这违反了论坛指南,我是stackoverflow的新手,所以请保持友善。

我有250Hz的数据进入串行端口,我想将所有数据保存到.csv文件中。当然draw()不能跟上数据的速度...

此刻,我正在使用serialEvent(port)收集和解析数据。解析后,我在draw中调用一个函数,将数据添加到表的新行中,然后每5秒保存该表...

是的,我看到了一个明显的缺陷,即如果我将当前数据保存在绘图中,那么当然它不能保存所有输入的数据,而只能保存在输入时出现的数据。数据保存功能称为...,但我不确定解决该问题的最佳方法。缓冲方案?还是我可以有一个单独的线程将所有进入表的数据都添加到表中?

这导致了我最初的(广泛的)问题... 有没有一种方法可以将所有传入数据保存到文件中而无需轮询?

感谢所有。.下面的代码: 吐温

import processing.serial.*;
import static javax.swing.JOptionPane.*;

Table table;
String Path = "PathProvidedHere.csv";
String message;

//Some time keeping variables
int hours, minutes, seconds, milliseconds;
float SaveTime;

//Serial port selection
Serial myPort;
String COMx, COMlist = "";
final boolean debug = true;
String portName;

// Data variables
float yaw = 0.0; float pitch = 0.0; float roll = 0.0;
float A1, A2, A3, A4;
float E1, E2, E3, E4;

void setup()
{
    //Set up GIU box
    size(1024, 768, P3D);
    frameRate(250);  
    smooth();

    //Some other setups  like fonts, graphs, etc.

    //Set up the logging table
    table = new Table();  
    table.addColumn("A1"); table.addColumn("A2"); table.addColumn("A3"); table.addColumn("A4");
    table.addColumn(""); table.addColumn("E1"); table.addColumn("E3"); table.addColumn("E4");  
    table.addColumn(" "); table.addColumn("min"); table.addColumn("sec"); table.addColumn("milli");

    portName = chooseCOM();

    delay(1000); 
}


void draw()
{  
    SavetoCSV();

    //serialEvent(myPort);  // read and parse incoming serial message

    ACouple();
    Unrelated();
    FunctionsHere();

    if(millis() - SaveTime > 5000)
    {
        saveTable(table, Path);
        SaveTime=millis();
    }
}


String chooseCOM()
{
    setupP2 = true;

    try 
    {
        if (debug) printArray(Serial.list());

        int i = Serial.list().length;

        if (i != 0) 
        {
            if (i >= 2) 
            {
                // need to check which port the inst uses -
                // for now we'll just let the user decide
                for (int j = 0; j < i; ) 
                {
                    COMlist += char(j+'a') + " = " + Serial.list()[j];
                    if (++j < i) COMlist += ",  ";
                }

                COMx = showInputDialog("Which COM port is correct? (a,b,..):\n"+COMlist);

                if (COMx == null) exit();

                if (COMx.isEmpty()) exit();

                i = int(COMx.toLowerCase().charAt(0) - 'a') + 1;
            }

            String portName = Serial.list()[i-1];

            if (debug) //println(portName + " Selected");

            myPort = new Serial(this, portName, 115200); // change baud rate to your liking
            myPort.bufferUntil(13); // buffer until CR/LF appears, but not required..

            return portName;
        } 
        else 
        {
            showMessageDialog(frame, "Device is not connected to the PC");
            exit();
        }
    }
    catch (Exception e)
    { //Print the type of error
        showMessageDialog(frame, "COM port is not available (may\nbe in use by another program)");
        //println("Error:", e);
        exit();
    }

    return "noPort";
}


void serialEvent(Serial myPort)
{
    int newLine = 13; // new line character in ASCII

    do
    {
        message = myPort.readStringUntil(newLine); // read from port until new line

        if (message != null) 
        {
            String[] list = split(trim(message), " ");

            if (list.length == 4 && list[0].equals("i")) 
            {
                yaw =  float(list[1]); // convert to float yaw
                pitch = float(list[2]); // convert to float pitch
                roll = float(list[3]); // convert to float roll
            } 
            else if (list.length == 5 && list[0].equals("s")) 
            {  
                A1 = float(list[1]);
                A2 = float(list[2]);
                A3 = float(list[3]);
                A4 = float(list[4]);
            } 
            else if (list.length >=2 && list[0].equals("b")) 
            {  
                Battery = int(list[1]);
            } 
            else if (list.length >= 2 && list[0].equals("m")) 
            {  
                MACid = int(list[1]);
            } 
            else 
            {
                //print anything extra to console
                //println(message);
            }
        }
    } while (message != null);
}

void SavetoCSV() 
{
    if (A1 != 0) 
    {
        TableRow newRow = table.addRow();
        newRow.setFloat("A1", (A1));
        newRow.setFloat("A2", (A2));
        newRow.setFloat("A3", (A3));
        newRow.setFloat("A4", (A4));

        //saveTable(table, Path);
    }
}

其他信息:  -处理P3  -作为记录,使用我的脚本的其余部分,我最多可以绘制80hz  -保存所有数据并稍后解析

1 个答案:

答案 0 :(得分:0)

走了缓冲路线。...我想我现在要靠近了。不确定我是按正确的顺序保存数据还是保存过程会停止其余过程...

代码:

import processing.serial.*;
import static javax.swing.JOptionPane.*;

//Arrays to save the data
LinkedList<Integer> A1c = new LinkedList<Integer>();
LinkedList<Integer> A2c = new LinkedList<Integer>();
LinkedList<Integer> A3c = new LinkedList<Integer>();
LinkedList<Integer> A4c = new LinkedList<Integer>();
int bufferLength = 500;
int bufflen = 0;

//Serial port selection
Serial myPort;
String COMx, COMlist = "";
final boolean debug = true;
String portName;

// Data variables
float yaw = 0.0; float pitch = 0.0; float roll = 0.0;
float A1, A2, A3, A4;

//Data log variables
Table table;
String Path = "PathtoFile.csv"; 


void setup() {
  //Set up GIU box
  size(1024, 768, P3D);
  frameRate(250);  
  strokeWeight(50);
  smooth();

  //Set up the logging table
  table = new Table();  
  table.addColumn("A1"); table.addColumn("A2"); table.addColumn("A3"); table.addColumn("A4");

      portName = chooseCOM();
}


void draw() {

    //SavetoCSV now called within SerialEvent()
    //SavetoCSV();
    //serialEvent(myPort);  // read and parse incoming serial message

  Some();
  Unrelated();
  FunctionsHere();
}


void serialEvent(Serial myPort) {
  int newLine = 13; // new line character in ASCII
  do {
    message = myPort.readStringUntil(newLine); // read from port until new line
    if (message != null) {
      String[] list = split(trim(message), " ");
      if (list.length == 4 && list[0].equals("i")) {
        yaw =  float(list[1]); // convert to float yaw
        pitch = float(list[2]); // convert to float pitch
        roll = float(list[3]); // convert to float roll
      } else if (list.length == 5 && list[0].equals("s")) {  
        A1 = float(list[1]);
        A2 = float(list[2]);
        A3 = float(list[3]);
        A4 = float(list[4]);

        if (bufflen < bufferLength) {
          A1c.push(int(A1));
          A2c.push(int(A2));
          A3c.push(int(A3));
          A4c.push(int(A4));

          bufflen++;
        }
        else{
          bufflen = 0;
          SavetoCSV();
        }
      } else if (list.length >=2 && list[0].equals("b")) {  
        Battery = int(list[1]);
      } else if (list.length >= 2 && list[0].equals("m")) {  
        MACid = int(list[1]);
      } else {
        //print anything extra to console
        //println(message);
      }
    }
  } while (message != null);
}

void SavetoCSV() {

  if (A1 != 0) {
    for (int i = bufferLength - 1; i >= 0; i--){
     if (i < bufferLength){
       TableRow newRow = table.addRow();
       newRow.setFloat("A1", (A1c.get(i)));
       newRow.setFloat("A2", (A2c.get(i)));
       newRow.setFloat("A3", (A3c.get(i)));
       newRow.setFloat("A4", (A4c.get(i)));
     } else saveTable(table, Path);
    }
  }
}


String chooseCOM() {
  setupP2 = true;
  try {
    if (debug) printArray(Serial.list());
    int i = Serial.list().length;
    if (i != 0) {
      if (i >= 2) {
        // need to check which port the inst uses -
        // for now we'll just let the user decide
        for (int j = 0; j < i; ) {
          COMlist += char(j+'a') + " = " + Serial.list()[j];
          if (++j < i) COMlist += ",  ";
        }
        COMx = showInputDialog("Which COM port is correct? (a,b,..):\n"+COMlist);
        if (COMx == null) exit();
        if (COMx.isEmpty()) exit();
        i = int(COMx.toLowerCase().charAt(0) - 'a') + 1;
      }
      String portName = Serial.list()[i-1];
      if (debug) //println(portName + " Selected");
      myPort = new Serial(this, portName, 115200); // change baud rate to your liking
      myPort.bufferUntil(13); // buffer until CR/LF appears, but not required..
      return portName;
    } else {
      showMessageDialog(frame, "Device is not connected to the PC");
      exit();
    }
  }
  catch (Exception e)
  { //Print the type of error
    showMessageDialog(frame, "COM port is not available (may\nbe in use by another program)");
    //println("Error:", e);
    exit();
  }
  return "noPort";
}