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

Public Member Functions

ResultSerialPort open (StringSpan path, const SerialOpenOptions &options=SerialOpenOptions())
 Opens a serial port and applies the requested settings.
 
ResultSerialPort setSettings (const SerialSettings &settings)
 Applies settings to an already opened serial descriptor.
 
ResultSerialPort getSettings (SerialSettings &settings) const
 Reads current settings from an opened serial descriptor.
 
- Public Member Functions inherited from SC::FileDescriptor
ResultFile openForWriteToDevNull ()
 ... [UniqueHandleDeclaration2Snippet]
 
ResultFile openStdOutDuplicate ()
 Opens a duplicated file descriptor handle for reading from stdout.
 
ResultFile openStdErrDuplicate ()
 Opens a duplicated file descriptor handle for reading from stderr.
 
ResultFile openStdInDuplicate ()
 Opens a duplicated file descriptor handle for reading from stdin.
 
ResultFile open (StringSpan path, FileOpen mode)
 Opens a file descriptor handle from a file system path.
 
ResultFile read (Span< char > data, Span< char > &actuallyRead, uint64_t offset)
 Reads bytes at offset into user supplied span.
 
ResultFile read (Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
 Reads bytes at offset into user supplied span.
 
ResultFile read (Span< char > data, Span< char > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
 
ResultFile readUntilFullOrEOF (Span< char > data, Span< char > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into Span, until full or EOF is reached.
 
ResultFile read (Span< uint8_t > data, Span< uint8_t > &actuallyRead)
 Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
 
template<typename T >
ResultFile readUntilEOF (T &destination)
 Reads into a given dynamic buffer until End of File (EOF) is signaled.
 
ResultFile readUntilEOF (IGrowableBuffer &&buffer)
 Reads into a given dynamic buffer until End of File (EOF) is signaled.
 
ResultFile writeString (StringSpan data)
 Writes a string to the file descriptor.
 
ResultFile write (Span< const char > data, uint64_t offset)
 Writes bytes at offset from start of the file descriptor.
 
ResultFile write (Span< const uint8_t > data, uint64_t offset)
 Writes bytes at offset from start of the file descriptor.
 
ResultFile write (Span< const char > data)
 Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
 
ResultFile write (Span< const uint8_t > data)
 Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
 
ResultFile seek (SeekMode seekMode, int64_t offset)
 Changes the current position in the file descriptor, if seekable.
 
ResultFile currentPosition (size_t &position) const
 Gets current descriptor position (if seekable)
 
ResultFile sizeInBytes (size_t &sizeInBytes) const
 Gets total file size in bytes (if seekable)
 
ResultFile stat (FileDescriptorStat &fileStat) const
 Obtains richer metadata associated with the currently open descriptor.
 
ResultFile chmod (uint32_t mode)
 Change file permission bits for the currently open descriptor.
 
ResultFile chown (uint32_t uid, uint32_t gid)
 Change owner and group for the currently open descriptor.
 
ResultFile sync ()
 Flush file data and metadata to stable storage.
 
ResultFile syncData ()
 Flush file data to stable storage.
 
ResultFile truncate (uint64_t sizeInBytes)
 Resize the underlying file to the specified size in bytes.
 

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()

ResultSerialPort 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()

ResultSerialPort 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()

ResultSerialPort 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: