Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpSerial Class Reference

#include <visp3/core/vpSerial.h>

Public Types

enum  bytesize_t { fivebits = 5, sixbits = 6, sevenbits = 7, eightbits = 8 }
 
enum  parity_t { parity_none = 0, parity_odd = 1, parity_even = 2 }
 
enum  stopbits_t { stopbits_one = 1, stopbits_two = 2 }
 
enum  flowcontrol_t { flowcontrol_none = 0, flowcontrol_software, flowcontrol_hardware }
 

Public Member Functions

 vpSerial (const std::string &port="", unsigned long baudrate=9600, bytesize_t bytesize=eightbits, parity_t parity=parity_none, stopbits_t stopbits=stopbits_one, flowcontrol_t flowcontrol=flowcontrol_none)
 
virtual ~vpSerial ()
 
int available ()
 
void close ()
 
unsigned long getBaudrate ()
 
bytesize_t getBytesize ()
 
flowcontrol_t getFlowcontrol ()
 
parity_t getParity ()
 
std::string getPort ()
 
stopbits_t getStopbits ()
 
void open ()
 
bool read (char *c, long timeout_s)
 
std::string readline (const std::string &eol)
 
void setBaudrate (const unsigned long baudrate)
 
void setBytesize (const bytesize_t &bytesize)
 
void setFlowcontrol (const flowcontrol_t &flowcontrol)
 
void setParity (const parity_t &parity)
 
void setPort (const std::string &port)
 
void setStopbits (const stopbits_t &stopbits)
 
void write (const std::string &s)
 

Detailed Description

This class allows a serial port communication between 2 devices.

The following example shows how to write a string on port /dev/ttyUSB0 using the default constructor:

#include <visp3/core/vpSerial.h>
int main()
{
#ifndef WIN32
vpSerial serial("/dev/ttyUSB0");
serial.write("Hello world");
#endif
}
Note
This class is not implemented on windows-like OS.
Examples:
mbot-apriltag-2D-half-vs.cpp, mbot-apriltag-ibvs.cpp, mbot-apriltag-pbvs.cpp, testSerialRead.cpp, and testSerialWrite.cpp.

Definition at line 70 of file vpSerial.h.

Member Enumeration Documentation

◆ bytesize_t

Defines the possible byte sizes for the serial port.

Enumerator
fivebits 

Data is encoded with 5 bits.

sixbits 

Data is encoded with 6 bits.

sevenbits 

Data is encoded with 7 bits.

eightbits 

Data is encoded with 8 bits.

Definition at line 76 of file vpSerial.h.

◆ flowcontrol_t

Defines the possible flowcontrol types for the serial port.

Enumerator
flowcontrol_none 

No flow control.

flowcontrol_software 

Software flow control.

flowcontrol_hardware 

Hardware flow control.

Definition at line 103 of file vpSerial.h.

◆ parity_t

Defines the possible parity types for the serial port.

Enumerator
parity_none 

No parity check.

parity_odd 

Check for odd parity.

parity_even 

Check for even parity.

Definition at line 86 of file vpSerial.h.

◆ stopbits_t

Defines the possible stopbit types for the serial port.

Enumerator
stopbits_one 

1 stop bit is used

stopbits_two 

2 stop bits are used

Definition at line 95 of file vpSerial.h.

Constructor & Destructor Documentation

◆ vpSerial()

vpSerial::vpSerial ( const std::string &  port = "",
unsigned long  baudrate = 9600,
bytesize_t  bytesize = eightbits,
parity_t  parity = parity_none,
stopbits_t  stopbits = stopbits_one,
flowcontrol_t  flowcontrol = flowcontrol_none 
)

Creates a serial port object that opens the port if the parameter is not empty.

#include <visp3/core/vpSerial.h>
int main()
{
#ifndef WIN32
vpSerial serial("/dev/ttyUSB0");
#endif
}

Otherwise the port needs to be opened using open().

#include <visp3/core/vpSerial.h>
int main()
{
#ifndef WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.open();
#endif
}
Parameters
[in]port: Serial port name. A string similar to /dev/ttyUSB0, /dev/ttySO, /dev/ttyAMA0...
[in]baudrate: The baudrate parameter. Common values are 9600, 115200...
[in]bytesize: Size of each byte in the serial transmission of data. Default is 8 bits.
[in]parity: Parity parameter. Default is without parity check.
[in]stopbits: Number of stop bits used. Default is 1 stop bit.
[in]flowcontrol: Type of flowcontrol used. Default is no flow control.

