DynExp
Highly flexible laboratory automation for dynamically changing experiments.
HardwareAdapterEthernet.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
9 #pragma once
10 
11 #include <QTcpSocket>
12 
13 #include "stdafx.h"
14 #include "HardwareAdapter.h"
15 
16 namespace DynExp
17 {
18  class HardwareAdapterTcpSocket;
19 
25  {
26  public:
33  NetworkException(std::string Description, const int ErrorCode,
34  const std::source_location Location = std::source_location::current()) noexcept
35  : SerialCommunicationException(std::move(Description), ErrorCode, Location)
36  {}
37  };
38 
43  {
44  public:
50  : ServerName{ Owner, "ServerName", "Server name",
51  "IP address or host name of the server to connect to", true, "localhost" },
52  Port{ Owner, "Port", "Server port",
53  "Port of the remote server to connect to", true, 1000, 1, std::numeric_limits<uint16_t>::max(), 1, 0 }
54  {}
55 
60  std::string MakeAddress() const { return ServerName.Get() + ":" + Util::ToStr(Port.Get()); }
61 
64  };
65 
70  {
71  public:
78 
79  virtual ~HardwareAdapterTcpSocketParams() = default;
80 
81  virtual const char* GetParamClassTag() const noexcept override { return "HardwareAdapterTcpSocketParams"; }
82 
84 
85  private:
88  };
89 
94  {
95  public:
98 
101 
102  private:
103  virtual ParamsBasePtrType MakeParams(ItemIDType ID, const DynExpCore& Core) const override { return DynExp::MakeParams<HardwareAdapterTcpSocketConfigurator>(ID, Core); }
104  };
105 
110  {
111  Q_OBJECT
112 
113  public:
116 
117  public slots:
124  void Init(QString ServerName, quint16 Port);
125 
126  private slots:
131  void OnConnected();
132 
137  void OnDisconnected();
138 
145  void OnErrorOccurred(QAbstractSocket::SocketError SocketError);
146 
151  void OnDataAvailable();
152 
153  private:
158  bool IsOpen() const noexcept;
159 
164  bool IsOpening() const noexcept;
165 
166  virtual void OpenChild() override;
167  virtual void CloseChild() override;
168  virtual void ResetChild() override;
169  virtual void ClearChild() override;
170  virtual void FlushChild() override;
171  virtual void ReadChild() override;
172  virtual void WriteChild(const QString& String) override;
173  virtual void Write_endl_Child() override;
174 
175  QString ServerName;
176  quint16 Port;
177 
178  QTcpSocket Socket;
179  };
180 
186  {
187  Q_OBJECT
188 
189  public:
192 
193  constexpr static auto Name() noexcept { return "TCP Socket"; }
194 
195  HardwareAdapterTcpSocket(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params);
196  virtual ~HardwareAdapterTcpSocket() = default;
197 
198  virtual std::string GetName() const override { return Name(); }
199 
200  private:
201  QWorkerPtrType MakeWorker() override;
202  void InitWorker() override { Init(); }
203 
210  void Init();
211 
212  void ResetImpl(dispatch_tag<QSerialCommunicationHardwareAdapter>) override final;
214 
215  signals:
221 
224  void InitSig(QString ServerName, quint16 Port);
226  };
227 }
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
Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connectio...
Configurator class for HardwareAdapterBase.
Parameter class for HardwareAdapterBase.
Configurator class for HardwareAdapterTcpSocket.
virtual 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 HardwareAdapterTcpSocket.
void ConfigureParamsImpl(dispatch_tag< QSerialCommunicationHardwareAdapterParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
HardwareAdapterTcpSocketParams(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a HardwareAdapterTcpSocket instance.
NetworkParamsExtension NetworkParams
Bundles several parameters to describe a network connection. Use in parameter classes.
virtual void ConfigureParamsImpl(dispatch_tag< HardwareAdapterTcpSocketParams >)
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 ~HardwareAdapterTcpSocketParams()=default
Qt worker for network communication in a separate thread.
virtual void ResetChild() override
Resets the worker and the communication connection.
bool IsOpen() const noexcept
Checks whether Socket is connected.
virtual void Write_endl_Child() override
Writes end of the line character(s) to the communication connection's hardware interface.
virtual void ClearChild() override
Clears the communication connection's buffers and state.
void OnDisconnected()
Qt slot called when Socket has been disconnected. Calls QSerialCommunicationHardwareAdapterWorker::Se...
void Init(QString ServerName, quint16 Port)
Initializes HardwareAdapterTcpSocketWorker::Socket for TCP communication with the given settings.
QString ServerName
IP address to connect to.
virtual void WriteChild(const QString &String) override
Writes String to the communication connection's hardware interface.
void OnConnected()
Qt slot called when Socket has been connected. Calls QSerialCommunicationHardwareAdapterWorker::SetCo...
virtual void OpenChild() override
Opens the communication connection.
void OnDataAvailable()
Qt slot called when Socket has received data which can be read. Calls QSerialCommunicationHardwareAda...
virtual void CloseChild() override
Closes the communication connection.
void OnErrorOccurred(QAbstractSocket::SocketError SocketError)
Qt slot called when Socket transitioned into an error state. Calls QSerialCommunicationHardwareAdapte...
QTcpSocket Socket
COM port for serial communication.
quint16 Port
Network port to connect to.
virtual void FlushChild() override
Flushes the communication connection's buffers.
virtual void ReadChild() override
Reads from the communication connection's hardware interface.
bool IsOpening() const noexcept
Checks whether Socket is connecting right now.
Implements a hardware adapter to communicate with text-based commands over TCP sockets.
constexpr static auto Name() noexcept
Every derived class has to redefine this function.
virtual void ResetImpl(dispatch_tag< HardwareAdapterTcpSocket >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual ~HardwareAdapterTcpSocket()=default
void InitSig(QString ServerName, quint16 Port)
Initializes HardwareAdapterTcpSocketWorker::Socket for TCP communication with the given settings.
void InitWorker() override
Tells the worker instance to perform initialization steps, e.g. by emitting a Qt signal which is rece...
virtual std::string GetName() const override
Returns the name of this Object type.
Defines an exception caused by a hardware operation on an ethernet interface (e.g....
NetworkException(std::string Description, const int ErrorCode, const std::source_location Location=std::source_location::current()) noexcept
Constructs a NetworkException instance.
Bundles several parameters to describe a network connection. Use in parameter classes.
NetworkParamsExtension(DynExp::ParamsBase &Owner)
Constructs a NetworkParamsExtension instance.
std::string MakeAddress() const
Builds a network address from the parameters' content.
DynExp::ParamsBase::Param< ParamsConfigDialog::NumberType > Port
Network port.
DynExp::ParamsBase::Param< ParamsConfigDialog::TextType > ServerName
IP address.
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
Abstract base class for object parameter classes. Each class derived from class Object must be accomp...
Definition: Object.h:326
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...
Defines an exception caused by a serial communication operation of a hardware adapter.
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.
std::string ToStr(const T &Value, int Precision=-1)
Converts a (numeric) value of type T to a std::string using operator<< of std::stringstream.
Definition: Util.h:625
Accumulates include statements to provide a precompiled header.