How can I use processing.app.Preferences.getInteger() methode in java? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I use processing.app.Preferences.getInteger() methode in java? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I posted a question yesterday(How can I use processing.app.Preferences in java?). I change my code to this…

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        ArrayList<String> portList = new ArrayList();
        Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
        while(portIdentifiers.hasMoreElements()){
            CommPortIdentifier portt = (CommPortIdentifier)portIdentifiers.nextElement();
            portList.add(portt.getName());
        }
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portList.get(0));
        SerialPort port = (SerialPort)portId.open("Main", 9600);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
        while(true){
            while(input.available()>0) {
                System.out.print((char)(input.read()));
            }
        }
    }
}

and this code is working and it is good. The portId.open() method give tow argument, first name of process which use the port and second is rate of data sending. But now my question is how can I write some code and change my hard code to soft code. “Main” and 9600 is constant.How can I get this info from jvm or System or …I don’t know… When I use Preferences.getInteger("serial.debug_rate") I get this error

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at processing.app.Preferences.getInteger(Preferences.java:885)
    at pkg.Main.main(Main.java:36)
Java Result: 1

and this is the Previous code from this link.

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import processing.app.Preferences;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        Preferences.init(null);
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
                Preferences.get("serial.port"));

        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        while(true){
            while(input.available()>0) {
                System.out.print((char)(input.read()));
            }
        }
    }
}

Answer

If you’re simply trying to read from a a serial port, is reading the port name and baud rate worth the trouble of using Arduino’s prefernce file ? You will know the port name and baud rate because you chose them manually when uploading the Arduino sketch to your board.

In my opinion it seems simpler to use Processing’s Serial library which uses gnu.io.SerialPort; behind the scenes anyway:

import processing.serial.*;

Serial port;

void setup(){
  print("Serial ports: ");println(Serial.list());
  try{
    port = new Serial(this,Serial.list()[0],9600);//try to open 1st port
  }catch(Exception e){
    System.err.println("*****Error Opening Serial Port!*****");
    e.printStackTrace();
  }
}
void draw(){/*you can draw the string to screen here if you like*/}
void serialEvent(Serial p) { 
  println(p.readString()); 
}

You can also do nice things like bufferUntil() so you won’t have to manually wait for a character to finish appending chars to a String.

The library resides in Processing’s install folder under modesjavalibrariesserial so you can use it in a pure Java project adding the serial and Processing’s core.jar libraries to the project and extending PApplet. If for some reason you don’t want to use the Processing library, you can learn from it.

Also, regarding your original query, it seems you are not loading the preferences file (which lives in ARDUINO_INSTALL_FOLDER/lib/preferences.txt) which is why you may be getting null references when retrieving values. At this point you are also free to use Java’s preference classes or simply load the text file and retrieve what’s between serial.port= and a new line character or serial.debug_rate and a new line character.

Update Here is the code above using Arduino’s preferences file read a simple strings:

import processing.serial.*;

Serial port;
final String PORT_TOKEN = "serial.port=";
final String BAUD_TOKEN = "serial.debug_rate=";

void setup(){
  print("Serial ports: ");println(Serial.list());
  //begin parsing arduino prefs
  String[] arduinoPrefs = loadStrings("C:\Program Files\Arduino\lib\preferences.txt");
  String portName = "";
  int baudRate = 0;
  for(String s : arduinoPrefs){//for each line in text file
    if(s.contains(PORT_TOKEN)) portName = s.substring(s.indexOf(PORT_TOKEN)+PORT_TOKEN.length());//find the PORT_TOKEN and if there is one, retrieve the value
    if(s.contains(BAUD_TOKEN)) baudRate = int(s.substring(s.indexOf(BAUD_TOKEN)+BAUD_TOKEN.length()));//do the same for the BAUD_TOKEN
  }
  //end parsing arduino prefs
  try{
    if(portName.length() > 0 && baudRate > 0) 
      port = new Serial(this,portName,baudRate);
    else
      port = new Serial(this,Serial.list()[0],9600);//try to open 1st port
  }catch(Exception e){
    System.err.println("*****Error Opening Serial Port!*****");
    e.printStackTrace();
  }
}
void draw(){/*you can draw the string to screen here if you like*/}
void serialEvent(Serial p) { 
  println(p.readString()); 
}

Remember to update the path to preferences.txt in the loadString() call based on your OS and Arduino install folder.

We are here to answer your question about How can I use processing.app.Preferences.getInteger() methode in java? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji