-1

I'm facing a problem regarding of using the data from rf transmision on a class that I wish to use to store on the database. Code 1 is the code for the RF transmission. Is there anyway I can use the data in the highlight of data to be used in code 1?

public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {

            String inputLine=input.readLine();
            String[] parts = inputLine.split(",");
            String part1 = parts[0]; 
            String part2 = parts[1];
            System.out.print(part1); // data to be used 
            System.out.print(" , ");
            System.out.println(part2); //data to be used
            //System.out.println(data);

code 1

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;


public class test implements SerialPortEventListener {
SerialPort serialPort;
    /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { 
        "COM4", // Windows

};

/**
* A BufferedReader which will be fed by a InputStreamReader 
* converting the bytes into characters 
* making the displayed results codepage independent
*/

private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

public void initialize() {

    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier)     portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {

            String inputLine=input.readLine();
            String[] parts = inputLine.split(",");
            String part1 = parts[0]; 
            String part2 = parts[1];
            System.out.print(part1); // data to be used 
            System.out.print(" , ");
            System.out.println(part2); //data to be used
            //System.out.println(data);

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}

public static void main(String[] args) throws Exception {
    test main = new test();
    main.initialize();
    Thread t=new Thread() {
        public void run() {
            //the following line will keep this app alive for 1000 seconds,
            //waiting for events to occur and responding to them (printing incoming messages to console).
            try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
        }
    };
    t.start();
    System.out.println("Started");

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
calvin ern
  • 783
  • 1
  • 6
  • 12
  • I don't understand the question. Of course there are ways to use any data. How did you try to use it? What problem did you have? – WillShackleford Aug 22 '15 at 10:07
  • @WillShackleford i wan to use it like for example to store on SQLite. So the problem is I don't know how to like store the received data in a variable and used it from another class or form in java. Thanks for helping – calvin ern Aug 22 '15 at 12:43
  • What example to store on SQLite are you referring to? – WillShackleford Aug 22 '15 at 15:12
  • @WillShackleford like database , a table like – calvin ern Aug 22 '15 at 15:18
  • @WillShackleford the data i seperated from xxx,xxx, form part1 and part 2 which is a string. i wish to used as a data to store in the database i've created. – calvin ern Aug 22 '15 at 15:20

1 Answers1

0

I am probably still confused as to what your asking. I think the simplest way for another class to use the variables would be for it to be passed the values in arguments to one of its functions.

eg.

class ClassThatUsesPart1Part2 {

    public void usePart1Part2(String part1, String part2) {
        // put code that uses part1 and part2 here
    }
}

Add another variable to your class.

private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

/** Variable of class that will use part1, part2.*/
private ClassThatUsesPart1Part2 part12User = new ClassThatUsesPart1Part2();

Then use it within your function.

        String inputLine=input.readLine();
        String[] parts = inputLine.split(",");
        String part1 = parts[0]; 
        String part2 = parts[1];
        part12User.usePart1Part2(part1,part2);

Is this what you were looking for or have I just completely misunderstood?

WillShackleford
  • 6,918
  • 2
  • 17
  • 33