Definition at line 100 of file vpSerial.cpp.

References open().

◆ ~vpSerial()

vpSerial::~vpSerial ( )
virtual

Destructor that closes the serial port.

Definition at line 114 of file vpSerial.cpp.

References close().

Member Function Documentation

◆ available()

int vpSerial::available ( )

Return the number of characters in the buffer.

Definition at line 189 of file vpSerial.cpp.

References vpException::fatalError.

◆ close()

void vpSerial::close ( )

Closes the serial port.

See also
open()

Definition at line 206 of file vpSerial.cpp.

References vpException::fatalError.

Referenced by write(), and ~vpSerial().

◆ getBaudrate()

unsigned long vpSerial::getBaudrate ( )
inline

Return the baud rate; 9600, 115200...

See also
setBaudrate()

Definition at line 121 of file vpSerial.h.

◆ getBytesize()

bytesize_t vpSerial::getBytesize ( )
inline

Return the byte size.

See also
setBytesize()

Definition at line 129 of file vpSerial.h.

◆ getFlowcontrol()

flowcontrol_t vpSerial::getFlowcontrol ( )
inline

Return the flow control type.

See also
setFlowcontrol()

Definition at line 137 of file vpSerial.h.

◆ getParity()

parity_t vpSerial::getParity ( )
inline

Return parity.

See also
setParity()

Definition at line 145 of file vpSerial.h.

◆ getPort()

std::string vpSerial::getPort ( )
inline

Return the serial port name like /dev/ttyUSB0, /dev/ttySO, /dev/ttyAMA0...

See also
setPort()

Definition at line 153 of file vpSerial.h.

◆ getStopbits()

stopbits_t vpSerial::getStopbits ( )
inline

Return number of stop bits used.

See also
setStopbits()

Definition at line 161 of file vpSerial.h.

◆ open()

void vpSerial::open ( )

Open the serial port. If the serial port name is empty, or if the serial port is already opened an exception vpException::fatalError is thrown.

The following example shows how to open the serial port /dev/ttyUSB0 without using the constructor:

#include <visp3/core/vpSerial.h>
int main()
{
#ifndef WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.setBaudrate(9600);
serial.open();
#endif
}

Definition at line 249 of file vpSerial.cpp.

References vpException::fatalError.

Referenced by vpSerial().

◆ read()

bool vpSerial::read ( char *  c,
long  timeout_s 
)

Read one character at a time.

Parameters
[out]c: Character that is read.
[in]timeout_s: Timeout in seconds.
Returns
true if success, false otherwise.

Definition at line 284 of file vpSerial.cpp.

References vpException::fatalError.

Referenced by readline().

◆ readline()

std::string vpSerial::readline ( const std::string &  eol)

Reads a set of characters until a given delimiter has been received.

Parameters
[in]eol: End of line delimiter. A typical example is "\n".
Returns
A string containing the data that has been read.
Examples:
testSerialRead.cpp.

Definition at line 318 of file vpSerial.cpp.

References read().

◆ setBaudrate()

void vpSerial::setBaudrate ( const unsigned long  baudrate)

Set serial baud rate. Typical values are 9600, 19200, 38400, 57600, 115200...

See also
getBaudrate()

Definition at line 123 of file vpSerial.cpp.

◆ setBytesize()

void vpSerial::setBytesize ( const bytesize_t bytesize)

Set byte size.

See also
getBytesize()

Definition at line 132 of file vpSerial.cpp.

◆ setFlowcontrol()

void vpSerial::setFlowcontrol ( const flowcontrol_t flowcontrol)

Set flow control type.

See also
getFlowcontrol()

Definition at line 141 of file vpSerial.cpp.

◆ setParity()

void vpSerial::setParity ( const parity_t parity)

Set parity.

See also
getParity()

Definition at line 150 of file vpSerial.cpp.

◆ setPort()

void vpSerial::setPort ( const std::string &  port)

Set the serial port name. The name is a string similar to /dev/ttyUSB0, /dev/ttySO, /dev/ttyAMA0...

#include <visp3/core/vpSerial.h>
int main()
{
#ifndef WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.open();
#endif
}
See also
getPort()

Definition at line 181 of file vpSerial.cpp.

◆ setStopbits()

void vpSerial::setStopbits ( const stopbits_t stopbits)

Set number of stop bits.

See also
getStopbits()

Definition at line 159 of file vpSerial.cpp.

◆ write()

void vpSerial::write ( const std::string &  s)