2008-07-09 23:18:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2010-10-07 21:13:00 +00:00
|
|
|
* $Id: SerialConnection.java,v 1.4 2010/10/07 21:13:00 nifi Exp $
|
2008-07-09 23:18:05 +00:00
|
|
|
*
|
|
|
|
* -----------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* SerialConnection
|
|
|
|
*
|
|
|
|
* Authors : Joakim Eriksson, Niclas Finne
|
|
|
|
* Created : 5 jul 2008
|
2010-10-07 21:13:00 +00:00
|
|
|
* Updated : $Date: 2010/10/07 21:13:00 $
|
|
|
|
* $Revision: 1.4 $
|
2008-07-09 23:18:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package se.sics.contiki.collect;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public abstract class SerialConnection {
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected final SerialConnectionListener listener;
|
2008-07-09 23:18:05 +00:00
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected boolean isSerialOutputSupported = true;
|
|
|
|
|
|
|
|
protected String comPort;
|
2008-07-10 14:52:59 +00:00
|
|
|
protected boolean isOpen;
|
|
|
|
protected boolean isClosed = true;
|
|
|
|
protected String lastError;
|
2008-07-09 23:18:05 +00:00
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected PrintWriter serialOutput;
|
|
|
|
|
|
|
|
protected SerialConnection(SerialConnectionListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMultiplePortsSupported() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSerialOutputSupported(boolean isSerialOutputSupported) {
|
|
|
|
this.isSerialOutputSupported = isSerialOutputSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSerialOutputSupported() {
|
|
|
|
return isSerialOutputSupported;
|
|
|
|
}
|
|
|
|
|
2008-07-09 23:18:05 +00:00
|
|
|
public boolean isOpen() {
|
|
|
|
return isOpen;
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
public boolean isClosed() {
|
|
|
|
return isClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract String getConnectionName();
|
|
|
|
|
2008-07-09 23:18:05 +00:00
|
|
|
public String getComPort() {
|
|
|
|
return comPort;
|
|
|
|
}
|
|
|
|
|
2008-07-10 14:52:59 +00:00
|
|
|
public void setComPort(String comPort) {
|
|
|
|
this.comPort = comPort;
|
|
|
|
}
|
|
|
|
|
2008-07-09 23:18:05 +00:00
|
|
|
public String getLastError() {
|
|
|
|
return lastError;
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected PrintWriter getSerialOutput() {
|
|
|
|
return serialOutput;
|
2008-07-09 23:18:05 +00:00
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected void setSerialOutput(PrintWriter serialOutput) {
|
|
|
|
this.serialOutput = serialOutput;
|
2008-07-09 23:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void writeSerialData(String data) {
|
|
|
|
PrintWriter serialOutput = this.serialOutput;
|
|
|
|
if (serialOutput != null) {
|
|
|
|
serialOutput.println(data);
|
|
|
|
serialOutput.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
public abstract void open(String comPort);
|
|
|
|
|
|
|
|
public final void close() {
|
2008-07-10 14:52:59 +00:00
|
|
|
isClosed = true;
|
2008-07-09 23:18:05 +00:00
|
|
|
lastError = null;
|
|
|
|
closeConnection();
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected final void closeConnection() {
|
2008-07-10 14:52:59 +00:00
|
|
|
isOpen = false;
|
2008-07-09 23:18:05 +00:00
|
|
|
if (serialOutput != null) {
|
|
|
|
serialOutput.close();
|
|
|
|
serialOutput = null;
|
|
|
|
}
|
2010-10-07 21:13:00 +00:00
|
|
|
doClose();
|
2008-07-10 14:52:59 +00:00
|
|
|
serialClosed();
|
2008-07-09 23:18:05 +00:00
|
|
|
}
|
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected abstract void doClose();
|
|
|
|
|
|
|
|
protected final void serialData(String line) {
|
|
|
|
listener.serialData(this, line);
|
|
|
|
}
|
2008-07-09 23:18:05 +00:00
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected final void serialOpened() {
|
|
|
|
listener.serialOpened(this);
|
|
|
|
}
|
2008-07-09 23:18:05 +00:00
|
|
|
|
2010-10-07 21:13:00 +00:00
|
|
|
protected final void serialClosed() {
|
|
|
|
listener.serialClosed(this);
|
|
|
|
}
|
2008-07-09 23:18:05 +00:00
|
|
|
|
|
|
|
}
|