com.raben.telescope.comm
Class SerialConnection

java.lang.Object
  |
  +--com.raben.telescope.comm.SerialConnection
All Implemented Interfaces:
javax.comm.CommPortOwnershipListener, java.util.EventListener, javax.comm.SerialPortEventListener

public class SerialConnection
extends java.lang.Object
implements javax.comm.SerialPortEventListener, javax.comm.CommPortOwnershipListener

A class to handle communications with a serial connection

Version:
$Revision: 1.1.1.1 $ $Date: 2003/06/13 04:51:09 $
Author:
Vern Raben

Field Summary
 
Fields inherited from interface javax.comm.CommPortOwnershipListener
PORT_OWNED, PORT_OWNERSHIP_REQUESTED, PORT_UNOWNED
 
Constructor Summary
SerialConnection()
          Create a SerialConnection using default settings of 8 bits, 9600 baud, no parity, no flow control, and 1 stop bit on port named COM1 .
 
Method Summary
 void closeConnection()
          Close the port and clean up associated elements.
static java.lang.String displayCharArrayAsHexString(char[] chars)
          Convert array of bytes to space separated hex string for debug display
static java.lang.String[] getAvailablePorts()
          Get serial ports
 int getBaudRate()
          Get baud rate
 int getDatabits()
          Get data bits
 int getFlowControl()
          Get flow control
 int getParity()
          Get current parity setting
 java.lang.String getPortName()
          Get the port name being used by this connection
 int getStopbits()
          Get stop bits
 boolean isDebug()
          Check debug flag return true if set
 boolean isOpen()
          Check if the serial port is open
 void openConnection()
          Open a serial connection and IO streams using the current parameters
 void ownershipChange(int type)
          Handle ownership change event.
 char[] receiveChars()
          Receive character array from serial connection
 java.lang.String receiveString()
          Receive String from serial connection
 void sendChars(char[] chars)
          Send an array of chars to the serial connection
 void sendString(java.lang.String str)
          Send string to the serial connection
 void serialEvent(javax.comm.SerialPortEvent evt)
          Handle Serial Port Events.
 void setBaudRate(int baudRate)
          Set baud rate.
 void setDatabits(int databits)
          Set new data bits.
 void setDebug(boolean debug)
          Set debug flag
 void setFlowControl(int flowControl)
          Set flow control Formed by "or-ing" both "in" and "out" flow control settings.
 void setParity(int parity)
          Set parity
 void setPortName(java.lang.String portName)
          Set name of port to open
 void setStopbits(int stopbits)
          Set stop bits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SerialConnection

public SerialConnection()
Create a SerialConnection using default settings of 8 bits, 9600 baud, no parity, no flow control, and 1 stop bit on port named COM1 .

Method Detail

openConnection

public void openConnection()
                    throws java.io.IOException
Open a serial connection and IO streams using the current parameters

Throws:
java.io.IOException - may occur

closeConnection

public void closeConnection()
Close the port and clean up associated elements.


isOpen

public boolean isOpen()
Check if the serial port is open

Returns:
true if port is open, false if port is closed.

serialEvent

public void serialEvent(javax.comm.SerialPortEvent evt)
Handle Serial Port Events.

Specified by:
serialEvent in interface javax.comm.SerialPortEventListener
Parameters:
evt - SerialPortEvent

ownershipChange

public void ownershipChange(int type)
Handle ownership change event. When PORT_OWNERSHIP_REQUESTED event is received, close the connection.

Specified by:
ownershipChange in interface javax.comm.CommPortOwnershipListener
Parameters:
type - Type of owernership event

sendChars

public void sendChars(char[] chars)
               throws java.io.IOException
Send an array of chars to the serial connection

Parameters:
chars - Array of chars
Throws:
java.io.IOException - may occur

sendString

public void sendString(java.lang.String str)
                throws java.io.IOException
Send string to the serial connection

Parameters:
str - The string to send
Throws:
java.io.IOException - may occur

receiveString

public java.lang.String receiveString()
Receive String from serial connection

Returns:
String

receiveChars

public char[] receiveChars()
Receive character array from serial connection

Returns:
char[]

setPortName

public void setPortName(java.lang.String portName)
Set name of port to open

Parameters:
portName - (example for MS Windows COM1 or COM2)

getPortName

public java.lang.String getPortName()
Get the port name being used by this connection

Returns:
Current name of the port

setStopbits

public void setStopbits(int stopbits)
Set stop bits.

Parameters:
stopbits - New stop bits setting.

getStopbits

public int getStopbits()
Get stop bits

Returns:
Current stop bits.

setParity

public void setParity(int parity)
Set parity

Parameters:
parity - New parity setting.

getParity

public int getParity()
Get current parity setting

Returns:
parity settting

getBaudRate

public int getBaudRate()
Get baud rate

Returns:
Current baud rate.

setBaudRate

public void setBaudRate(int baudRate)
Set baud rate.

Parameters:
baudRate - New baud rate (9600 is default)

getDatabits

public int getDatabits()
Get data bits

Returns:
Current data bits setting.

setDatabits

public void setDatabits(int databits)
Set new data bits.

Parameters:
databits - New data bits setting.

setFlowControl

public void setFlowControl(int flowControl)
Set flow control Formed by "or-ing" both "in" and "out" flow control settings. The efault is SerialPort.FLOWCONTROL_NONE | SerialPort.FLOWCONTROL_NONE.

Parameters:
flowControl - Flow control settings setting
See Also:
for flow control definitions

getFlowControl

public int getFlowControl()
Get flow control

Returns:
Current flow control setting.

displayCharArrayAsHexString

public static java.lang.String displayCharArrayAsHexString(char[] chars)
Convert array of bytes to space separated hex string for debug display

Parameters:
chars - Array of chars
Returns:
String for display

setDebug

public void setDebug(boolean debug)
Set debug flag

Parameters:
debug - Set to true to list cmds sent, responses received

isDebug

public boolean isDebug()
Check debug flag return true if set

Returns:
boolean

getAvailablePorts

public static java.lang.String[] getAvailablePorts()
Get serial ports

Returns:
String array of ports on workstation (may return null)


Copyright © 2003 Raben Systems, Inc.. All Rights Reserved.