Class Serial

Class Documentation

class Serial

Enables acces to serial devices.

Open a serial device, change baudrates, read from and write to the device. Status-information after calling the functions get be

Public Functions

Serial(const char *dev_name, const SerialFlags &flags)

Opens the device (if possible) with the given flags.

Parameters:

flags – can be values of the following values ORed together

  • CS5, CS6, CS7, or CS8 databits

  • CSTOPB set two stop bits, rather than one.

  • CREAD enable receiver.

  • PARENB enable parity generation on output and parity checking for input.

  • PARODD parity for input and output is odd.

  • HUPCL lower modem control lines after last process closes the device (hang up).

  • CLOCAL ignore modem control lines

  • B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B921600 Baudrates

  • CRTSCTS flow control.

Serial(const char *dev_name, SerialFlags::BaudRate baud_rate, const SerialFlags &flags)

This is an overloaded constructor, provided for convenience. buad_rate overwrites the baud rate set in flags.

~Serial()

Restore old serial settings and close device

int changeBaudrate(SerialFlags::BaudRate speed)

speed is one of the followind values :

  • B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B921600 Returns 0 on success. Returns -status on failure.

int clearReceiveBuffer()

Clears the serial port’s receive buffer.

int clearSendBuffer()

Clears the serial port’s send buffer.

inline bool open(const SerialFlags &flags)

Opens the serial interface with the given flags

bool open()

Opens the serial interface.

bool isOpen() const

Returns true if the serial interface is opened. false otherwhise.

void close()

Close the serial interface.

ssize_t write(const void *data, ssize_t size)

Write data to serial out.

ssize_t read(void *data, ssize_t size, unsigned long time = 100, bool return_on_less_data = true)

Read data from device. This function waits until

Parameters:
  • time – us passed or the respected number of bytes are received via serial line. if (

  • return_on_less_data – is true (default value), the number of bytes that have been receives are returned and the data is stored in

  • data. – If the parameter is false, data is only read from serial line, if at least

  • size – bytes are available.

inline int status() const

All routines return a negavtiv number on error. Then the global errno is stored into a private variable. Use this funtion to ask for this value. Especially the constructor cannot return an error-value. Check the succesful opening of the device by calling this function. It returns 0, if no error occured.

std::string statusText() const
inline const char *deviceName() const
inline int fileDescriptor()

Return the file descriptor of the serial class