serial
Class Serial

java.lang.Object
  |
  +--serial.Serial
Direct Known Subclasses:
CrossPointMux, IntelixMSeries, LynX10, PanasonicPTL592U, SerialModem, SerialProjector, ToshibaTLP670, TwoWayX10

public class Serial
extends Object
implements javax.comm.SerialPortEventListener

The Serial class is designed to control serial devices. This is the base class for all serial devices we use.


Field Summary
protected  int answerTimeout
           
protected  boolean cancelEcho
           
protected  String device
           
protected  javax.comm.SerialPortEventListener listener
           
protected  Semaphore semaphore
           
protected  javax.comm.SerialPort serial
           
protected  BufferedReader serial_in
           
protected  OutputStream serial_out
           
protected  int timeout_wait
           
protected  boolean waitingForResponse
           
 
Constructor Summary
Serial()
           
 
Method Summary
 boolean addEventListener(javax.comm.SerialPortEventListener lsnr)
          Allows you to add a listener that will listen on the stuff comming from the serial port
 void close()
          Closes the port.
static Serial getNewInstance()
          This method is here so that children of this class can override it so that main() calls appropriate constructor
static void main(String[] args)
          Used for standalone debugging and hardware tests.
 void open(String in_device)
           
 void open(String sInDevice, int iSpeed, int iDatabits, int iStopbits, int iParity)
          Opens a com port.
 void open(String sInDevice, int iSpeed, int iDatabits, int iStopbits, int iParity, int iFlowControl)
          Opens a com port.
 void openNoParams(String in_device)
          I cannot remember why I added this method but it should not be used - KZG
protected  boolean processSerialEvent(javax.comm.SerialPortEvent spe)
          This method can be overriden in classes that extend this one.
 int read()
          Reads a byte from the buffer (if nothing to read, returns EOF)
 byte[] readBytes()
          like readString but returns an array of bytes
 String readString()
          This method will read everything that is currently available in the buffer and return it as a string
 boolean ready()
          Tells you if there is any data waiting to be fetched from the buffer
 void send(byte[] cmd)
          Sends your message down the serial port
 void send(String cmd)
           
 String sendSynch(byte[] cmd, int timeout, boolean force)
          this method will send your command to the serial port and wait for a response from the serial port and pass this response back to you.
 String sendSynch(String cmd)
           
 String sendSynch(String cmd, int timeout, boolean force)
           
 void serialEvent(javax.comm.SerialPortEvent spe)
          This method is called from the serial port controller when there is a serial port event
 void setFlowControlMode(int f)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

device

protected String device

serial

protected javax.comm.SerialPort serial

serial_out

protected OutputStream serial_out

serial_in

protected BufferedReader serial_in

timeout_wait

protected int timeout_wait

listener

protected javax.comm.SerialPortEventListener listener

answerTimeout

protected int answerTimeout

waitingForResponse

protected boolean waitingForResponse

cancelEcho

protected boolean cancelEcho

semaphore

protected Semaphore semaphore
Constructor Detail

Serial

public Serial()
Method Detail

open

public void open(String in_device)
          throws javax.comm.NoSuchPortException,
                 javax.comm.PortInUseException,
                 IOException
Parameters:
in_device - the name of the serial device to speak to. Uses standard port settings. (e.g. "/dev/term/ttyrg" DONT FORGET ABOUT TERM)

open

public void open(String sInDevice,
                 int iSpeed,
                 int iDatabits,
                 int iStopbits,
                 int iParity)
          throws javax.comm.NoSuchPortException,
                 javax.comm.PortInUseException,
                 IOException
Opens a com port.
Parameters:
sInDevice - a String value
iSpeed - an int value
iDatabits - an int value
iStopbits - an int value
iParity - an int value
Throws:
javax.comm.NoSuchPortException - if an error occurs
javax.comm.PortInUseException - if an error occurs
IOException - if an error occurs

open

public void open(String sInDevice,
                 int iSpeed,
                 int iDatabits,
                 int iStopbits,
                 int iParity,
                 int iFlowControl)
          throws javax.comm.NoSuchPortException,
                 javax.comm.PortInUseException,
                 IOException
Opens a com port.
Parameters:
sInDevice - a String value
iSpeed - an int value
iDatabits - an int value
iStopbits - an int value
iParity - an int value
iFlowControl - an int value
Throws:
javax.comm.NoSuchPortException - if an error occurs
javax.comm.PortInUseException - if an error occurs
IOException - if an error occurs

openNoParams

public void openNoParams(String in_device)
                  throws javax.comm.NoSuchPortException,
                         javax.comm.PortInUseException,
                         IOException
I cannot remember why I added this method but it should not be used - KZG

setFlowControlMode

public void setFlowControlMode(int f)

send

public void send(byte[] cmd)
          throws IOException
Sends your message down the serial port
Parameters:
cmd[] - bytes you want to send down the line

send

public void send(String cmd)
          throws IOException

sendSynch

public String sendSynch(byte[] cmd,
                        int timeout,
                        boolean force)
                 throws IOException
this method will send your command to the serial port and wait for a response from the serial port and pass this response back to you.
Parameters:
cmd - the command
timeout - the timeout (in milliseconds -- how long to wait if there is no response)
force - if force is true, this method will always wait for full length of timeout and return everything that was said during that time; otherwise, this method may return earlier, the moment it gets its first response from the serial port
Returns:
String rep of whatever came back through the serial port or empty string (it never returns null)

sendSynch

public String sendSynch(String cmd,
                        int timeout,
                        boolean force)
                 throws IOException

sendSynch

public String sendSynch(String cmd)
                 throws IOException

close

public void close()
           throws IOException
Closes the port.

ready

public boolean ready()
Tells you if there is any data waiting to be fetched from the buffer

read

public int read()
Reads a byte from the buffer (if nothing to read, returns EOF)

readString

public String readString()
This method will read everything that is currently available in the buffer and return it as a string

readBytes

public byte[] readBytes()
like readString but returns an array of bytes

addEventListener

public boolean addEventListener(javax.comm.SerialPortEventListener lsnr)
Allows you to add a listener that will listen on the stuff comming from the serial port
Returns:
true if ok, false if there already is a listener attached

serialEvent

public void serialEvent(javax.comm.SerialPortEvent spe)
This method is called from the serial port controller when there is a serial port event
Specified by:
serialEvent in interface javax.comm.SerialPortEventListener
Parameters:
spe - a SerialPortEvent value

processSerialEvent

protected boolean processSerialEvent(javax.comm.SerialPortEvent spe)
This method can be overriden in classes that extend this one. If the extended method reads stuff from the buffer, it should return true. Otherwise (i.e. when it does not read anything from the buffer), it should return false;

getNewInstance

public static Serial getNewInstance()
This method is here so that children of this class can override it so that main() calls appropriate constructor

main

public static void main(String[] args)
Used for standalone debugging and hardware tests.