This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.
ofSerial provides a cross platform system for interfacing with the serial port. You can choose the port and baud rate, and then read and send data. Please note that the port must be set manually in the code, so you should be clear what port your device is on. For example, Arduino users should check the arduino app to see what port their device is on. Alternatively the ofSerial class can attempt to communicate with the first available device it finds. More...
#include <ofSerial.h>
Public Member Functions | |
Constructor and Destructor | |
ofSerial () | |
virtual | ~ofSerial () |
List Devices | |
void | listDevices () |
This lists all the available serial devices to the console or standard output. | |
OF_DEPRECATED_MSG ("Use listDevices() instead", void enumerateDevices()) | |
Prints out the available serial devices. | |
std::vector< ofSerialDeviceInfo > | getDeviceList () |
Returns a vector of ofSerialDeviceInfo instances with the devicePath, deviceName, deviceID set. | |
Serial Connection | |
bool | setup () |
Attempts to setup the first available device at a baud rate of 9600. | |
bool | setup (std::string portName, int baudrate) |
Opens the serial port, with the given name and baud rate. | |
bool | setup (int deviceNumber, int baudrate) |
Opens the serial port based on the order in which is listed and sets the baud rate. | |
bool | isInitialized () const |
void | close () |
Closes the connection to the serial device. | |
Read Data | |
int | available () |
The available method is useful when you want to know how many bytes are available in the serial port. For instance, if you only want to read when there are 8 bytes waiting for you, you would do: | |
long | readBytes (unsigned char *buffer, size_t length) |
Reads 'length' bytes from the connected serial device. | |
long | readBytes (char *buffer, size_t length) |
long | readBytes (ofBuffer &buffer, size_t length) |
int | readByte () |
Reads and returns a single byte from the requested device. | |
Write Data | |
long | writeBytes (const unsigned char *buffer, size_t length) |
This writes bytes into the serial buffer from the buffer pointer passed in. | |
long | writeBytes (const char *buffer, size_t length) |
long | writeBytes (const ofBuffer &buffer) |
bool | writeByte (unsigned char singleByte) |
Writes a single byte to the connected serial device. | |
bool | writeByte (char singleByte) |
Clear Data | |
void | flush (bool flushIn=true, bool flushOut=true) |
Clears data from one or both of the serial buffers. | |
void | drain () |
Drain is only available on OSX and Linux and is very similar to flush(), but blocks until all the data has been written to or read from the serial port. | |
Protected Member Functions | |
void | buildDeviceList () |
Enumerate all devices attached to a serial port. | |
Protected Attributes | |
std::string | deviceType |
std::vector< ofSerialDeviceInfo > | devices |
< | |
bool | bHaveEnumeratedDevices |
< This vector stores information about all serial devices found. | |
bool | bInited |
< | |
int | fd |
< | |
struct termios | oldoptions |
This is the set of (current) terminal attributes to be reused when changing a subset of options. | |
Detailed Description
ofSerial provides a cross platform system for interfacing with the serial port. You can choose the port and baud rate, and then read and send data. Please note that the port must be set manually in the code, so you should be clear what port your device is on. For example, Arduino users should check the arduino app to see what port their device is on. Alternatively the ofSerial class can attempt to communicate with the first available device it finds.
To start up a serial connection to another device you do the following:
Constructor & Destructor Documentation
◆ ofSerial()
ofSerial::ofSerial | ( | ) |
Initializes the serial connection, but doesn't actually open the connection to any devices. You'll need to use the setup() method before doing that.
◆ ~ofSerial()
|
virtual |
Member Function Documentation
◆ available()
int ofSerial::available | ( | ) |
The available method is useful when you want to know how many bytes are available in the serial port. For instance, if you only want to read when there are 8 bytes waiting for you, you would do:
This is useful when you know how long a complete message from a device is going to be.
◆ buildDeviceList()
|
protected |
Enumerate all devices attached to a serial port.
This method tries to collect basic information about all devices attached to a serial port.
- See also
- ofSerial::listDevices()
- enumerateWin32Ports()
◆ close()
void ofSerial::close | ( | ) |
Closes the connection to the serial device.
◆ drain()
void ofSerial::drain | ( | ) |
Drain is only available on OSX and Linux and is very similar to flush(), but blocks until all the data has been written to or read from the serial port.
◆ flush()
void ofSerial::flush | ( | bool | flushIn = true , |
bool | flushOut = true |
||
) |
Clears data from one or both of the serial buffers.
Any data in the cleared buffers is discarded.
- Parameters
-
flushIn If true then it clears the incoming data buffer flushOut If true then it clears the outgoing data buffer.
◆ getDeviceList()
vector< ofSerialDeviceInfo > ofSerial::getDeviceList | ( | ) |
Returns a vector of ofSerialDeviceInfo instances with the devicePath, deviceName, deviceID set.
◆ isInitialized()
bool ofSerial::isInitialized | ( | ) | const |
◆ listDevices()
void ofSerial::listDevices | ( | ) |
This lists all the available serial devices to the console or standard output.
On OSX and Linux this will return all the devices listed in /dev tty and cu, so you might want to compare it against a list of devices that you're expecting if you want to use it to dynamically connect to a device.
◆ OF_DEPRECATED_MSG()
ofSerial::OF_DEPRECATED_MSG | ( | "Use listDevices() instead" | , |
void | enumerateDevices() | ||
) |
◆ readByte()
int ofSerial::readByte | ( | ) |
Reads and returns a single byte from the requested device.
- Returns
- The single byte as integer. If there is no data it will return
OF_SERIAL_NO_DATA
, and on error it returnsOF_SERIAL_ERROR
◆ readBytes() [1/3]
long ofSerial::readBytes | ( | char * | buffer, |
size_t | length | ||
) |
◆ readBytes() [2/3]
long ofSerial::readBytes | ( | ofBuffer & | buffer, |
size_t | length | ||
) |
◆ readBytes() [3/3]
long ofSerial::readBytes | ( | unsigned char * | buffer, |
size_t | length | ||
) |
Reads 'length' bytes from the connected serial device.
In some cases it may read less than 'length' bytes, so for reliable reading of >1 bytes of data the return value must be checked against the number of bytes requested, and if fewer bytes than requested were read then the call must be tried again.
This function should only be called when Serial.available() is reporting >0 bytes available.
An example of how to reliably read 8 bytes:
Be aware that the type of your buffer can only be unsigned char. If you're trying to receieve ints or signed chars over a serial connection you'll need to do some bit manipulation to correctly interpret that values.
◆ setup() [1/3]
bool ofSerial::setup | ( | ) |
◆ setup() [2/3]
bool ofSerial::setup | ( | int | deviceNumber, |
int | baudrate | ||
) |
◆ setup() [3/3]
bool ofSerial::setup | ( | std::string | portName, |
int | baudrate | ||
) |
◆ writeByte() [1/2]
bool ofSerial::writeByte | ( | char | singleByte | ) |
◆ writeByte() [2/2]
bool ofSerial::writeByte | ( | unsigned char | singleByte | ) |
Writes a single byte to the connected serial device.
Check the return value to be sure the data was written.
◆ writeBytes() [1/3]
long ofSerial::writeBytes | ( | const char * | buffer, |
size_t | length | ||
) |
◆ writeBytes() [2/3]
long ofSerial::writeBytes | ( | const ofBuffer & | buffer | ) |
◆ writeBytes() [3/3]
long ofSerial::writeBytes | ( | const unsigned char * | buffer, |
size_t | length | ||
) |
This writes bytes into the serial buffer from the buffer pointer passed in.
Member Data Documentation
◆ bHaveEnumeratedDevices
|
protected |
< This vector stores information about all serial devices found.
◆ bInited
|
protected |
<
Indicate having enumerated devices (serial ports) available.
◆ devices
|
protected |
<
Name of the device on the other end of the serial connection.
◆ deviceType
|
protected |
◆ fd
|
protected |
<
Indicate the successful initialization of the serial connection.
File descriptor for the serial port.
◆ oldoptions
|
protected |
This is the set of (current) terminal attributes to be reused when changing a subset of options.
The documentation for this class was generated from the following files:
- /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/communication/ofSerial.h
- /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/communication/ofSerial.cpp