Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
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>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
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 106 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 87 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 97 of file vpSerial.h.

Constructor & Destructor Documentation

◆ vpSerial()

BEGIN_VISP_NAMESPACE 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>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main()
{
#ifndef WIN32
vpSerial serial("/dev/ttyUSB0");
#endif
}

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

#include <visp3/core/vpSerial.h>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main()
{
#ifndef WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.open();
#endif
}
void open()
Definition: vpSerial.cpp:240
void setPort(const std::string &port)
Definition: vpSerial.cpp:169
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 flow control used. Default is no flow control.

Definition at line 104 of file vpSerial.cpp.

References open().

◆ ~vpSerial()

vpSerial::~vpSerial ( )
virtual

Destructor that closes the serial port.

Definition at line 116 of file vpSerial.cpp.

References close().

Member Function Documentation

◆ available()

int vpSerial::available ( )

Return the number of characters in the buffer.

Definition at line 174 of file vpSerial.cpp.

References vpException::fatalError.

◆ close()

void vpSerial::close ( )

Closes the serial port.

See also
open()

Definition at line 192 of file vpSerial.cpp.

References vpException::fatalError.

Referenced by ~vpSerial().

◆ getBaudrate()

unsigned long vpSerial::getBaudrate ( )
inline

Return the baud rate; 9600, 115200...

See also
setBaudrate()

Definition at line 125 of file vpSerial.h.

◆ getBytesize()

bytesize_t vpSerial::getBytesize ( )
inline

Return the byte size.

See also
setBytesize()

Definition at line 131 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 143 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 149 of file vpSerial.h.

◆ getStopbits()

stopbits_t vpSerial::getStopbits ( )
inline

Return number of stop bits used.

See also
setStopbits()

Definition at line 155 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>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main()
{
#ifndef _WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.setBaudrate(9600);
serial.open();
#endif
}
@ flowcontrol_none
No flow control.
Definition: vpSerial.h:108
void setParity(const parity_t &parity)
Definition: vpSerial.cpp:140
void setStopbits(const stopbits_t &stopbits)
Definition: vpSerial.cpp:146
@ parity_none
No parity check.
Definition: vpSerial.h:89
@ stopbits_one
1 stop bit is used
Definition: vpSerial.h:99
void setFlowcontrol(const flowcontrol_t &flowcontrol)
Definition: vpSerial.cpp:134
void setBytesize(const bytesize_t &bytesize)
Definition: vpSerial.cpp:128
@ eightbits
Data is encoded with 8 bits.
Definition: vpSerial.h:81
void setBaudrate(const unsigned long baudrate)
Definition: vpSerial.cpp:122

Definition at line 240 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 275 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 309 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 122 of file vpSerial.cpp.

◆ setBytesize()

void vpSerial::setBytesize ( const bytesize_t bytesize)

Set byte size.

See also
getBytesize()

Definition at line 128 of file vpSerial.cpp.

◆ setFlowcontrol()

void vpSerial::setFlowcontrol ( const flowcontrol_t flowcontrol)

Set flow control type.

See also
getFlowcontrol()

Definition at line 134 of file vpSerial.cpp.

◆ setParity()

void vpSerial::setParity ( const parity_t parity)

Set parity.

See also
getParity()

Definition at line 140 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>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main()
{
#ifndef WIN32
vpSerial serial;
serial.setPort("/dev/ttyUSB0");
serial.open();
#endif
}
See also
getPort()

Definition at line 169 of file vpSerial.cpp.

◆ setStopbits()

void vpSerial::setStopbits ( const stopbits_t stopbits)

Set number of stop bits.

See also
getStopbits()

Definition at line 146 of file vpSerial.cpp.

◆ write()

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

Writes a string to the serial port.

Parameters
[in]s: The data to write.
Examples
mbot-apriltag-2D-half-vs.cpp, mbot-apriltag-ibvs.cpp, mbot-apriltag-pbvs.cpp, and testSerialWrite.cpp.

Definition at line 332 of file vpSerial.cpp.

References vpException::fatalError.