All pastes #766764 Raw Edit

Benny!

public java v1 · immutable
·published 2007-11-09 03:39 UTC
rendered paste body
/* * cardReaderListener.java * * Created on 16. mars 2007, 13:59 * */package no.abakus.bedcard.logic.cardreader;import java.io.InputStreamReader;import java.io.OutputStream;import java.util.Enumeration;import javax.comm.CommPortIdentifier;import javax.comm.SerialPort;import javax.comm.SerialPortEvent;import javax.comm.SerialPortEventListener;/** *  *  @author Stian Sønderland */public abstract class CardReader implements SerialPortEventListener {        // global variables:    cardReaderListener l;    SerialPort sPort;    OutputStream out;    InputStreamReader in;    CommPortIdentifier cpID;        // status variables    boolean isRunning = false;    boolean isReady = true;    boolean debug = false;    boolean connected = false;        // abstract    String portname;    String readerName = "Not set";    String getNameChar = "N";                    /** Constructor */        public CardReader(cardReaderListener listener) {        l = listener;    }                        /** Public methods  */        public void start() {       autoDetectPort();    }    public void stop() {    	if (!connected) return;    	if (sPort != null) {    	    sPort.close();    	}    	connected = false;    	l.notifyReaderStatusChange(this);    }        public boolean isConnected() {        return connected;    }        public String getName() {        return readerName;    }        public void setCardReaderListener(cardReaderListener listener) {    	l = listener;    }                    /** Defined by SerialPortEventListener  */        public void serialEvent(SerialPortEvent event) {        String c = "";        byte[] b = new byte[1];        try {            isReady = false;            while (in.ready()) {                b[0] = (byte)in.read();                if (debug) System.out.println("Byte: "+ b[0]);                c = c + new String(b, "ASCII");                Thread.sleep(5);            }        } catch (Exception e) {            e.printStackTrace();        }        isReady = true;        process(c);    }                            /** Other methods  */        protected boolean sendMessage(String s) {        if (isReady) {            byte[] b = s.getBytes();            try {                out.write(b);                if (debug) System.out.println("Bytes written: " + s);            } catch (Exception e) {                e.printStackTrace();                return false;            }            return true;        } else {            System.out.println("Not ready...");            try {                Thread.sleep(200);                sendMessage(s);            } catch (Exception e) {                e.printStackTrace();                return false;            }        }        return true;    }        protected boolean setPort(CommPortIdentifier portId) {        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {            try {               sPort = (SerialPort) portId.open(portId.getName(), 9600);            sPort.addEventListener(this);            out = sPort.getOutputStream();            in = new InputStreamReader(sPort.getInputStream(), "ASCII");            sPort.notifyOnDataAvailable(true);            if (debug) System.out.println("Port set..");            } catch (Exception e) {                e.printStackTrace();                return false;            }        }        cpID = portId;        return true;    }        @SuppressWarnings("unchecked")    private boolean autoDetectPort() {    	if (debug) System.out.println("Autodetecting Ports for " + readerName);        try {            Enumeration<CommPortIdentifier> e = CommPortIdentifier.getPortIdentifiers();            while (e.hasMoreElements() && !connected) {                CommPortIdentifier cpi = ((CommPortIdentifier)e.nextElement());                if (cpi.getName().substring(0, 3).equals("COM")) {                    if (debug) System.out.println("COM port detected: " + cpi.getName());                    if (!cpi.isCurrentlyOwned()) {                        if (setPort(cpi)) {                            portname = cpi.getName();                                                 if (debug) System.out.println("Trying " + portname + "...");                            sendMessage(getNameChar);                            Thread.sleep(200);                            if (!connected) {                                sPort.close();                            }                        }                     }                }            }        } catch (Exception e) {            e.printStackTrace();            return false;        }        if (connected) {            l.notifyReaderStatusChange(this);            return true;        } else {            return false;        }                    }         abstract protected void process (String message);    }