jcontrol.comm
Class RS232

java.lang.Object
  extended by jcontrol.comm.RS232
All Implemented Interfaces:
ConsoleInputStream, ConsoleOutputStream, BasicInputStream, BasicOutputStream, ComplexInputStream, ComplexOutputStream
Direct Known Subclasses:
FT1_2

public class RS232
extends Object
implements BasicInputStream, BasicOutputStream, ComplexInputStream, ComplexOutputStream, ConsoleOutputStream, ConsoleInputStream

Implements RS232 communication for JControl.

Author:
Marcus Timmermann, Andreas Wesseler, Helge Böhme
Available on JControl Devices:
all

Field Summary
static int ECHO
          Enables echo for received characters.
static int FLOWCONTROL_RTSCTS
          Enables hardware handshaking using RTS and CTS lines.
static int FLOWCONTROL_XONXOFF
          Enables software handshaking using XON and XOFF symbols.
static int PARITY_EVEN
          Enables even parity "8E1" (default: no parity "8N1").
static int PARITY_ODD
          Enables odd parity "8O1" (default: no parity "8N1").
 
Fields inherited from interface jcontrol.comm.ConsoleOutputStream
STD_LF
 
Fields inherited from interface jcontrol.comm.ConsoleInputStream
LF_CHARS
 
Constructor Summary
RS232()
          Constructs a new RS232 I/O-stream using the default baudrate.
RS232(int baudrate)
          Constructs a new RS232 I/O-stream using the specified baudrate.
 
Method Summary
 int available()
          Returns the number of available characters in the input buffer.
 void close()
          Closes the stream, further writes and reads are not possible, the RS232 hardware will be turned off.
 int errorCode()
          Returns the current Error-Code.
 void print(String text)
          Writes a String to the serial line.
 void println()
          Writes a linefeed to the serial line.
 void println(String text)
          Writes a String and a linefeed to the serial line.
 char read()
          Reads the next available character from the input buffer.
 int read(byte[] buffer, int index, int length)
          Reads a byte array from the input buffer.
 String readLine()
          Reads a single String from the serial line.
 String readUTF8()
          Reads a String from the serial line using Java coding with 2 size bytes first.
 void setBaudrate(int baudrate)
          Sets the serial port baudrate.
 void setParams(int params)
          Sets some serial port feature parameters.
 int write(byte[] buffer, int index, int length)
          Writes a block of bytes to the serial port -- as it is.
 void write(char data)
          Writes one byte to the serial port.
 void writeUTF8(String text)
          Writes a String to the serial line using Java coding with 2 size bytes first.
 
Methods inherited from class java.lang.Object
clone, equals, notifyAll, wait
 

Field Detail

ECHO

public static final int ECHO
Enables echo for received characters.

See Also:
setParams(int), Constant Field Values

FLOWCONTROL_RTSCTS

public static final int FLOWCONTROL_RTSCTS
Enables hardware handshaking using RTS and CTS lines. May be ignored on some devices. Do not use in conjunction with FLOWCONTROL_XONXOFF.

See Also:
setParams(int), Constant Field Values

FLOWCONTROL_XONXOFF

public static final int FLOWCONTROL_XONXOFF
Enables software handshaking using XON and XOFF symbols. Use only for ASCII based communication. Do not use in conjunction with FLOWCONTROL_RTSCTS.

See Also:
setParams(int), Constant Field Values

PARITY_EVEN

public static final int PARITY_EVEN
Enables even parity "8E1" (default: no parity "8N1"). Do not use in conjunction with PARITY_ODD.

See Also:
setParams(int), PARITY_ODD, Constant Field Values

PARITY_ODD

public static final int PARITY_ODD
Enables odd parity "8O1" (default: no parity "8N1"). Do not use in conjunction with PARITY_EVEN.

See Also:
setParams(int), PARITY_EVEN, Constant Field Values
Constructor Detail

RS232

public RS232()
      throws IOException
Constructs a new RS232 I/O-stream using the default baudrate. On the same hardware there are no multiple instances allowed. The default baudrate is specified in the system property "rs232.baudrate".

Throws:
IOException - if port is in use

RS232

public RS232(int baudrate)
      throws IOException
Constructs a new RS232 I/O-stream using the specified baudrate.

Parameters:
baudrate - the bautrate to use, possible values are: 2400, 9600, 19200 (default), 31250, 63 (62500), 125 (125000), 250 (250000). This is controller dependent.
Throws:
IOException - if port is in use
See Also:
setBaudrate(int)
Method Detail

available

public int available()
Returns the number of available characters in the input buffer.

Returns:
the number of available chars

close

