4 #include "moc_HardwareAdapter.cpp"
47 std::rethrow_exception(Exception);
74 std::rethrow_exception(Exception);
102 "A hardware adapter is in an error state. It requires to be reset in order to transition into a ready state.");
123 {
"Zero", LineEndingType::Zero },
124 {
"LF", LineEndingType::LF },
125 {
"CRLF", LineEndingType::CRLF },
126 {
"CR", LineEndingType::CR }
156 if (Pos == std::string::npos)
181 for (; NumTries > 0; --NumTries)
188 std::this_thread::sleep_for(DelayBetweenTries);
230 Write(InStream.str());
270 Write(std::string(1, Char));
285 ReadBuffer.write(String.c_str(), String.length());
293 auto DerivedParams = dynamic_Params_cast<SerialCommunicationHardwareAdapter>(
GetParams());
312 auto DataRead =
Read();
314 if (!DataRead.empty())
315 ReadBuffer.write(DataRead.c_str(), DataRead.length());
359 auto Owner = std::dynamic_pointer_cast<const QSerialCommunicationHardwareAdapter>(
GetOwner());
363 Owner->QSerialCommunicationHardwareAdapterWorkerOnly.SetCommunicationChannelOpened();
368 auto Owner = std::dynamic_pointer_cast<const QSerialCommunicationHardwareAdapter>(
GetOwner());
372 Owner->QSerialCommunicationHardwareAdapterWorkerOnly.SetCommunicationChannelClosed();
377 auto Owner = std::dynamic_pointer_cast<const QSerialCommunicationHardwareAdapter>(
GetOwner());
381 Owner->QSerialCommunicationHardwareAdapterWorkerOnly.DataRead(String);
386 QSerialCommunicationHardwareAdapterWorkerOnly(*this), Worker(nullptr), CommunicationChannelOpened(false)
418 if (!WorkerUniquePtr)
420 WorkerUniquePtr->MoveToWorkerThread(
GetID());
423 Worker = WorkerUniquePtr.release();
484 emit
WriteSig(QString::fromStdString(String));
499 std::exception_ptr e;
Implementation of DynExp hardware adapter objects.
Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connectio...
void ThrowException(std::exception_ptr Exception) const
Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped exce...
std::exception_ptr GetExceptionChild(const std::chrono::milliseconds Timeout) const override final
Returns a pointer to the exception which has caused this Object instance to fail.
virtual ~HardwareAdapterBase()=0
void SetExceptionUnsafe(std::exception_ptr Exception) const
Stores Exception in LastException.
static constexpr auto ShortTimeoutDefault
Default timeout e.g. used as a default for calls to Object::GetException().
virtual void EnsureReadyStateChild()=0
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
std::exception_ptr LastException
Stores the most recent exception caused by a hardware operation.
static constexpr auto HardwareOperationTimeout
Default timeout used to lock the mutex provided by the base class Util::ILockable to synchronize acce...
void ThrowExceptionUnsafe(std::exception_ptr Exception) const
Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped exce...
auto GetExceptionUnsafe() const
Getter for LastException.
void ResetImpl(dispatch_tag< Object >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
void ResetException() const
Sets LastException to nullptr in a thread-safe way.
virtual ~HardwareAdapterConfiguratorBase()=0
virtual ~HardwareAdapterParamsBase()=0
void SetWarning(std::string Description, int ErrorCode) const
Setter for Object::Warning. Sets the warning by a description and an error code.
ParamsConstTypeSyncPtrType GetParams(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Locks the mutex of the parameter class instance Params assigned to this Object instance and returns a...
ItemIDType GetID() const noexcept
Returns the ID of this Object instance. Thread-safe since ID is const.
void EnsureReadyState(bool IsAutomaticStartup)
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
std::exception_ptr GetException(const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
Returns a pointer to the exception which has caused this Object instance to fail.
Refer to ParamsBase::dispatch_tag.
virtual ~QSerialCommunicationHardwareAdapterConfigurator()=0
virtual ~QSerialCommunicationHardwareAdapterParams()=0
void DataRead(const std::string &String) const
Calls QSerialCommunicationHardwareAdapter::DataRead on QWorker::Owner. Does nothing if QWorker::Owner...
void Read()
Reads from the communication connection's hardware interface.
void Clear()
Clears the communication connection's buffers and state.
void Write_endl()
Writes end of the line character(s) to the communication connection's hardware interface.
void Flush()
Flushes the communication connection's buffers.
void Close()
Closes the communication connection.
void Write(const QString String)
Writes String to the communication connection's hardware interface.
void Open()
Opens the communication connection.
void Reset()
Resets the worker and the communication connection.
virtual ~QSerialCommunicationHardwareAdapterWorker()=0
void SetCommunicationChannelClosed() const noexcept
Calls QSerialCommunicationHardwareAdapter::SetCommunicationChannelClosed on QWorker::Owner....
void SetCommunicationChannelOpened() const noexcept
Calls QSerialCommunicationHardwareAdapter::SetCommunicationChannelOpened on QWorker::Owner....
virtual void InitWorker()
Tells the worker instance to perform initialization steps, e.g. by emitting a Qt signal which is rece...
void ResetSig()
Resets the worker and the communication connection.
std::exception_ptr PendingException
Stores exceptions which have occurred in the worker instance thread.
void ClearSig() const
Clears the communication connection's buffers and state.
bool IsConnectedChild() const noexcept override final
Determines the connection status of the hardware interface.
void OpenSig()
Opens the communication connection.
QSerialCommunicationHardwareAdapterWorker * Worker
Worker instance running in its own thread to handle the actual communication there.
void Write_endl_Sig() const
Writes end of the line character(s) to the communication connection's hardware interface.
std::atomic< bool > CommunicationChannelOpened
Indicates whether the worker instance has opened the communication channel.
virtual ~QSerialCommunicationHardwareAdapter()=0
void ClearChild() const override final
Clears internal buffers of the underlying hardware interface.
std::string Read() const override final
Reads a string from the underlying hardware interface.
void FlushSig() const
Flushes the communication connection's buffers.
void ThrowPendingException() const
If PendingException is not nullptr, sets it to nullptr and throws the exception calling Util::Forward...
bool IsReadyChild() const override final
Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) a...
void Write(const std::string &String) const override final
Writes a string to the underlying hardware interface.
QSerialCommunicationHardwareAdapter(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a hardware adapter instance.
void Write_endl() const override final
Writes end of the line characters to the underlying hardware interface.
void WriteSig(const QString String) const
Writes String to the communication connection's hardware interface.
void ResetImpl(dispatch_tag< SerialCommunicationHardwareAdapter >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
void EnsureReadyStateChild() override final
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
void CloseSig()
Closes the communication connection.
void DataRead(const std::string &String) const
Passes data to the hardware adapter which has been received by the worker instance.
void ReadSig() const
Reads from the communication connection's hardware interface.
void FlushChild() const override final
Flushes the underlying hardware interface.
virtual QWorkerPtrType MakeWorker()=0
Abstract factory function for a worker instance derived from QSerialCommunicationHardwareAdapterWorke...
virtual ~SerialCommunicationHardwareAdapterConfigurator()=0
virtual ~SerialCommunicationHardwareAdapterParams()=0
static Util::TextValueListType< LineEndingType > AvlblLineEndingsStrList()
Assigns labels to the entries of LineEndingType.
Defines a hardware adapter for serial communication. Logical const-ness: see declaration of class Dyn...
void Init()
Initializes the instance at construction or in case Object::Reset() is called.
constexpr static unsigned int GetLineEndingLength(SerialCommunicationHardwareAdapterParams::LineEndingType LineEnding) noexcept
Determines the amount of characters required to express a line ending type defined by SerialCommunica...
virtual void Write_endl() const =0
Writes end of the line characters to the underlying hardware interface.
virtual void FlushChild() const
Flushes the underlying hardware interface.
virtual void Write(const std::string &String) const =0
Writes a string to the underlying hardware interface.
bool CheckOverflow() const
Checks whether the length of the read buffer (ReadBuffer) exceeds the maximal allowed length given by...
std::string LineEndingString
String corresponding to LineEnding. Refer to LineEndingToChar()
virtual void ClearChild() const
Clears internal buffers of the underlying hardware interface.
virtual ~SerialCommunicationHardwareAdapter()=0
constexpr static auto GetMaxBufferSize() noexcept
Defines the maximal size of the hardware adapter's (read) buffer.
void Flush() const
Flushes the underlying hardware interface and calls ReadIntoBuffer().
constexpr static std::array< char, 2 > LineEndingToChar(SerialCommunicationHardwareAdapterParams::LineEndingType LineEnding) noexcept
Converts SerialCommunicationHardwareAdapterParams::LineEndingType to two characters being used as the...
const SerialCommunicationHardwareAdapter & operator<<(endl) const
Writes a new line calling Write_endl().
void InsertIntoBuffer(const std::string &String) const
This method can be called from derived classes to manually insert data into the read buffer....
std::string ReadLine() const
Calls ReadIntoBuffer() before it extracts the first line from the read buffer.
std::string ReadAll() const
Calls ReadIntoBuffer() before it extracts the entire content from the read buffer.
virtual std::string Read() const =0
Reads a string from the underlying hardware interface.
std::atomic< SerialCommunicationHardwareAdapterParams::LineEndingType > LineEnding
Copy of SerialCommunicationHardwareAdapterParams::LineEnding to avoid locking the corresponding Seria...
void ResetImpl(dispatch_tag< HardwareAdapterBase >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
void ReadIntoBuffer() const
Calls Read() to retrieve data from the underlying hardware interface. Writes the retrieved data to th...
std::string WaitForLine(unsigned int NumTries=10, std::chrono::milliseconds DelayBetweenTries=std::chrono::milliseconds(10)) const
Calls ReadLine() for NumTries times until a line is received. Makes the calling thread sleep for Dela...
void Clear() const
Clears the content and state flags of the read buffer (ReadBuffer) as well as possible internal buffe...
const SerialCommunicationHardwareAdapter & operator>>(std::stringstream &OutStream) const
Writes the read buffer's content into OutStream calling ReadAll().
std::stringstream ReadBuffer
Buffer storing data read from the underlying physical hardware.
void ClearReadBuffer() const
Clears the content and state flags of the read buffer (ReadBuffer)
SerialCommunicationHardwareAdapter(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a hardware adapter instance.
Type denoting the end of a line when piped to operator<<(endl)
void Log(const std::string &Message, const ErrorType Type=ErrorType::Info, const size_t Line=0, const std::string &Function="", const std::string &File="", const int ErrorCode=0, const std::stacktrace &Trace={}) noexcept
Logs an event from information specified manually.
DynExp exceptions are derived from this class. It contains basic information about the cause of the e...
LockType AcquireLock(const std::chrono::milliseconds Timeout=DefaultTimeout) const
Locks the internal mutex. Blocks until the mutex is locked or until the timeout duration is exceeded.
An invalid argument like a null pointer has been passed to a function.
An operation cannot be performed currently since the related object is in an invalid state like an er...
auto GetOwner() const noexcept
Returns this worker instance's owner.
OwnerPtrType Owner
Pointer to the hardware adapter owning this instance.
@ None
Unspecified/arbitrary waveform.
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
void ForwardException(std::exception_ptr e)
Wraps the exception passed to the function in a ForwardedException and throws the ForwardedException....
EventLogger & EventLog()
This function holds a static EventLogger instance and returns a reference to it. DynExp uses only one...
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Accumulates include statements to provide a precompiled header.