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;
42 if (SerialPorts.empty())
44 PortName.SetTextList(std::move(SerialPorts));
56 QSerialPort::StopBits StopBits, QSerialPort::Parity Parity)
59 Port.setPort(QSerialPortInfo(PortName));
60 Port.setBaudRate(BaudRate);
61 Port.setDataBits(DataBits);
62 Port.setStopBits(StopBits);
63 Port.setParity(Parity);
73 if (
Port.error() != QSerialPort::NoError)
87 Port.open(QIODevice::ReadWrite);
125 QByteArray BytesRead(
Port.readAll());
128 if (BytesRead.size())
134 auto StdString(String.toStdString());
136 auto BytesWritten =
Port.write(StdString.c_str(), StdString.size());
137 if (BytesWritten !=
Util::NumToT<
decltype(BytesWritten)>(StdString.size()))
144 auto Owner = std::dynamic_pointer_cast<const HardwareAdapterSerialPort>(
GetOwner());
148 const auto LineEnding =
Owner->GetLineEnding();
150 auto BytesWritten =
Port.write(
Owner->LineEndingToChar(LineEnding).data(),
Owner->GetLineEndingLength(LineEnding));
151 if (BytesWritten !=
Owner->GetLineEndingLength(LineEnding))
166 auto Worker = std::make_unique<HardwareAdapterSerialPortWorker>();
175 static bool AlreadyRegistered =
false;
177 if (!AlreadyRegistered)
179 qRegisterMetaType<QSerialPort::BaudRate>();
180 qRegisterMetaType<QSerialPort::DataBits>();
181 qRegisterMetaType<QSerialPort::StopBits>();
182 qRegisterMetaType<QSerialPort::Parity>();
184 AlreadyRegistered =
true;
190 auto DerivedParams = dynamic_Params_cast<HardwareAdapterSerialPort>(
GetParams());
192 emit
InitSig(QString::fromStdString(DerivedParams->PortName), DerivedParams->BaudRate,
193 DerivedParams->DataBits, DerivedParams->StopBits, DerivedParams->Parity);
Implementation of a hardware adapter to communicate text-based commands over COM ports.
static Util::TextValueListType< BaudRateType > BaudRateTypeStrList()
Assigns labels to the entries of BaudRateType.
BaudRateType
Extends QSerialPort::BaudRate enumeration with further Baud rates.
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.
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.
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.
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, HardwareAdapterSerialPortParams::BaudRateType 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.
static constexpr 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...
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Accumulates include statements to provide a precompiled header.