Java SerialSocket

After a bit of use, we have realized that the RXTX library was a bit difficult to code with. So we decided to write a class to encapsulate the main features of this work into a neat set of methods.



The model under which this class was made, was the java.net.Socket class that makes network connections look so easy to use and interact with. Below are the methods used in this class.

SerialSocket(String name,int baud): this is the constructor, that opens a new connection to the serial port specified by the name argument ("COM3" or whatever), and the given baud rate. It does not have any serious error checking yet, but it is in the to-do list for later.

close(): closes the serial port.

getInputStream() / getOutputStream(): returns the respective stream to communicate with the port.

getBufferedReader() / getBufferedWriter(): returns the respective reader or writer since serial communication is character-based, almost every time you use it, you will need readers and writers.

addEventListener(SerialPortEventListener x): this method actually calls the SerialPort.addEventListener(SerialPortEventListener) method to add the SerialPortEventListener (an interface that requires you to implement a public synchronized void serialEvent(SerialPortEvent evt) method) and then calls the SerialPort.notifyOnDataAvailable(true) method. What it does is to register a function to be called when data arrives to the stream.


import java.io.*;
import java.util.*;
import gnu.io.*;

public class SerialSocket {

    private InputStream in;
    private OutputStream out;
    private BufferedReader reader;
    private BufferedWriter writer;
    private SerialPort port;
    public static final int TIMEOUT = 2000;


    public SerialSocket(String name, int baud){
        try{
            CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(name);
            port = (SerialPort) id.open(this.getClass().getName(),TIMEOUT);
            port.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            in = port.getInputStream();
            out = port.getOutputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            writer = new BufferedWriter(new OutputStreamWriter(out));
        }
        catch(NoSuchPortException e){
            e.printStackTrace();    //some error handling
        }catch(PortInUseException e){
            e.printStackTrace();    //some error handling
        }catch(IOException e){
            e.printStackTrace();    //some error handling
        }catch(UnsupportedCommOperationException e){
            e.printStackTrace();    //some error handling
        }
    }

    public InputStream getInputStream(){
        return in;
    }

    public OutputStream getOutputStream(){
        return out;
    }

    public BufferedReader getBufferedReader(){
        return reader;
    }

    public BufferedWriter getBufferedWriter(){
        return writer;
    }

    public void addEventListener (SerialPortEventListener listener){
        try{
            port.addEventListener(listener);
        }catch(TooManyListenersException e){
            e.printStackTrace();    //some error handling
        }
        port.notifyOnDataAvailable(true);
    }

    public void close(){
        port.close();
    }
}

Of course any comments, corrections or additions to the code above are more than welcome.
This project will be continued, with a graphical oscilloscope to plot the input from the serial connection.

No comments:

Post a Comment