SerialPort

class wpilib.SerialPort(*args, **kwargs)

Bases: wpilib.ErrorBase

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 fomatted buffers. This allows the intermingled use of Printf(), Scanf(), 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

Overloaded function.

  1. __init__(self: wpilib._wpilib.SerialPort, baudRate: int, port: wpilib._wpilib.SerialPort.Port = <Port.kOnboard: 0>, dataBits: int = 8, parity: wpilib._wpilib.SerialPort.Parity = <Parity.kParity_None: 0>, stopBits: wpilib._wpilib.SerialPort.StopBits = <StopBits.kStopBits_One: 10>) -> None

Create an instance of a Serial Port class.

Parameters
  • baudRate – The baud rate to configure the serial port.

  • port – The physical 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.

  1. __init__(self: wpilib._wpilib.SerialPort, baudRate: int, portName: str, port: wpilib._wpilib.SerialPort.Port = <Port.kOnboard: 0>, dataBits: int = 8, parity: wpilib._wpilib.SerialPort.Parity = <Parity.kParity_None: 0>, stopBits: wpilib._wpilib.SerialPort.StopBits = <StopBits.kStopBits_One: 10>) -> None

Create an instance of a Serial Port class.

Prefer to use the constructor that doesn’t take a port name, but in some cases the automatic detection might not work correctly.

Parameters
  • baudRate – The baud rate to configure the serial port.

  • port – The physical port to use

  • portName – The direct port name 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.

class FlowControl(self: wpilib._wpilib.SerialPort.FlowControl, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kFlowControl_None

kFlowControl_XonXoff

kFlowControl_RtsCts

kFlowControl_DtrDsr

kFlowControl_DtrDsr = <FlowControl.kFlowControl_DtrDsr: 4>
kFlowControl_None = <FlowControl.kFlowControl_None: 0>
kFlowControl_RtsCts = <FlowControl.kFlowControl_RtsCts: 2>
kFlowControl_XonXoff = <FlowControl.kFlowControl_XonXoff: 1>
property name
class Parity(self: wpilib._wpilib.SerialPort.Parity, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kParity_None

kParity_Odd

kParity_Even

kParity_Mark

kParity_Space

kParity_Even = <Parity.kParity_Even: 2>
kParity_Mark = <Parity.kParity_Mark: 3>
kParity_None = <Parity.kParity_None: 0>
kParity_Odd = <Parity.kParity_Odd: 1>
kParity_Space = <Parity.kParity_Space: 4>
property name
class Port(self: wpilib._wpilib.SerialPort.Port, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kOnboard

kMXP

kUSB

kUSB1

kUSB2

kMXP = <Port.kMXP: 1>
kOnboard = <Port.kOnboard: 0>
kUSB = <Port.kUSB: 2>
kUSB1 = <Port.kUSB: 2>
kUSB2 = <Port.kUSB2: 3>
property name
class StopBits(self: wpilib._wpilib.SerialPort.StopBits, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kStopBits_One

kStopBits_OnePointFive

kStopBits_Two

kStopBits_One = <StopBits.kStopBits_One: 10>
kStopBits_OnePointFive = <StopBits.kStopBits_OnePointFive: 15>
kStopBits_Two = <StopBits.kStopBits_Two: 20>
property name
class WriteBufferMode(self: wpilib._wpilib.SerialPort.WriteBufferMode, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kFlushOnAccess

kFlushWhenFull

kFlushOnAccess = <WriteBufferMode.kFlushOnAccess: 1>
kFlushWhenFull = <WriteBufferMode.kFlushWhenFull: 2>
property name
disableTermination(self: wpilib.SerialPort) → None

Disable termination behavior.

enableTermination(self: wpilib.SerialPort, terminator: str = '\n') → None

Enable termination and specify the termination character.

Termination is currently only implemented for receive. When the the terminator is received, the Read() or Scanf() will return fewer bytes than requested, stopping after the terminator.

Parameters

terminator – The character to use for termination.

flush(self: wpilib.SerialPort) → None

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.

getBytesReceived(self: wpilib.SerialPort) → int

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

Returns

The number of bytes available to read

read(self: wpilib.SerialPort, buffer: buffer) → int

Read raw bytes out of the buffer.

Parameters
  • buffer – Pointer to the buffer to store the bytes in.

  • count – The maximum number of bytes to read.

Returns

The number of bytes actually read into the buffer.

reset(self: wpilib.SerialPort) → None

Reset the serial port driver to a known state.

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

setFlowControl(self: wpilib.SerialPort, flowControl: wpilib._wpilib.SerialPort.FlowControl) → None

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

By default, flow control is disabled.

setReadBufferSize(self: wpilib.SerialPort, size: int) → None

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 or Scanf. 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.

setTimeout(self: wpilib.SerialPort, timeout: float) → None

Configure the timeout of the serial port.

This defines the timeout for transactions with the hardware. It will affect reads and very large writes.

Parameters

timeout – The number of seconds to to wait for I/O.

setWriteBufferMode(self: wpilib.SerialPort, mode: wpilib._wpilib.SerialPort.WriteBufferMode) → None

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 Printf() 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.

setWriteBufferSize(self: wpilib.SerialPort, size: int) → None

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.

write(self: wpilib.SerialPort, buffer: buffer) → int

Write raw bytes to the buffer.

Parameters
  • buffer – Pointer to 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.