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:
49 enum BaudRateType : std::underlying_type_t<QSerialPort::BaudRate> {
50 Baud1200 = QSerialPort::BaudRate::Baud1200,
51 Baud2400 = QSerialPort::BaudRate::Baud2400,
52 Baud4800 = QSerialPort::BaudRate::Baud4800,
53 Baud9600 = QSerialPort::BaudRate::Baud9600,
54 Baud19200 = QSerialPort::BaudRate::Baud19200,
55 Baud38400 = QSerialPort::BaudRate::Baud38400,
56 Baud57600 = QSerialPort::BaudRate::Baud57600,
57 Baud115200 = QSerialPort::BaudRate::Baud115200,
58 Baud230400 = 230400,
59 Baud460800 = 460800,
60 Baud921600 = 921600
61 };
62
68
74
76
77 virtual const char* GetParamClassTag() const noexcept override { return "HardwareAdapterSerialPortParams"; }
78
79 Param<TextList> PortName = { *this, {}, "PortName", "Port",
80 "Name of the serial port to be assigned to this hardware adapter" };
81 Param<BaudRateType> BaudRate = { *this, BaudRateTypeStrList(), "BaudRate", "Baud rate",
82 "Data rate in bits per second", true, BaudRateType::Baud9600 };
83 Param<QSerialPort::DataBits> DataBits = { *this, Util::QtEnumToTextValueList<QSerialPort::DataBits>(0, 0, 4), "DataBits", "Data bits",
84 "Number of data bits in each character", true, QSerialPort::Data8 };
85 Param<QSerialPort::StopBits> StopBits = { *this, Util::QtEnumToTextValueList<QSerialPort::StopBits>(0, 0, 0, 4), "StopBits", "Stop bits",
86 "Number of stop bits after each character", true, QSerialPort::OneStop };
87 Param<QSerialPort::Parity> Parity = { *this, Util::QtEnumToTextValueList<QSerialPort::Parity>(0, 0, 0, 6), "Parity", "Parity",
88 "Error detection method", true, QSerialPort::NoParity };
89
90 private:
95 void ConfigureParamsImpl(dispatch_tag<QSerialCommunicationHardwareAdapterParams>) override final;
96
98 };
99
104 {
105 public:
108
111
112 private:
113 virtual DynExp::ParamsBasePtrType MakeParams(ItemIDType ID, const DynExpCore& Core) const override { return DynExp::MakeParams<HardwareAdapterSerialPortConfigurator>(ID, Core); }
114 };
115
120 {
121 Q_OBJECT
122
123 public:
126
127 public slots:
137 void Init(QString PortName, HardwareAdapterSerialPortParams::BaudRateType BaudRate, QSerialPort::DataBits DataBits,
138 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity);
139
140 private slots:
141 void OnDataAvailable();
142
143 private:
150 bool CheckError(const std::source_location Location = std::source_location::current()) const;
151
152 virtual void OpenChild() override;
153 virtual void CloseChild() override;
154 virtual void ResetChild() override;
155 virtual void ClearChild() override;
156 virtual void FlushChild() override;
157 virtual void ReadChild() override;
158 virtual void WriteChild(const QString& String) override;
159 virtual void Write_endl_Child() override;
160
161 QSerialPort Port;
162 };
163
169 {
170 Q_OBJECT
171
172 public:
175
176 constexpr static auto Name() noexcept { return "Serial Port"; }
177
182 static auto Enumerate();
183
185 virtual ~HardwareAdapterSerialPort() = default;
186
187 virtual std::string GetName() const override { return Name(); }
188
189 private:
190 QWorkerPtrType MakeWorker() override;
191 void InitWorker() override { Init(); }
192
198
199 void Init();
200
201 void ResetImpl(dispatch_tag<QSerialCommunicationHardwareAdapter>) override final;
203
204 signals:
210
213 void InitSig(QString PortName, HardwareAdapterSerialPortParams::BaudRateType BaudRate, QSerialPort::DataBits DataBits,
214 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity);
216 };
217}
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.
static Util::TextValueListType< BaudRateType > BaudRateTypeStrList()
Assigns labels to the entries of BaudRateType.
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.
BaudRateType
Extends QSerialPort::BaudRate enumeration with further Baud rates.
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...
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.
void Init(QString PortName, HardwareAdapterSerialPortParams::BaudRateType BaudRate, QSerialPort::DataBits DataBits, QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
Initializes HardwareAdapterSerialPortWorker::Port for serial communication with the given settings.
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.
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, HardwareAdapterSerialPortParams::BaudRateType 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:107
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.
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition QtUtil.h:37
Accumulates include statements to provide a precompiled header.