DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
HardwareAdapterSerialPort.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
9#pragma once
10
11#include <QtSerialPort/QSerialPort>
12#include <QtSerialPort/QSerialPortInfo>
13
14#include "stdafx.h"
15#include "HardwareAdapter.h"
16
17namespace DynExp
18{
19 class HardwareAdapterSerialPort;
20
26 {
27 public:
34 SerialPortException(std::string Description, const int ErrorCode,
35 const std::source_location Location = std::source_location::current()) noexcept
36 : SerialCommunicationException(std::move(Description), ErrorCode, Location)
37 {}
38 };
39
44 {
45 public:
51
53
54 virtual const char* GetParamClassTag() const noexcept override { return "HardwareAdapterSerialPortParams"; }
55
56 Param<TextList> PortName = { *this, {}, "PortName", "Port",
57 "Name of the serial port to be assigned to this hardware adapter" };
58 Param<QSerialPort::BaudRate> BaudRate = { *this, Util::QtEnumToTextValueList<QSerialPort::BaudRate>(0, 0, 4), "BaudRate", "Baud rate",
59 "Data rate in bits per second", true, QSerialPort::Baud9600 };
60 Param<QSerialPort::DataBits> DataBits = { *this, Util::QtEnumToTextValueList<QSerialPort::DataBits>(0, 0, 4), "DataBits", "Data bits",
61 "Number of data bits in each character", true, QSerialPort::Data8 };
62 Param<QSerialPort::StopBits> StopBits = { *this, Util::QtEnumToTextValueList<QSerialPort::StopBits>(0, 0, 0, 4), "StopBits", "Stop bits",
63 "Number of stop bits after each character", true, QSerialPort::OneStop };
64 Param<QSerialPort::Parity> Parity = { *this, Util::QtEnumToTextValueList<QSerialPort::Parity>(0, 0, 0, 6), "Parity", "Parity",
65 "Error detection method", true, QSerialPort::NoParity };
66
67 private:
72 void ConfigureParamsImpl(dispatch_tag<QSerialCommunicationHardwareAdapterParams>) override final;
73
75 };
76
81 {
82 public:
85
88
89 private:
90 virtual DynExp::ParamsBasePtrType MakeParams(ItemIDType ID, const DynExpCore& Core) const override { return DynExp::MakeParams<HardwareAdapterSerialPortConfigurator>(ID, Core); }
91 };
92
97 {
98 Q_OBJECT
99
100 public:
103
104 public slots:
114 void Init(QString PortName, QSerialPort::BaudRate BaudRate, QSerialPort::DataBits DataBits,
115 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity);
116
117 private slots:
118 void OnDataAvailable();
119
120 private:
127 bool CheckError(const std::source_location Location = std::source_location::current()) const;
128
129 virtual void OpenChild() override;
130 virtual void CloseChild() override;
131 virtual void ResetChild() override;
132 virtual void ClearChild() override;
133 virtual void FlushChild() override;
134 virtual void ReadChild() override;
135 virtual void WriteChild(const QString& String) override;
136 virtual void Write_endl_Child() override;
137
138 QSerialPort Port;
139 };
140
146 {
147 Q_OBJECT
148
149 public:
152
153 constexpr static auto Name() noexcept { return "Serial Port"; }
154
159 static auto Enumerate();
160
162 virtual ~HardwareAdapterSerialPort() = default;
163
164 virtual std::string GetName() const override { return Name(); }
165
166 private:
167 QWorkerPtrType MakeWorker() override;
168 void InitWorker() override { Init(); }
169
175
176 void Init();
177
178 void ResetImpl(dispatch_tag<QSerialCommunicationHardwareAdapter>) override final;
180
181 signals:
187
190 void InitSig(QString PortName, QSerialPort::BaudRate BaudRate, QSerialPort::DataBits DataBits,
191 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity);
193 };
194}
Implementation of DynExp hardware adapter objects.
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
Configurator class for HardwareAdapterSerialPort.
virtual DynExp::ParamsBasePtrType MakeParams(ItemIDType ID, const DynExpCore &Core) const override
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Parameter class for HardwareAdapterSerialPort.
Param< QSerialPort::StopBits > StopBits
Amount of stop bits.
Param< QSerialPort::DataBits > DataBits
Amount of data bits.
HardwareAdapterSerialPortParams(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a HardwareAdapterSerialPort instance.
virtual ~HardwareAdapterSerialPortParams()=default
void ConfigureParamsImpl(dispatch_tag< QSerialCommunicationHardwareAdapterParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Param< QSerialPort::BaudRate > BaudRate
Baud rate.
virtual void ConfigureParamsImpl(dispatch_tag< HardwareAdapterSerialPortParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Param< QSerialPort::Parity > Parity
Parity setting.
Qt worker for serial/COM port communication in a separate thread.
bool CheckError(const std::source_location Location=std::source_location::current()) const
Checks whether Port is in an error state. If this is the case, sets an exception using QSerialCommuni...
virtual void ResetChild() override
Resets the worker and the communication connection.
virtual void Write_endl_Child() override
Writes end of the line character(s) to the communication connection's hardware interface.
virtual void FlushChild() override
Flushes the communication connection's buffers.
virtual void ClearChild() override
Clears the communication connection's buffers and state.
QSerialPort Port
COM port for serial communication.
virtual void CloseChild() override
Closes the communication connection.
void Init(QString PortName, QSerialPort::BaudRate BaudRate, QSerialPort::DataBits DataBits, QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
Initializes HardwareAdapterSerialPortWorker::Port for serial communication with the given settings.
virtual void ReadChild() override
Reads from the communication connection's hardware interface.
virtual void WriteChild(const QString &String) override
Writes String to the communication connection's hardware interface.
virtual void OpenChild() override
Opens the communication connection.
void OnDataAvailable()
Qt slot called when Port has received data which can be read.
Implements a hardware adapter to communicate with text-based commands over COM ports.
void Init()
Initializes the instance at construction or in case Object::Reset() is called.
QWorkerPtrType MakeWorker() override
Abstract factory function for a worker instance derived from QSerialCommunicationHardwareAdapterWorke...
static auto Enumerate()
Enumerates all serial ports available on the system.
void RegisterQTypesAsMetaTypes()
Registers QSerialPort enumerations as Qt meta types using qRegisterMetaType() (refer to Qt documentat...
void InitSig(QString PortName, QSerialPort::BaudRate BaudRate, QSerialPort::DataBits DataBits, QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
Initializes HardwareAdapterSerialPortWorker::Port for serial communication with the given settings.
static constexpr auto Name() noexcept
Every derived class has to redefine this function.
void InitWorker() override
Tells the worker instance to perform initialization steps, e.g. by emitting a Qt signal which is rece...
virtual ~HardwareAdapterSerialPort()=default
virtual std::string GetName() const override
Returns the name of this Object type.
void ResetImpl(dispatch_tag< QSerialCommunicationHardwareAdapter >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual void ResetImpl(dispatch_tag< HardwareAdapterSerialPort >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition Object.h:2303
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition Object.h:349
Configurator class for QSerialCommunicationHardwareAdapter.
Parameter class for QSerialCommunicationHardwareAdapter.
Qt worker which performs actual serial communication hardware operations in a separate thread in orde...
SerialCommunicationHardwareAdapter is based on a Qt communication object (wrapped by QSerialCommunica...
std::unique_ptr< QSerialCommunicationHardwareAdapterWorker > QWorkerPtrType
Pointer-type owning the related QSerialCommunicationHardwareAdapterWorker instance.
Defines an exception caused by a serial communication operation of a hardware adapter.
Defines an exception caused by a hardware operation on a COM port (e.g. reading/writing data to a COM...
SerialPortException(std::string Description, const int ErrorCode, const std::source_location Location=std::source_location::current()) noexcept
Constructs a SerialPortException instance.
const int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes
Definition Exception.h:106
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.
Definition Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
Accumulates include statements to provide a precompiled header.