SerialPort

class wpilib.SerialPort(baudRate, port, dataBits=8, parity=0, stopBits=10, simPort=None)[source]

Bases: 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

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.
  • simPort – This must be an object that implements all of the serial* functions from hal_impl that you use. See test_serial.py for an example.
class FlowControl[source]

Bases: object

kDtsDsr = 4
kNone = 0
kRtsCts = 2
kXonXoff = 1
class Parity[source]

Bases: object

kEven = 2
kMark = 3
kNone = 0
kOdd = 1
kSpace = 4
class Port[source]

Bases: object

kMXP = 1
kOnboard = 0
kUSB = 2
kUSB1 = 2
kUSB2 = 3
class StopBits[source]

Bases: object

kOne = 10
kOnePointFive = 15
kTwo = 20
class WriteBufferMode[source]

Bases: object

kFlushOnAccess = 1
kFlushWhenFull = 2
disableTermination()[source]

Disable termination behavior.

enableTermination(terminator=b'\n')[source]

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 (default is \n).
flush()[source]

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.

free()[source]

Destructor

getBytesReceived()[source]

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

Returns:The number of bytes available to read.
port
read(count)[source]

Read raw bytes out of the buffer.

Parameters:count – The maximum number of bytes to read.
Returns:A list containing the read bytes
readString(count=None)[source]

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
reset()[source]

Reset the serial port driver to a known state.

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

setFlowControl(flowControl)[source]

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

By default, flow control is disabled.

Parameters:flowControl – the FlowControl value to use
setReadBufferSize(size)[source]

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.
setTimeout(timeout)[source]

Configure the timeout of the serial self.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.
setWriteBufferMode(mode)[source]

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 (WriteBufferMode) – The write buffer mode.
setWriteBufferSize(size)[source]

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(buffer)[source]

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(data)[source]

Write an ASCII encoded 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.