DynExp
Highly flexible laboratory automation for dynamically changing experiments.
HardwareAdapterSerialPort.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "moc_HardwareAdapterSerialPort.cpp"
6 
7 namespace DynExp
8 {
10  {
11  auto SerialPortInfos = QSerialPortInfo::availablePorts();
12  std::vector<std::string> SerialPortNames;
13 
14  for (int i = 0; i < Util::NumToT<int>(SerialPortInfos.length()); ++i)
15  SerialPortNames.emplace_back(SerialPortInfos[i].portName().toStdString());
16 
17  return SerialPortNames;
18  }
19 
21  {
22  auto SerialPorts = HardwareAdapterSerialPort::Enumerate();
23  if (SerialPorts.empty())
24  throw Util::EmptyException("There is not any available serial port.");
25  PortName.SetTextList(std::move(SerialPorts));
26 
28  }
29 
31  : Port(this)
32  {
33  QObject::connect(&Port, &QSerialPort::readyRead, this, &HardwareAdapterSerialPortWorker::OnDataAvailable);
34  }
35 
36  void HardwareAdapterSerialPortWorker::Init(QString PortName, QSerialPort::BaudRate BaudRate, QSerialPort::DataBits DataBits,
37  QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
38  {
40  Port.setPort(QSerialPortInfo(PortName));
41  Port.setBaudRate(BaudRate);
42  Port.setDataBits(DataBits);
43  Port.setStopBits(StopBits);
44  Port.setParity(Parity);
45  }
46 
48  {
49  Read();
50  }
51 
52  bool HardwareAdapterSerialPortWorker::CheckError(const std::source_location Location) const
53  {
54  if (Port.error() != QSerialPort::NoError)
55  {
56  SetException(SerialPortException(Util::ToStr(Port.errorString()), Port.error(), Location));
57 
58  return false;
59  }
60 
61  return true;
62  }
63 
65  {
66  if (!Port.isOpen())
67  {
68  Port.open(QIODevice::ReadWrite);
69  if (CheckError())
71  }
72  }
73 
75  {
76  if (Port.isOpen())
77  {
78  Port.clear();
79  Port.close();
80 
82  }
83  }
84 
86  {
87  Close();
88 
89  Port.clearError();
90  }
91 
93  {
94  if (Port.isOpen())
95  Port.clear();
96  }
97 
99  {
100  if (Port.isOpen())
101  Port.flush();
102  }
103 
105  {
106  QByteArray BytesRead(Port.readAll());
107  CheckError();
108 
109  if (BytesRead.size())
110  DataRead(BytesRead.constData());
111  }
112 
114  {
115  auto StdString(String.toStdString());
116 
117  auto BytesWritten = Port.write(StdString.c_str(), StdString.size()); // crop terminating '\0'
118  if (BytesWritten != Util::NumToT<decltype(BytesWritten)>(StdString.size()))
119  SetException(SerialPortException("It was not possible to write the desired amount of data to a serial port.", Port.error()));
120  CheckError();
121  }
122 
124  {
125  auto Owner = std::dynamic_pointer_cast<const HardwareAdapterSerialPort>(GetOwner());
126  if (!Owner)
127  return;
128 
129  const auto LineEnding = Owner->GetLineEnding();
130 
131  auto BytesWritten = Port.write(Owner->LineEndingToChar(LineEnding).data(), Owner->GetLineEndingLength(LineEnding));
132  if (BytesWritten != Owner->GetLineEndingLength(LineEnding))
133  SetException(SerialPortException("It was not possible to write the desired amount of data to a serial port.", Port.error()));
134  else
135  Port.flush();
136  CheckError();
137  }
138 
139  HardwareAdapterSerialPort::HardwareAdapterSerialPort(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params)
140  : QSerialCommunicationHardwareAdapter(OwnerThreadID, std::move(Params))
141  {
143  }
144 
146  {
147  auto Worker = std::make_unique<HardwareAdapterSerialPortWorker>();
148 
150 
151  return Worker;
152  }
153 
155  {
156  static bool AlreadyRegistered = false;
157 
158  if (!AlreadyRegistered)
159  {
160  qRegisterMetaType<QSerialPort::BaudRate>();
161  qRegisterMetaType<QSerialPort::DataBits>();
162  qRegisterMetaType<QSerialPort::StopBits>();
163  qRegisterMetaType<QSerialPort::Parity>();
164 
165  AlreadyRegistered = true;
166  }
167  }
168 
170  {
171  auto DerivedParams = dynamic_Params_cast<HardwareAdapterSerialPort>(GetParams());
172 
173  emit InitSig(QString::fromStdString(DerivedParams->PortName), DerivedParams->BaudRate,
174  DerivedParams->DataBits, DerivedParams->StopBits, DerivedParams->Parity);
175  }
176 
178  {
179  emit ResetSig();
180  Init();
181 
183  }
184 }
Implementation of a hardware adapter to communicate text-based commands over COM ports.
void ConfigureParamsImpl(dispatch_tag< QSerialCommunicationHardwareAdapterParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
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.
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.
HardwareAdapterSerialPort(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a hardware adapter instance.
void ResetImpl(dispatch_tag< QSerialCommunicationHardwareAdapter >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
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...
Definition: Object.cpp:436
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition: Object.h:349
void SetException(const ExceptionType &Exception) const
Calls QSerialCommunicationHardwareAdapter::SetException on QWorker::Owner. Does nothing if QWorker::O...
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 Close()
Closes the communication connection.
void SetCommunicationChannelClosed() const noexcept
Calls QSerialCommunicationHardwareAdapter::SetCommunicationChannelClosed on QWorker::Owner....
void SetCommunicationChannelOpened() const noexcept
Calls QSerialCommunicationHardwareAdapter::SetCommunicationChannelOpened on QWorker::Owner....
SerialCommunicationHardwareAdapter is based on a Qt communication object (wrapped by QSerialCommunica...
void ResetSig()
Resets the worker and the communication connection.
QSerialCommunicationHardwareAdapterWorker * Worker
Worker instance running in its own thread to handle the actual communication there.
std::unique_ptr< QSerialCommunicationHardwareAdapterWorker > QWorkerPtrType
Pointer-type owning the related QSerialCommunicationHardwareAdapterWorker instance.
constexpr static auto GetMaxBufferSize() noexcept
Defines the maximal size of the hardware adapter's (read) buffer.
Defines an exception caused by a hardware operation on a COM port (e.g. reading/writing data to a COM...
Thrown when a list is expected to contain entries and when a query results in an empty answer or an e...
Definition: Exception.h:224
auto GetOwner() const noexcept
Returns this worker instance's owner.
Definition: QtUtil.h:349
OwnerPtrType Owner
Pointer to the hardware adapter owning this instance.
Definition: QtUtil.h:371
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
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
ToT NumToT(const FromT Value)
Converts a value of a numeric type to a value of another numeric type checking the conversion for its...
Definition: Util.h:687
Accumulates include statements to provide a precompiled header.