Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::SerialDescriptor Struct Reference

Native serial port descriptor with configuration support. More...

#include <SerialPort.h>

Inheritance diagram for SC::SerialDescriptor:
SC::FileDescriptor SC::UniqueHandle< detail::FileDescriptorDefinition >

Public Member Functions

Result open (StringSpan path, const SerialOpenOptions &options=SerialOpenOptions())
 Opens a serial port and applies the requested settings.
 
Result setSettings (const SerialSettings &settings)
 Applies settings to an already opened serial descriptor.
 
Result getSettings (SerialSettings &settings) const
 Reads current settings from an opened serial descriptor.
 
- Public Member Functions inherited from SC::FileDescriptor
Result openForWriteToDevNull ()
 ... [UniqueHandleDeclaration2Snippet]
 
Result openStdOutDuplicate ()
 Opens a duplicated file descriptor handle for reading from stdout.
 
Result openStdErrDuplicate ()
 Opens a duplicated file descriptor handle for reading from stderr.
 
Result openStdInDuplicate ()
 Opens a duplicated file descriptor handle for reading from stdin.
 
Result open (StringSpan path, FileOpen mode)
 Opens a file descriptor handle from a file system path.
 
Result read (Span< char > data, Span< char > &actuallyRead, uint64_t offset)
 Reads bytes at offset into user supplied span.
 
Result read (Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
 Reads bytes at offset into user supplied span.
 
Result read (Span< char > data, Span< char > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
 
Result readUntilFullOrEOF (Span< char > data, Span< char > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into Span, until full or EOF is reached.
 
Result read (Span< uint8_t > data, Span< uint8_t > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
 
template<typename T >
Result readUntilEOF (T &destination)
 Reads into a given dynamic buffer until End of File (EOF) is signaled.
 
Result readUntilEOF (IGrowableBuffer &&buffer)
 Reads into a given dynamic buffer until End of File (EOF) is signaled.
 
Result writeString (StringSpan data)
 Writes a string to the file descriptor.
 
Result write (Span< const char > data, uint64_t offset)
 Writes bytes at offset from start of the file descriptor.
 
Result write (Span< const uint8_t > data, uint64_t offset)
 Writes bytes at offset from start of the file descriptor.
 
Result write (Span< const char > data)
 Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
 
Result write (Span< const uint8_t > data)
 Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
 
Result seek (SeekMode seekMode, int64_t offset)
 Changes the current position in the file descriptor, if seekable.
 
Result currentPosition (size_t &position) const
 Gets current descriptor position (if seekable)
 
Result sizeInBytes (size_t &sizeInBytes) const
 Gets total file size in bytes (if seekable)
 
 UniqueHandle ()=default
 
 UniqueHandle (const UniqueHandle &v)=delete
 
 UniqueHandle (UniqueHandle &&v)
 
 UniqueHandle (const Handle &externalHandle)
 

Additional Inherited Members

- Public Types inherited from SC::FileDescriptor
enum  SeekMode {
  SeekStart ,
  SeekEnd ,
  SeekCurrent
}
 How the offset to FileDescriptor::seek is defined. More...
 

Detailed Description

Native serial port descriptor with configuration support.

SerialDescriptor serial;
SerialOpenOptions options;
options.settings.baudRate = 115200;
options.settings.dataBits = SerialSettings::DataBits::Bits8;
options.settings.parity = SerialSettings::Parity::None;
options.settings.stopBits = SerialSettings::StopBits::One;
options.settings.flowControl = SerialSettings::FlowControl::None;
#if SC_PLATFORM_WINDOWS
StringView serialPath = "COM3";
#else
StringView serialPath = "/dev/ttyUSB0";
#endif
SC_TRY(serial.open(serialPath, options));
SerialSettings current;
SC_TRY(serial.getSettings(current));
current.baudRate = 57600;
SC_TRY(serial.setSettings(current));
const char tx[] = {'P', 'I', 'N', 'G'};
SC_TRY(serial.write({tx, sizeof(tx)}));
char rxBuffer[64] = {};
Span<char> rxData;
SC_TRY(serial.read({rxBuffer, sizeof(rxBuffer)}, rxData));
// rxData is a slice of rxBuffer with the actual received bytes

Member Function Documentation

◆ getSettings()

Result SC::SerialDescriptor::getSettings ( SerialSettings & settings) const

Reads current settings from an opened serial descriptor.

Parameters
settingsOutput settings.
Returns
Valid Result if settings have been read.

◆ open()

Result SC::SerialDescriptor::open ( StringSpan path,
const SerialOpenOptions & options = SerialOpenOptions() )

Opens a serial port and applies the requested settings.

Parameters
pathSerial device path (/dev/tty* on Posix, COM* on Windows)
optionsOpen and configuration options.
Returns
Valid Result if open/configuration succeeded.

◆ setSettings()

Result SC::SerialDescriptor::setSettings ( const SerialSettings & settings)

Applies settings to an already opened serial descriptor.

Parameters
settingsSettings to apply.
Returns
Valid Result if settings have been applied.

The documentation for this struct was generated from the following file: