edu.berkeley.guir.ptk.util
Class SerialReadWrite

java.lang.Object
  extended byedu.berkeley.guir.ptk.PTK
      extended byedu.berkeley.guir.ptk.util.SerialReadWrite

public class SerialReadWrite
extends PTK

Enables reading from and writing to the serial port. You can read/write Strings or byte[] ("packets"). The default port is COM1 and the default packet size is 25 The packet size is applicable only when calling readPacket(). As an alternative, readLine() will read all bytes up to a new line. You can set these data fields by passing new values into a constructor.

Author:
tmatthew

Field Summary
protected  java.lang.String CLASS_NAME
           
protected  boolean m_bInitialized
           
protected  boolean m_bShutdown
           
protected  java.io.InputStream m_is
           
protected  java.io.OutputStream m_os
           
protected  int packet_size
           
protected  java.lang.String port
           
protected  javax.comm.SerialPort serialPort
           
 
Fields inherited from class edu.berkeley.guir.ptk.PTK
debug, MAX_DEBUG, MED_DEBUG, MIN_DEBUG, NO_DEBUG
 
Constructor Summary
SerialReadWrite()
           
SerialReadWrite(int packet_size)
           
SerialReadWrite(java.lang.String port)
           
SerialReadWrite(java.lang.String port, int packet_size)
           
 
Method Summary
 boolean closeSource()
          Close the serial port.
 boolean openSource()
          Opens the serial port for reading and writing.
 void printAllPorts()
          Print the ports your machine has available.
 java.lang.String readLine()
          Read a line (up until the first newline) from the serial port.
 byte[] readPacket()
          Read a packet of size SerialReadWrite.packet_size from the serial port and return it as a byte array.
 boolean writePacket(java.lang.String message)
          Write a String to the serial port as a byte array.
 boolean writeString(java.lang.String message)
          Write a String to the serial port.
 
Methods inherited from class edu.berkeley.guir.ptk.PTK
getMAX, getMED, getMIN, getNO, printDebug, printDebug, printError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

packet_size

protected int packet_size

port

protected java.lang.String port

m_is

protected java.io.InputStream m_is

m_os

protected java.io.OutputStream m_os

CLASS_NAME

protected java.lang.String CLASS_NAME

m_bInitialized

protected boolean m_bInitialized

m_bShutdown

protected boolean m_bShutdown

serialPort

protected javax.comm.SerialPort serialPort
Constructor Detail

SerialReadWrite

public SerialReadWrite()

SerialReadWrite

public SerialReadWrite(java.lang.String port,
                       int packet_size)

SerialReadWrite

public SerialReadWrite(java.lang.String port)

SerialReadWrite

public SerialReadWrite(int packet_size)
Method Detail

openSource

public boolean openSource()
Opens the serial port for reading and writing.

Returns:
success

readLine

public java.lang.String readLine()
Read a line (up until the first newline) from the serial port.

Returns:
the String read from the serial port.

readPacket

public byte[] readPacket()
Read a packet of size SerialReadWrite.packet_size from the serial port and return it as a byte array.


closeSource

public boolean closeSource()
Close the serial port.

Returns:
success

writeString

public boolean writeString(java.lang.String message)
Write a String to the serial port.

Parameters:
message - the String to write.
Returns:
boolean indicating success of write. If false, the OutputStream could be null.

writePacket

public boolean writePacket(java.lang.String message)
Write a String to the serial port as a byte array.

Parameters:
message - the String to write.
Returns:
boolean indicating success of write. If false, the OutputStream could be null.

printAllPorts

public void printAllPorts()
Print the ports your machine has available.