PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How to manage several Serial messages in processing

 July 11, 2022     arduino, io, messages, processing     No comments   

Issue

I am reading the UID of my RFID card and storing it in a variable called myUID.

After that I am authorizing to the card with the factory key and read block number 4 (which has been written to earlier) and store it in a string readBlock.

On the Arduino, I print out the variables onto the serial interface like so.

Serial.println(myUID);
Serial.println(readBlock);

On the client side, I use a Java program that reads in serial data. My program uses the Processing Library.

Serial mySerial;
PrintWriter output;

void setup() {
 output = createWriter( "data.txt" );
 mySerial = new Serial( this, Serial.list()[0], 9600 );
 mySerial.bufferUntil('\n');
}

void draw(){
  while (mySerial.available() > 0) {
  String inBuffer = mySerial.readString();   
  if (inBuffer != null)
    output.println(inBuffer); 
  }
}
void keyPressed() { // Press a key to save the data
  output.flush(); // Write the remaining data
  output.close(); // Finish the file
  exit(); // Stop the program
}

Now my data.txt is expected to look like

xxx xxx xxx xxx (uid of card)
00 00 00 00 00 00 00 00 ... (read block from card)

but looks like

237 63 58 1
07
37 37 95
 37 
97 98 50 54 37 5
4 55 102 55 52 
45 98

I have tried several things like readStringUntil('\n'); in the Processing Library but without success.


Solution

For everyone interested, I have fixed the problem myself with many hours of searching Google, so maybe this will help someone in the future:

I could fix it with this code:

import processing.serial.*;
int count = 0;
String input = "";
String fileName = dataPath("SET FILEPATH HERE");

Serial mySerial;

import java.io.*;

void setup() {
  mySerial = new Serial(this, Serial.list()[0], 9600);
  mySerial.bufferUntil('\n');

  File f = new File(fileName);
  if (f.exists()) {
    f.delete();
  }
}

void draw(){}

// listen to serial events happening
void serialEvent(Serial mySerial){
  input = mySerial.readStringUntil('\n'); 
  write(input, count);
  count++;
}

// function for writing the data to the file
void write(String inputString, int counter) {
  // should new data be appended or replace any old text in the file?  
  boolean append = false;

  // just for my purpose, because I have got two lines of serial which need to get written to the file 
  //(Line 1: UID of card, Line 2: Read block of card)  
  if(counter < 2){
    append = true;  
  }
  else{
    count = 0;
  }


  try {
    File file = new File("D:/xampp/htdocs/pizza/src/rfid/data.txt");

    if (!file.exists()) {
      file.createNewFile();
    }

    FileWriter fw = new FileWriter(file, append);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

    pw.write(inputString + '\n');
    pw.close();
  }
  catch(IOException ioe) {
    System.out.println("Exception ");
    ioe.printStackTrace();
  }
}


Answered By - TheEarthkin
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing