"

2013 FRC Java API

"

edu.wpi.first.wpilibj
Class SerialPort

java.lang.Object
  extended by edu.wpi.first.wpilibj.SerialPort

public class SerialPort
extends Object

Driver for the RS-232 serial port on the cRIO. The current implementation uses the VISA formatted I/O mode. This means that all traffic goes through the formatted buffers. This allows the intermingled use of print(), readString(), and the raw buffer accessors read() and write(). More information can be found in the NI-VISA User Manual here: http://www.ni.com/pdf/manuals/370423a.pdf and the NI-VISA Programmer's Reference Manual here: http://www.ni.com/pdf/manuals/370132c.pdf


Nested Class Summary
static class SerialPort.FlowControl
          Represents what type of flow control to use for serial communication
static class SerialPort.Parity
          Represents the parity to use for serial communications
static class SerialPort.StopBits
          Represents the number of stop bits to use for Serial Communication
static class SerialPort.WriteBufferMode
          Represents which type of buffer mode to use when writing to a serial port
 
Constructor Summary
SerialPort(int baudRate)
          Create an instance of a Serial Port class.
SerialPort(int baudRate, int dataBits)
          Create an instance of a Serial Port class.
SerialPort(int baudRate, int dataBits, SerialPort.Parity parity)
          Create an instance of a Serial Port class.
SerialPort(int baudRate, int dataBits, SerialPort.Parity parity, SerialPort.StopBits stopBits)
          Create an instance of a Serial Port class.
 
Method Summary
 void disableTermination()
          Disable termination behavior.
 void enableTermination()
          Enable termination and specify the termination character.
 void enableTermination(char terminator)
          Enable termination and specify the termination character.
 void flush()
          Force the output buffer to be written to the port.
 void free()
          Destructor.
 int getBytesReceived()
          Get the number of bytes currently available to read from the serial port.
 void print(String write)
          Deprecated. use write(string.getBytes()) instead
 byte[] read(int count)
          Read raw bytes out of the buffer.
 String readString()
          Read a string out of the buffer.
 String readString(int count)
          Read a string out of the buffer.
 void reset()
          Reset the serial port driver to a known state.
 void setFlowControl(SerialPort.FlowControl flowControl)
          Set the type of flow control to enable on this port.
 void setTimeout(double timeout)
          Configure the timeout of the serial port.
 void setWriteBufferMode(SerialPort.WriteBufferMode mode)
          Specify the flushing behavior of the output buffer.
 int write(byte[] buffer, int count)
          Write raw bytes to the buffer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SerialPort

public SerialPort(int baudRate,
                  int dataBits,
                  SerialPort.Parity parity,
                  SerialPort.StopBits stopBits)
           throws VisaException
Create an instance of a Serial Port class.

Parameters:
baudRate - The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
dataBits - The number of data bits per transfer. Valid values are between 5 and 8 bits.
parity - Select the type of parity checking to use.
stopBits - The number of stop bits to use as defined by the enum StopBits.
Throws:
VisaException

SerialPort

public SerialPort(int baudRate,
                  int dataBits,
                  SerialPort.Parity parity)
           throws VisaException
Create an instance of a Serial Port class. Defaults to one stop bit.

Parameters:
baudRate - The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
dataBits - The number of data bits per transfer. Valid values are between 5 and 8 bits.
parity - Select the type of parity checking to use.
Throws:
VisaException

SerialPort

public SerialPort(int baudRate,
                  int dataBits)
           throws VisaException
Create an instance of a Serial Port class. Defaults to no parity and one stop bit.

Parameters:
baudRate - The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
dataBits - The number of data bits per transfer. Valid values are between 5 and 8 bits.
Throws:
VisaException

SerialPort

public SerialPort(int baudRate)
           throws VisaException
Create an instance of a Serial Port class. Defaults to 8 databits, no parity, and one stop bit.

Parameters:
baudRate - The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
Throws:
VisaException
Method Detail

free

public void free()
Destructor.


setFlowControl

public void setFlowControl(SerialPort.FlowControl flowControl)
                    throws VisaException
Set the type of flow control to enable on this port. By default, flow control is disabled.

Parameters:
flowControl -
Throws:
VisaException

enableTermination

public void enableTermination(char terminator)
                       throws VisaException
Enable termination and specify the termination character. Termination is currently only implemented for receive. When the the terminator is received, the read() or readString() will return fewer bytes than requested, stopping after the terminator.

Parameters:
terminator - The character to use for termination.
Throws:
VisaException

enableTermination

public void enableTermination()
                       throws VisaException
Enable termination and specify the termination character. Termination is currently only implemented for receive. When the the terminator is received, the read() or readString() will return fewer bytes than requested, stopping after the terminator. The default terminator is '\n'

Throws:
VisaException

disableTermination

public void disableTermination()
                        throws VisaException
Disable termination behavior.

Throws:
VisaException

getBytesReceived

public int getBytesReceived()
                     throws VisaException
Get the number of bytes currently available to read from the serial port.

Returns:
The number of bytes available to read.
Throws:
VisaException

print

public void print(String write)
           throws VisaException
Deprecated. use write(string.getBytes()) instead

Output formatted text to the serial port.

Parameters:
write - A string to write
Throws:
VisaException

readString

public String readString()
                  throws VisaException
Read a string out of the buffer. Reads the entire contents of the buffer

Returns:
The read string
Throws:
VisaException

readString

public String readString(int count)
                  throws VisaException
Read a string out of the buffer. Reads the entire contents of the buffer

Parameters:
count - the number of characters to read into the string
Returns:
The read string
Throws:
VisaException

read

public byte[] read(int count)
            throws VisaException
Read raw bytes out of the buffer.

Parameters:
count - The maximum number of bytes to read.
Returns:
An array of the read bytes
Throws:
VisaException

write

public int write(byte[] buffer,
                 int count)
          throws VisaException
Write raw bytes to the buffer.

Parameters:
buffer - the buffer to read the bytes from.
count - The maximum number of bytes to write.
Returns:
The number of bytes actually written into the port.
Throws:
VisaException

setTimeout

public void setTimeout(double timeout)
                throws VisaException
Configure the timeout of the serial port. This defines the timeout for transactions with the hardware. It will affect reads if less bytes are available than the read buffer size (defaults to 1) and very large writes.

Parameters:
timeout - The number of seconds to to wait for I/O.
Throws:
VisaException

setWriteBufferMode

public void setWriteBufferMode(SerialPort.WriteBufferMode mode)
                        throws VisaException
Specify the flushing behavior of the output buffer. When set to kFlushOnAccess, data is synchronously written to the serial port after each call to either print() or write(). When set to kFlushWhenFull, data will only be written to the serial port when the buffer is full or when flush() is called.

Parameters:
mode - The write buffer mode.
Throws:
VisaException

flush

public void flush()
           throws VisaException
Force the output buffer to be written to the port. This is used when setWriteBufferMode() is set to kFlushWhenFull to force a flush before the buffer is full.

Throws:
VisaException

reset

public void reset()
           throws VisaException
Reset the serial port driver to a known state. Empty the transmit and receive buffers in the device and formatted I/O.

Throws:
VisaException

"

2013 FRC Java API

"

"
For updated information see the Java FRC site
"