9. Orangutan Serial Port Communication

The OrangutanSerial class and the C functions in this section provide access to the serial port on the Baby Orangutan B, Orangutan LV-168, and 3pi. This allows two-way, TTL-level communication on pins PD0 (RX) and PD1 (TX) with another microcontroller, a serial device, or (through a USB-serial adapter or RS-232 level converter) a laptop or desktop computer. These functions are not available within the Arduino environment, which has its own serial functions.

When sending data, a USART_UDRE_vect interrupt vector is called after each byte is sent, allowing the library to automatically start sending the next byte from the send buffer. When receiving data, a USART_RX_vect interrupt vector is called after each byte is received, allowing the library to automatically store the byte in the receive buffer. To use a polling method instead of interrupts, see the setMode() and check() functions below.

For a higher level overview of this library and programs that show how this library can be used, please see Section 6.j of the Pololu AVR C/C++ Library User’s Guide.

Reference

C++ methods are shown in red.

C functions are shown in green.

static void OrangutanSerial::setBaudRate(unsigned long baud)

unsigned char serial_set_baud_rate(unsigned long baud)

Sets the baud rate on the serial port. Standard values up to 115200 should work fine; for higher speeds, please consult the AVR documentation.

static void OrangutanSerial::receive(char *buffer, unsigned char size)

void serial_receive(char *buffer, unsigned char size)

Sets up a buffer for background reception. This function returns immediately, but data arriving at the serial port will be copied into this buffer until size bytes have been stored.

static char OrangutanSerial::receiveBlocking(char *buffer, unsigned char size, unsigned int timeout_ms)

char serial_receive_blocking(char *buffer, unsigned char size, unsigned int timeout_ms)

Receives data, not returning until the buffer is full or the timeout (specified in milliseconds) has expired. This function is useful for simple programs and for situations in which you know exactly how many bytes to expect.

static void OrangutanSerial::receiveRing(char *buffer, unsigned char size)

void serial_receive_ring(char *)buffer, unsigned char size)

Sets up a ring buffer for background reception. This is a more advanced version of serial_receive that is useful when you need to read in data continuously without pauses. When the buffer is filled, getReceivedBytes() will reset to zero, and data will continue to be inserted at the beginning of the buffer.

static void OrangutanSerial::cancelReceive()

void serial_cancel_receive()

Stops background serial reception.

static inline unsigned char OrangutanSerial::getReceivedBytes()

unsigned char serial_get_received_bytes()

Gets the number of bytes that have been read into the buffer; this is also the index of the location at which the next received byte will be added.

static inline char OrangutanSerial::receiveBufferFull()

char serial_receive_buffer_full()

Returns true when the receive buffer has been filled with received bytes, so that serial reception is halted. This function should not be called when receiving data into a ring buffer.

static void OrangutanSerial::send(char *buffer, unsigned char size)

void serial_send(char *buffer, unsigned char size)

Sets up a buffer for background transmit. Data from this buffer will be transmitted until size bytes have been sent. If send() is called before sendBufferEmpty() returns true (when transmission of the last byte has started), the old buffer will be discarded and tramission will be cut short. This means that you should almost always wait until the data has been sent before calling this function again. See sendBlocking(), below, for an easy way to do this.

static void OrangutanSerial::sendBlocking(char *buffer, unsigned char size)

void serial_send_blocking(char *buffer, unsigned char size)

Same as send(), but waits until transmission of the last byte has started. When this function returns, it is safe to call send() or sendBlocking() again.

static inline unsigned char OrangutanSerial::getSentBytes()

unsigned char serial_get_sent_bytes()

Gets the number of bytes that have been sent since send() was called.

static char OrangutanSerial::sendBufferEmpty()

char serial_send_buffer_empty()

True when the send buffer is empty; when there are no more bytes to send.

static void OrangutanSerial::setMode(unsigned char mode)

void serial_set_mode(unsigned char mode)

Sets the serial library to use either interrupts (with the argument SERIAL_AUTOMATIC; the default) or polling (SERIAL_CHECK). If SERIAL_CHECK is selected, your code must call check() often to ensure reliable reception and timely transmission of data.

static char OrangutanSerial::getMode(unsigned char mode)

char serial_get_mode()

Returns the current serial mode,

static void OrangutanSerial::check()

void serial_check()

Checks for any bytes to be received or transmitted and performs the required action. Call this function often when in SERIAL_CHECK mode. In SERIAL_AUTOMATIC mode, you will not need to use this function.