C++ serial port class for ROS. More...
#include <serial_port.h>
Public Member Functions | |
int | baudRate () |
Get the current baud rate. More... | |
void | close () |
Close the serial port. More... | |
int | flush () |
Wrapper around tcflush. More... | |
void | open (const char *port_name, int baud_rate=115200) |
Open the serial port. More... | |
void | pauseStream () |
Pause streaming. More... | |
bool | portOpen () |
Check whether the port is open or not. More... | |
int | read (char *data, int max_length, int timeout=-1) |
Read from the port. More... | |
bool | readBetween (std::string *data, char start, char end, int timeout=-1) |
Read from the serial port between a start char and an end char. More... | |
int | readBytes (char *data, int length, int timeout=-1) |
Read a fixed number of bytes from the serial port. More... | |
int | readLine (char *data, int length, int timeout=-1) |
Read a line from the serial port. More... | |
bool | readLine (std::string *data, int timeout=-1) |
Read a line from the serial port. More... | |
void | resumeStream () |
Resume streaming. More... | |
SerialPort () | |
Constructor. More... | |
bool | startReadBetweenStream (std::function< void(std::string *)> f, char start, char end) |
Start a stream of readBetween() More... | |
bool | startReadLineStream (std::function< void(std::string *)> f) |
Start a stream of readLine(std::string*, int) More... | |
bool | startReadStream (std::function< void(char *, int)> f) |
Start a stream of read() More... | |
void | stopStream () |
Stop streaming. More... | |
int | write (const char *data, int length=-1) |
Write to the port. More... | |
~SerialPort () | |
Destructor. More... | |
Private Member Functions | |
void | readBetweenThread (char start, char end) |
Thread for a stream of readBetween() More... | |
void | readLineThread () |
Thread for a stream of readLine(std::string*, int) More... | |
void | readThread () |
Thread for a stream of read() More... | |
Private Attributes | |
int | baud_ |
Baud rate. More... | |
int | fd_ |
File descriptor. More... | |
std::function< void(std::string *)> | readBetweenCallback |
Stream readBetween callback boost function. More... | |
std::function< void(char *, int)> | readCallback |
Stream read callback boost function. More... | |
std::function< void(std::string *)> | readLineCallback |
Stream readLine callback boost function. More... | |
bool | stream_paused_ |
Whether streaming is paused or not. More... | |
bool | stream_stopped_ |
Whether streaming is stopped or not. More... | |
std::thread * | stream_thread_ |
Stream thread. More... | |
C++ serial port class for ROS.
This class was based on the serial port code found on the hokuyo_node as suggested by Blaise Gassend on the ros-users mailling list.
Definition at line 80 of file serial_port.h.
SerialPort::SerialPort | ( | ) |
Constructor.
Definition at line 62 of file serial_port.cpp.
SerialPort::~SerialPort | ( | ) |
Destructor.
Definition at line 67 of file serial_port.cpp.
|
inline |
Get the current baud rate.
Definition at line 111 of file serial_port.h.
void SerialPort::close | ( | ) |
Close the serial port.
This call essentially wraps close.
Definition at line 145 of file serial_port.cpp.
int SerialPort::flush | ( | ) |
Wrapper around tcflush.
Definition at line 405 of file serial_port.cpp.
void SerialPort::open | ( | const char * | port_name, |
int | baud_rate = 115200 |
||
) |
Open the serial port.
This opens the serial port for communication. Wrapper for open.
port_name | A null terminated character array containing the name of the port. |
baud_rate | Baud rate of the serial port. Defaults to 115200. |
tcsetattr returns true if at least one attribute was set. Hence, we might not have set everything on success.
Definition at line 73 of file serial_port.cpp.
void SerialPort::pauseStream | ( | ) |
Pause streaming.
Definition at line 544 of file serial_port.cpp.
|
inline |
Check whether the port is open or not.
Definition at line 105 of file serial_port.h.
int SerialPort::read | ( | char * | data, |
int | max_length, | ||
int | timeout = -1 |
||
) |
Read from the port.
This function allows to read data from the serial port. Simple wrapper for read.
data | Data coming from the serial port. |
max_length | Maximum length of the incoming data. |
timeout | Timeout in milliseconds. |
Definition at line 173 of file serial_port.cpp.
bool SerialPort::readBetween | ( | std::string * | data, |
char | start, | ||
char | end, | ||
int | timeout = -1 |
||
) |
Read from the serial port between a start char and an end char.
This function allows to read data from the serial port between a start and an end char.
data | Data coming from the serial port. |
start | Start character of the incoming data stream. |
end | End character of the incoming data stream. |
timeout | Timeout in milliseconds. |
Definition at line 326 of file serial_port.cpp.
|
private |
Thread for a stream of readBetween()
Stream version of the readBetween function.
start | Start character of the incoming data stream. |
end | End character of the incoming data stream. |
Definition at line 510 of file serial_port.cpp.
int SerialPort::readBytes | ( | char * | data, |
int | length, | ||
int | timeout = -1 |
||
) |
Read a fixed number of bytes from the serial port.
This function allows to read a fixed number of data bytes from the serial port, no more, no less.
data | Data coming from the serial port. |
length | Fixed length of the incoming data. |
timeout | Timeout in milliseconds. |
Definition at line 202 of file serial_port.cpp.
int SerialPort::readLine | ( | char * | data, |
int | length, | ||
int | timeout = -1 |
||
) |
Read a line from the serial port.
This function allows to read a line from the serial port. Data is return as char*
data | Data coming from the serial port. |
length | Length of the incoming data. |
timeout | Timeout in milliseconds. |
Definition at line 236 of file serial_port.cpp.
bool SerialPort::readLine | ( | std::string * | data, |
int | timeout = -1 |
||
) |
Read a line from the serial port.
This function allows to read a line from the serial port. Data is return as std::string
data | Data coming from the serial port. |
timeout | Timeout in milliseconds. |
Definition at line 274 of file serial_port.cpp.
|
private |
Thread for a stream of readLine(std::string*, int)
Stream version of the readLine function.
Definition at line 470 of file serial_port.cpp.
|
private |
Thread for a stream of read()
Stream version of the read function.
Definition at line 428 of file serial_port.cpp.
void SerialPort::resumeStream | ( | ) |
Resume streaming.
Definition at line 549 of file serial_port.cpp.
bool SerialPort::startReadBetweenStream | ( | std::function< void(std::string *)> | f, |
char | start, | ||
char | end | ||
) |
Start a stream of readBetween()
Stream version of the readBetween() function.
f | Callback boost function to receive the data. |
start | Start character of the incoming data stream. |
end | End character of the incoming data stream. |
Definition at line 495 of file serial_port.cpp.
bool SerialPort::startReadLineStream | ( | std::function< void(std::string *)> | f | ) |
Start a stream of readLine(std::string*, int)
Stream version of the readLine(std::string*, int) function.
f | Callback boost function to receive the data. |
Definition at line 456 of file serial_port.cpp.
bool SerialPort::startReadStream | ( | std::function< void(char *, int)> | f | ) |
Start a stream of read()
Stream version of the read function.
f | Callback boost function to receive the data. |
Definition at line 414 of file serial_port.cpp.
void SerialPort::stopStream | ( | ) |
Stop streaming.
Definition at line 535 of file serial_port.cpp.
int SerialPort::write | ( | const char * | data, |
int | length = -1 |
||
) |
Write to the port.
This function allows to send data through the serial port. Wraper for write.
data | Data to send in a character array or null terminated c string. |
length | Number of bytes being sent. Defaults to -1 if sending a c string. |
Definition at line 157 of file serial_port.cpp.
|
private |
Baud rate.
Definition at line 247 of file serial_port.h.
|
private |
File descriptor.
Definition at line 245 of file serial_port.h.
|
private |
Stream readBetween callback boost function.
Definition at line 281 of file serial_port.h.
|
private |
Stream read callback boost function.
Definition at line 277 of file serial_port.h.
|
private |
Stream readLine callback boost function.
Definition at line 279 of file serial_port.h.
|
private |
Whether streaming is paused or not.
Definition at line 284 of file serial_port.h.
|
private |
Whether streaming is stopped or not.
Definition at line 286 of file serial_port.h.
|
private |
Stream thread.
Definition at line 274 of file serial_port.h.