NDEVR
API Documentation
SerialPortConnection.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/Connection.h>
4#include <QSerialPortInfo.h>
5namespace NDEVR
6{
19
25 class NDEVR_SERIAL_PORT_API SerialPortConnection : public ConnectionBackend
26 {
27 public:
40 virtual uint04 peek(char* s, uint04 size) const override;
47 virtual uint04 rx(char* s, uint04 size = Constant<uint04>::Max) override;
52 void txInThread(const StringView& command);
57 virtual void tx(const StringView& command) override;
63 virtual bool open(const ConnectionInfo& request) override;
67 virtual void close() override;
72 virtual bool isOpen() const override;
77 virtual uint04 bytesAvailable() const override;
82 virtual TranslatedString lastError() const override;
90 bool setSerialPort(QSerialPort* serial_port, ConnectionOpenType connection_type = ConnectionOpenType::e_read_write, LogPtr log = LogPtr());
96 virtual bool waitForReadyRead(const TimeSpan& span) override;
102 void handleSerialPortError(int error, LogPtr log);
108 static void ToString(const QSerialPort& value, String& string);
114 static void FromString(const String& string, QSerialPort& value);
120 static ConnectionInfo CreateInfo(const QSerialPortInfo& value);
126 static ConnectionInfo CreateInfo(const QSerialPort& value);
131 String threadID() const override;
132 protected:
133 QSerialPort* m_serial_port = nullptr;
135 };
136
141 class NDEVR_SERIAL_PORT_API SerialPortConnectionFactory : public ConnectionFactory
142 {
143 public:
149 virtual bool handlesType(const StringView& request) const override;
155 virtual ConnectionBackend* createConnection(const ConnectionInfo& request) override;
172 };
173}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Provides the unseen backend logic for setting up, tearing down, and sending data over a specific type...
Definition Connection.h:169
ConnectionBackend(LogPtr log)
Constructs a ConnectionBackend with the given log.
Stores factories that can be registered for creating Connections of various types by instantiating a ...
Definition Connection.h:250
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
A ConnectionFactory that enables the creation of a SerialPortConnection for interfacing with a QSeria...
virtual Buffer< QSerialPortInfo > availablePortInfo()
Returns information about all available serial ports on the system.
virtual ConnectionBackend * createConnection(const ConnectionInfo &request) override
Creates a new SerialPortConnection backend for the given connection information.
virtual Buffer< ConnectionCategory > availableConnectionCategories() override
Returns the connection categories supported by this factory.
virtual Buffer< ConnectionInfo > availableConnections(const ConnectionSearchParameters &params) override
Returns connection information for all available serial port connections.
virtual bool handlesType(const StringView &request) const override
Checks whether this factory handles the given connection type string.
virtual uint04 bytesAvailable() const override
Returns the number of bytes available for reading from the serial port.
QSerialPort * m_serial_port
The underlying Qt serial port instance.
SerialPortConnection(LogPtr log)
Constructs a SerialPortConnection with the given log.
static void ToString(const QSerialPort &value, String &string)
Serializes a QSerialPort configuration to a string representation.
virtual bool open(const ConnectionInfo &request) override
Opens the serial port connection using the given connection information.
static ConnectionInfo CreateInfo(const QSerialPortInfo &value)
Creates a ConnectionInfo from a QSerialPortInfo descriptor.
virtual TranslatedString lastError() const override
Returns the last error message from the serial port.
virtual void close() override
Closes the serial port connection.
TranslatedString m_last_error
The most recent error message from the serial port.
virtual bool isOpen() const override
Checks whether the serial port is currently open.
void handleSerialPortError(int error, LogPtr log)
Handles a QSerialPort error code and logs the corresponding message.
virtual uint04 rx(char *s, uint04 size=Constant< uint04 >::Max) override
Receives data from the serial port.
static void FromString(const String &string, QSerialPort &value)
Deserializes a string representation into a QSerialPort configuration.
virtual bool waitForReadyRead(const TimeSpan &span) override
Blocks until data is ready to read or the timeout expires.
static ConnectionInfo CreateInfo(const QSerialPort &value)
Creates a ConnectionInfo from an existing QSerialPort.
bool setSerialPort(QSerialPort *serial_port, ConnectionOpenType connection_type=ConnectionOpenType::e_read_write, LogPtr log=LogPtr())
Sets the underlying QSerialPort and configures it with the given open type.
String threadID() const override
Returns a string identifying the thread this connection operates on.
void txInThread(const StringView &command)
Transmits a command over the serial port from within the connection thread.
virtual void tx(const StringView &command) override
Transmits a command over the serial port.
virtual uint04 peek(char *s, uint04 size) const override
Reads data from the serial port without consuming it.
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
ConnectionOpenType
Describes how to open a connection.
Definition Connection.h:64
@ e_read_write
Open for both reading and writing.
Definition Connection.h:68
A structure designed to store information about a specific Connection.
Definition Connection.h:90
Describes settings for performing a Connection search.
Definition Connection.h:149
A structure containing information needed to establish a SerialPortConnection.
uint04 baud_rate
The communication baud rate.
uint04 data_bits
The number of data bits per frame.
uint04 flow_control
The flow control mode.
uint04 stop_bits
The number of stop bits per frame.
uint04 parity
The parity checking mode.
String port_name
The system name of the serial port (e.g., "COM3").