edu.wpi.first.wpilibj

## Class SerialPort

- java.lang.Object
- - edu.wpi.first.wpilibj.SerialPort

-     public class SerialPort
      extends java.lang.Object

  Driver for the RS-232 serial port on the roboRIO.

  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

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `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.Port`
    `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 m_port.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SerialPort(int baudRate, SerialPort.Port port)` Create an instance of a Serial Port class.
    `SerialPort(int baudRate, SerialPort.Port port, int dataBits)` Create an instance of a Serial Port class.
    `SerialPort(int baudRate, SerialPort.Port port, int dataBits, SerialPort.Parity parity)` Create an instance of a Serial Port class.
    `SerialPort(int baudRate, SerialPort.Port port, int dataBits, SerialPort.Parity parity, SerialPort.StopBits stopBits)` Create an instance of a Serial Port class.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `disableTermination()` Disable termination behavior.
    `void` | `enableTermination()` Enable termination with the default terminator '\n'
    `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.
    `byte[]` | `read(int count)` Read raw bytes out of the buffer.
    `java.lang.String` | `readString()` Read a string out of the buffer.
    `java.lang.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` | `setReadBufferSize(int size)` Specify the size of the input buffer.
    `void` | `setTimeout(double timeout)` Configure the timeout of the serial m_port.
    `void` | `setWriteBufferMode(SerialPort.WriteBufferMode mode)` Specify the flushing behavior of the output buffer.
    `void` | `setWriteBufferSize(int size)` Specify the size of the output buffer.
    `int` | `write(byte[] buffer, int count)` Write raw bytes to the serial port.
    `int` | `writeString(java.lang.String data)` Write a string to the serial port.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SerialPort

          public SerialPort(int baudRate,
                            SerialPort.Port port,
                            int dataBits,
                            SerialPort.Parity parity,
                            SerialPort.StopBits stopBits)

      Create an instance of a Serial Port class.
      Parameters:
        `baudRate` - The baud rate to configure the serial port.
        `port` - The Serial port to use
        `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.

    - #### SerialPort

          public SerialPort(int baudRate,
                            SerialPort.Port port,
                            int dataBits,
                            SerialPort.Parity parity)

      Create an instance of a Serial Port class. Defaults to one stop bit.
      Parameters:
        `baudRate` - The baud rate to configure the serial port.
        `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.

    - #### SerialPort

          public SerialPort(int baudRate,
                            SerialPort.Port port,
                            int dataBits)

      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.
        `dataBits` - The number of data bits per transfer. Valid values are between 5 and 8 bits.

    - #### SerialPort

          public SerialPort(int baudRate,
                            SerialPort.Port port)

      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.

  - ### Method Detail

    - #### free

          public void free()

      Destructor.

    - #### setFlowControl

          public void setFlowControl(SerialPort.FlowControl flowControl)

      Set the type of flow control to enable on this port.

      By default, flow control is disabled.

      Parameters:
        `flowControl` - the FlowControl m_value to use

    - #### enableTermination

          public void enableTermination(char terminator)

      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.

    - #### enableTermination

          public void enableTermination()

      Enable termination with the default terminator '\n'

      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'

    - #### disableTermination

          public void disableTermination()

      Disable termination behavior.

    - #### getBytesReceived

          public int getBytesReceived()

      Get the number of bytes currently available to read from the serial port.
      Returns:
        The number of bytes available to read.

    - #### readString

          public java.lang.String readString()

      Read a string out of the buffer. Reads the entire contents of the buffer
      Returns:
        The read string

    - #### readString

          public java.lang.String readString(int count)

      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

    - #### read

          public byte[] read(int count)

      Read raw bytes out of the buffer.
      Parameters:
        `count` - The maximum number of bytes to read.
      Returns:
        An array of the read bytes

    - #### write

          public int write(byte[] buffer,
                           int count)

      Write raw bytes to the serial port.
      Parameters:
        `buffer` - The buffer of bytes to write.
        `count` - The maximum number of bytes to write.
      Returns:
        The number of bytes actually written into the port.

    - #### writeString

          public int writeString(java.lang.String data)

      Write a string to the serial port.
      Parameters:
        `data` - The string to write to the serial port.
      Returns:
        The number of bytes actually written into the port.

    - #### setTimeout

          public void setTimeout(double timeout)

      Configure the timeout of the serial m_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.

    - #### setReadBufferSize

          public void setReadBufferSize(int size)

      Specify the size of the input buffer.

      Specify the amount of data that can be stored before data from the device is returned to Read. If you want data that is received to be returned immediately, set this to 1.

      It the buffer is not filled before the read timeout expires, all data that has been received so far will be returned.

      Parameters:
        `size` - The read buffer size.

    - #### setWriteBufferSize

          public void setWriteBufferSize(int size)

      Specify the size of the output buffer.

      Specify the amount of data that can be stored before being transmitted to the device.

      Parameters:
        `size` - The write buffer size.

    - #### setWriteBufferMode

          public void setWriteBufferMode(SerialPort.WriteBufferMode mode)

      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.

    - #### flush

          public void flush()

      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.

    - #### reset

          public void reset()

      Reset the serial port driver to a known state.

      Empty the transmit and receive buffers in the device and formatted I/O.