4 #include "moc_HardwareAdapterSerialPort.cpp"
11 auto SerialPortInfos = QSerialPortInfo::availablePorts();
12 std::vector<std::string> SerialPortNames;
14 for (
int i = 0; i < Util::NumToT<int>(SerialPortInfos.length()); ++i)
15 SerialPortNames.emplace_back(SerialPortInfos[i].portName().toStdString());
17 return SerialPortNames;
23 if (SerialPorts.empty())
25 PortName.SetTextList(std::move(SerialPorts));
37 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
40 Port.setPort(QSerialPortInfo(PortName));
41 Port.setBaudRate(BaudRate);
42 Port.setDataBits(DataBits);
43 Port.setStopBits(StopBits);
44 Port.setParity(Parity);
68 Port.open(QIODevice::ReadWrite);
106 QByteArray BytesRead(
Port.readAll());
109 if (BytesRead.size())
115 auto StdString(String.toStdString());
117 auto BytesWritten =
Port.write(StdString.c_str(), StdString.size());
118 if (BytesWritten !=
Util::NumToT<decltype(BytesWritten)>(StdString.size()))
125 auto Owner = std::dynamic_pointer_cast<const HardwareAdapterSerialPort>(
GetOwner());
129 const auto LineEnding =
Owner->GetLineEnding();
131 auto BytesWritten =
Port.write(
Owner->LineEndingToChar(LineEnding).data(),
Owner->GetLineEndingLength(LineEnding));
132 if (BytesWritten !=
Owner->GetLineEndingLength(LineEnding))
147 auto Worker = std::make_unique<HardwareAdapterSerialPortWorker>();
156 static bool AlreadyRegistered =
false;
158 if (!AlreadyRegistered)
160 qRegisterMetaType<QSerialPort::BaudRate>();
161 qRegisterMetaType<QSerialPort::DataBits>();
162 qRegisterMetaType<QSerialPort::StopBits>();
163 qRegisterMetaType<QSerialPort::Parity>();
165 AlreadyRegistered =
true;
171 auto DerivedParams = dynamic_Params_cast<HardwareAdapterSerialPort>(
GetParams());
173 emit
InitSig(QString::fromStdString(DerivedParams->PortName), DerivedParams->BaudRate,
174 DerivedParams->DataBits, DerivedParams->StopBits, DerivedParams->Parity);
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...
Param< TextList > PortName
COM port name.
HardwareAdapterSerialPortWorker()
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...
Refer to ParamsBase::dispatch_tag.
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
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...
auto GetOwner() const noexcept
Returns this worker instance's owner.
OwnerPtrType Owner
Pointer to the hardware adapter owning this instance.
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.
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.
ToT NumToT(const FromT Value)
Converts a value of a numeric type to a value of another numeric type checking the conversion for its...
Accumulates include statements to provide a precompiled header.