public void close()
Closes the stream, further writes and reads are not possible, the RS232 hardware will be turned off.

Specified by:
close in interface BasicInputStream
Specified by:
close in interface BasicOutputStream

errorCode

public int errorCode()
Returns the current Error-Code. If an error occurs (IOException) the cause may be found here. The Error-Code is cleared after readout (if not, the error remains active and every RS232 access will throw another IOException). If an overrun error is indicated the FIFO should be cleared using read(...).

Returns:
the error code (the values ar or-ed together):
1 -- Parity error
2 -- Framing error
8 -- UART overrun error
16 -- FIFO buffer overrun
Additionally there are some information flags (not cleared):
4 -- sndXOFF, remembers last sent XOFF state
32 -- recAbortLF, set if an readLine() is waiting for a linefeed character
64 -- recXOFF, remembers last received XOFF state
128 -- Inactive, set if the port is closed.

print

public void print(String text)
Writes a String to the serial line. Unicode characters will not be decoded.

Specified by:
print in interface ConsoleOutputStream
Parameters:
text - String to write, its temporary stored in heap
See Also:
String

println

public void println()
Writes a linefeed to the serial line.

Specified by:
println in interface ConsoleOutputStream

println

public void println(String text)
Writes a String and a linefeed to the serial line. Unicode characters will not be decoded.

Specified by:
println in interface ConsoleOutputStream
Parameters:
text - String to write, its temporary stored in heap
See Also:
String

read

public char read()
          throws IOException
Reads the next available character from the input buffer.

Specified by:
read in interface BasicInputStream
Returns:
the read out character
Throws:
IOException - if an error occurs
See Also:
available()

read

public int read(byte[] buffer,
                int index,
                int length)
         throws IOException
Reads a byte array from the input buffer.

Specified by:
read in interface BasicInputStream
Parameters:
buffer - the byte array to fill
index - the index to start filling the array
length - number of bytes to read; the array is filled from index to index+length-1
Returns:
the number of bytes read
Throws:
IOException - if an error occurs

readLine

public String readLine()
                throws IOException
Reads a single String from the serial line. The String is received character-by-character until an linefeed character defined in ConsoleInputStream is received. There is a buffer if size BUFFERSIZE allocated to store the received bytes, if the buffer is full a linefeed is forced.

Specified by:
readLine in interface ConsoleInputStream
Returns:
the String read
Throws:
IOException - if an error occurs
See Also:
String

readUTF8

public String readUTF8()
                throws IOException
Reads a String from the serial line using Java coding with 2 size bytes first.

Specified by:
readUTF8 in interface ComplexInputStream
Returns:
read String
Throws:
IOException - if an error occurs
See Also:
String

setBaudrate

public void setBaudrate(int baudrate)
Sets the serial port baudrate. This is controller dependent.

Parameters:
baudrate - the baudrate to use, possible values are:
Baudrates for JControl/Stamp
BaudrateParameter
600600
12001200
24002400
48004800
96009600
19.20019200
31.25031250
38.40038
62.50062
125.000125
250.000250

Baudrates for JControl/SmartDisplay
BaudrateParameter
300300
600600
12001200
24002400
48004800
96009600
19.20019200
31.25031250
62.50062
125.000125
250.000250
Defaults to the value specified by the system property "rs232.baudrate".

setParams

public void setParams(int params)
Sets some serial port feature parameters. Defaults to the value specified by the system property "rs232.params".

Parameters:
params - bitmask with parameters
See Also:
PARITY_EVEN, PARITY_ODD, ECHO, FLOWCONTROL_XONXOFF, FLOWCONTROL_RTSCTS

write

public int write(byte[] buffer,
                 int index,
                 int length)
          throws IOException
Writes a block of bytes to the serial port -- as it is.

Specified by:
write in interface BasicOutputStream
Parameters:
buffer - the byte array to send
index - the index to start reading the array
length - number of bytes to write; the array is read from index to index+length-1
Returns:
the number of sent bytes
Throws:
IOException - if an error occurs
See Also:
write(char c)

write

public void write(char data)
           throws IOException
Writes one byte to the serial port. The unicode character is trucated to one byte.

Specified by:
write in interface BasicOutputStream
Parameters:
data - the byte to send (coded in a short allowing a range 0 ... 255)
Throws:
IOException - if an error occurs
See Also:
write(byte[],int,int)

writeUTF8

public void writeUTF8(String text)
               throws IOException
Writes a String to the serial line using Java coding with 2 size bytes first.

Specified by:
writeUTF8 in interface ComplexOutputStream
Parameters:
text - String to write, its temporary stored in heap
Throws:
IOException - if an error occurs
See Also:
String