Package jturbojet.serial

Source Code of jturbojet.serial.SerialDAO

package jturbojet.serial;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import jssc.SerialPort;
import jssc.SerialPortException;
import jturbojet.serial.interfaces.SerialUpdate;

public class SerialDAO implements Runnable {

    private final SerialPort serialPort;
    private final String portName;
    private final SerialUpdate update;
    private Thread thread;
    private boolean running;
    private String data;

    /**
     * Set up serial port connection. To start reading the port call
     * <code>startReading</code> function
     *
     * For <code>portName</code> parameter use <code>SerialPorts</code> class to
     * available ports from the system and give one of those ports as parameter
     * to here. You can also specifie it yourself, like "COM4" on windows.
     *
     * For <code>baudrate, databits, stopbits</code> and <code>parity</code>
     * parameters use <code>SerialPort</code> class static constats.
     *
     * <code>update</code> parameter need a <code>SerialUpdate</code> interface
     * implemented class. This class is used to call functions when serial
     * connections is readed.
     *
     * @param portName portname to read.
     * @param baudrate connection baudrate.
     * @param databits dataframe bit size.
     * @param stopbits number of stop bits.
     * @param parity parity used
     * @param update interface methods for serial read updates
     */
    public SerialDAO(String portName, int baudrate, int databits, int stopbits, int parity, SerialUpdate update) {
        this.update = update;
        this.portName = portName;

        serialPort = new SerialPort(portName);
        try {
            if (!serialPort.isOpened()) {
                serialPort.openPort();
                serialPort.setParams(baudrate, databits, stopbits, parity);
                int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;
                serialPort.setEventsMask(mask);
            }
        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    public String getPortName() {
        return portName;
    }

    public void startReading() {
        try {
            if (this.serialPort.isOpened()) {
                this.serialPort.purgePort(SerialPort.PURGE_RXCLEAR);
                this.running = true;
                this.thread = new Thread(this);
                this.thread.start();
            }
        } catch (SerialPortException ex) {
            Logger.getLogger(SerialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void stopReading() {
        this.running = false;
        this.thread = null;
        try {
            if (this.serialPort.isOpened()) {
                serialPort.closePort();
            }
        } catch (SerialPortException ex) {
            Logger.getLogger(SerialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        while (this.running) {
            try {
                if (serialPort != null) {
                    String c;
                    data = "";
                    do {
                        c = serialPort.readString(1);
                        data += c;
                    } while (!c.equals("\n"));
                    System.out.print(data);
                    this.update.readLinePerformed(data);
                }
            } catch (SerialPortException ex) {
                update.infoUpdatePerformed("Port " + this.serialPort.getPortName() + " closed");
                this.running = false;
            }
        }
        try {
            if (this.serialPort.isOpened()) {
                serialPort.closePort();
            }
        } catch (SerialPortException ex) {
            update.infoUpdatePerformed("Port not opened, try connecting again");
        }
    }
}
TOP

Related Classes of jturbojet.serial.SerialDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.