Problem with save data from serial port

edited February 2016 in Arduino

Hi guys I'm new with Processing development and I having some problems. here's the issue: I'm using an Arduino to measuere temperature and just wanna to send it to Processing by serial communication and store it in to a .txt file. I have already searched a solution in Processing tutorials and in this forum, but didn't find yet. Any help is welcome!! Here is my code (the comments are in Portuguese):

//Ler os dados da porta serial e salvar num arquivo .txt

import processing.serial.*;
import cc.arduino.*;

Serial portaCOM5; //Cria um objeto da classe Serial
String dados, COM5; //Dados em ASCII recebidos do Arduino
int val ;// Dados recebidos pela porta serial

//Banco de dados
PrintWriter arquivo; 

void setup ()
{
  size (200, 200);
  frameRate(10);   
  
     
   println(Serial.list ()); //Lista no Prompt de Comando todas as portas seriais disponíveis
   
   COM5 = Serial.list()[0]; //Recebe a porta USB escolhida
   portaCOM5= new Serial(this, "COM5", 9600); //taxa de transfarencia 9600bps
   portaCOM5.bufferUntil('\n');
      
        //Banco de dados:
   arquivo = createWriter("temperaturas"+"_"+month()+"-"+day()+"-"+year()+"_"+hour()+"-"+minute()+"-"+
            second()+".txt"); //cria um arquivo para armazenar as temperaturas
    
    arquivo.print("Monitoramento de Temperatura DHT11");
    arquivo.println();    
  
}

void draw()
{
  background (255); //Ajusta cor de fundo como branca
  
  }
   
void serialEvent(Serial portaCOM5)
{
  
     COM5= Serial.list()[0]; //Recebe a porta USB escolhida
       
      
  while (portaCOM5.available()>0)
  {
     val = portaCOM5.readString();
     arquivo.println();
  }
         
  arquivo.flush(); // Writes the remaining data to the file
  arquivo.close(); // Finishes the file
         
}

PS: When I check the monitor serial from Arduino, the data are there, so I believe anything here is wrong.

Thanks!!!!

Answers

  • edited February 2016 Answer ✓

    Hey Pedro! I don't have Arduino nor any valid serial port here. So I can't be of much help.
    Just did this very simple sample below. The idea is just to get 10 line readings.
    After that, issue saveStrings() and end the sketch. Check it out whether it works for ya: :-\"

    https://Processing.org/reference/saveStrings_.html
    https://Processing.org/reference/trim_.html
    https://Processing.org/reference/libraries/serial/index.html

    // forum.Processing.org/two/discussion/14931/
    // problem-with-save-data-from-serial-port
    
    // GoToLoop (2016-Feb-16)
    
    import processing.serial.Serial;
    
    static final String FILENAME = "temperatures.txt";
    static final int READS = 10, PORT = 0, BAUDS = 9600;
    
    final String[] reads = new String[READS];
    int idx;
    
    void setup() {
      noLoop();
    
      final String[] ports = Serial.list();
      println(ports.length, ENTER);
      printArray(ports);
    
      new Serial(this, ports[PORT], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      if (idx == READS) {
        saveStrings(dataFile(FILENAME), reads);
        exit();
      }
    }
    
    void serialEvent(final Serial s) {
      final String reading = s.readString().trim();
      reads[idx++] = reading;
    
      redraw();
      println(reading);
    }
    
Sign In or Register to comment.