Class SerialPortJNI

java.lang.Object
edu.wpi.first.hal.JNIWrapper
edu.wpi.first.hal.SerialPortJNI

public class SerialPortJNI
extends JNIWrapper
Serial Port JNI HAL functions.
See Also:
"SerialPort.h"
  • Constructor Details

  • Method Details

    • serialInitializePort

      public static int serialInitializePort​(byte port)
      Initializes a serial port.

      The channels are either the onboard RS232, the MXP UART, or 2 USB ports. The top port is USB1, the bottom port is USB2.

      Parameters:
      port - the serial port to initialize
      Returns:
      Serial Port Handle
      See Also:
      "HAL_InitializeSerialPort"
    • serialInitializePortDirect

      public static int serialInitializePortDirect​(byte port, String portName)
      Initializes a serial port with a direct name.

      This name is the /dev name for a specific port. Note these are not always consistent between roboRIO reboots.

      Parameters:
      port - the serial port to initialize
      portName - the dev port name
      Returns:
      Serial Port Handle
      See Also:
      "HAL_InitializeSerialPortDirect"
    • serialSetBaudRate

      public static void serialSetBaudRate​(int handle, int baud)
      Sets the baud rate of a serial port.

      Any value between 0 and 0xFFFFFFFF may be used. Default is 9600.

      Parameters:
      handle - the serial port handle
      baud - the baud rate to set
      See Also:
      "HAL_SetSerialBaudRate"
    • serialSetDataBits

      public static void serialSetDataBits​(int handle, byte bits)
      Sets the number of data bits on a serial port.

      Defaults to 8.

      Parameters:
      handle - the serial port handle
      bits - the number of data bits (5-8)
      See Also:
      "HAL_SetSerialDataBits"
    • serialSetParity

      public static void serialSetParity​(int handle, byte parity)
      Sets the number of parity bits on a serial port.

      Valid values are: 0: None (default) 1: Odd 2: Even 3: Mark - Means exists and always 1 4: Space - Means exists and always 0

      Parameters:
      handle - the serial port handle
      parity - the parity bit mode (see remarks for valid values)
      See Also:
      "HAL_SetSerialParity"
    • serialSetStopBits

      public static void serialSetStopBits​(int handle, byte stopBits)
      Sets the number of stop bits on a serial port.

      Valid values are: 10: One stop bit (default) 15: One and a half stop bits 20: Two stop bits

      Parameters:
      handle - the serial port handle
      stopBits - the stop bit value (see remarks for valid values)
      See Also:
      "HAL_SetSerialStopBits"
    • serialSetWriteMode

      public static void serialSetWriteMode​(int handle, byte mode)
      Sets the write mode on a serial port.

      Valid values are: 1: Flush on access 2: Flush when full (default)

      Parameters:
      handle - the serial port handle
      mode - the mode to set (see remarks for valid values)
      See Also:
      "HAL_SetSerialWriteMode"
    • serialSetFlowControl

      public static void serialSetFlowControl​(int handle, byte flow)
      Sets the flow control mode of a serial port.

      Valid values are: 0: None (default) 1: XON-XOFF 2: RTS-CTS 3: DTR-DSR

      Parameters:
      handle - the serial port handle
      flow - the mode to set (see remarks for valid values)
      See Also:
      "HAL_SetSerialFlowControl"
    • serialSetTimeout

      public static void serialSetTimeout​(int handle, double timeout)
      Sets the minimum serial read timeout of a port.
      Parameters:
      handle - the serial port handle
      timeout - the timeout in milliseconds
      See Also:
      "HAL_SetSerialTimeout"
    • serialEnableTermination

      public static void serialEnableTermination​(int handle, char terminator)
      Sets the termination character that terminates a read.

      By default this is disabled.

      Parameters:
      handle - the serial port handle
      terminator - the termination character to set
      See Also:
      "HAL_EnableSerialTermination"
    • serialDisableTermination

      public static void serialDisableTermination​(int handle)
      Disables a termination character for reads.
      Parameters:
      handle - the serial port handle
      See Also:
      "HAL_DisableSerialTermination"
    • serialSetReadBufferSize

      public static void serialSetReadBufferSize​(int handle, int size)
      Sets the size of the read buffer.
      Parameters:
      handle - the serial port handle
      size - the read buffer size
      See Also:
      "HAL_SetSerialReadBufferSize"
    • serialSetWriteBufferSize

      public static void serialSetWriteBufferSize​(int handle, int size)
      Sets the size of the write buffer.
      Parameters:
      handle - the serial port handle
      size - the write buffer size
      See Also:
      "HAL_SetSerialWriteBufferSize"
    • serialGetBytesReceived

      public static int serialGetBytesReceived​(int handle)
      Gets the number of bytes currently in the read buffer.
      Parameters:
      handle - the serial port handle
      Returns:
      the number of bytes in the read buffer
      See Also:
      "HAL_GetSerialBytesReceived"
    • serialRead

      public static int serialRead​(int handle, byte[] buffer, int count)
      Reads data from the serial port.

      Will wait for either timeout (if set), the termination char (if set), or the count to be full. Whichever one comes first.

      Parameters:
      handle - the serial port handle
      buffer - the buffer in which to store bytes read
      count - the number of bytes maximum to read
      Returns:
      the number of bytes actually read
      See Also:
      "HAL_ReadSerial"
    • serialWrite

      public static int serialWrite​(int handle, byte[] buffer, int count)
      Writes data to the serial port.
      Parameters:
      handle - the serial port handle
      buffer - the buffer to write
      count - the number of bytes to write from the buffer
      Returns:
      the number of bytes actually written
      See Also:
      "HAL_WriteSerial"
    • serialFlush

      public static void serialFlush​(int handle)
      Flushes the serial write buffer out to the port.
      Parameters:
      handle - the serial port handle
      See Also:
      "HAL_FlushSerial"
    • serialClear

      public static void serialClear​(int handle)
      Clears the receive buffer of the serial port.
      Parameters:
      handle - the serial port handle
      See Also:
      "HAL_ClearSerial"
    • serialClose

      public static void serialClose​(int handle)
      Closes a serial port.
      Parameters:
      handle - the serial port handle to close
      See Also:
      "HAL_CloseSerial"