66#include <QModbusDevice>
77#include <QDataStream>
88#include <QSettings>
9+ #include <QXmlStreamWriter>
910#include "enums.h"
1011
1112///
@@ -72,6 +73,7 @@ inline QDataStream& operator >>(QDataStream& in, TcpConnectionParams& params)
7273///
7374inline QSettings & operator <<(QSettings & out , const TcpConnectionParams & params )
7475{
76+ out .setValue ("TcpParams/IPAddress" , params .IPAddress );
7577 out .setValue ("TcpParams/ServicePort" , params .ServicePort );
7678 return out ;
7779}
@@ -84,11 +86,56 @@ inline QSettings& operator <<(QSettings& out, const TcpConnectionParams& params)
8486///
8587inline QSettings & operator >>(QSettings & in , TcpConnectionParams & params )
8688{
89+ params .IPAddress = in .value ("TcpParams/IPAddress" , "0.0.0.0" ).toString ();
8790 params .ServicePort = in .value ("TcpParams/ServicePort" , 502 ).toUInt ();
8891 params .normalize ();
8992 return in ;
9093}
9194
95+ ///
96+ /// \brief operator <<
97+ /// \param xml
98+ /// \param dd
99+ /// \return
100+ ///
101+ inline QXmlStreamWriter & operator <<(QXmlStreamWriter & xml , const TcpConnectionParams & params )
102+ {
103+ xml .writeStartElement ("TcpConnectionParams" );
104+ xml .writeAttribute ("IPAddress" , params .IPAddress );
105+ xml .writeAttribute ("ServicePort" , QString ::number (params .ServicePort ));
106+ xml .writeEndElement ();
107+
108+ return xml ;
109+ }
110+
111+ ///
112+ /// \brief operator >>
113+ /// \param xml
114+ /// \param params
115+ /// \return
116+ ///
117+ inline QXmlStreamReader & operator >>(QXmlStreamReader & xml , TcpConnectionParams & params )
118+ {
119+ if (xml .isStartElement () && xml .name () == QLatin1String ("TcpConnectionParams" )) {
120+ const QXmlStreamAttributes attributes = xml .attributes ();
121+
122+ if (attributes .hasAttribute ("IPAddress" )) {
123+ params .IPAddress = attributes .value ("IPAddress" ).toString ();
124+ }
125+
126+ if (attributes .hasAttribute ("ServicePort" )) {
127+ bool ok ; const quint16 port = attributes .value ("ServicePort" ).toUShort (& ok );
128+ if (ok ) params .ServicePort = port ;
129+ }
130+
131+ xml .skipCurrentElement ();
132+
133+ params .normalize ();
134+ }
135+
136+ return xml ;
137+ }
138+
92139///
93140/// \brief The SerialConnectionParams class
94141///
@@ -137,6 +184,23 @@ struct SerialConnectionParams
137184 }
138185};
139186Q_DECLARE_METATYPE (SerialConnectionParams )
187+ DECLARE_ENUM_STRINGS (QSerialPort ::Parity ,
188+ { QSerialPort ::NoParity , "NO" },
189+ { QSerialPort ::EvenParity , "EVEN" },
190+ { QSerialPort ::OddParity , "ODD" },
191+ { QSerialPort ::SpaceParity , "SPACE" },
192+ { QSerialPort ::MarkParity , "MARK" }
193+ )
194+ DECLARE_ENUM_STRINGS (QSerialPort ::StopBits ,
195+ { QSerialPort ::OneStop , "1" },
196+ { QSerialPort ::OneAndHalfStop , "1.5" },
197+ { QSerialPort ::TwoStop , "2" }
198+ )
199+ DECLARE_ENUM_STRINGS (QSerialPort ::FlowControl ,
200+ { QSerialPort ::NoFlowControl , "NO" },
201+ { QSerialPort ::HardwareControl , "HARDWARE" },
202+ { QSerialPort ::SoftwareControl , "SOFTWARE" }
203+ )
140204
141205///
142206/// \brief operator <<
@@ -191,6 +255,7 @@ inline QSettings& operator <<(QSettings& out, const SerialConnectionParams& para
191255 out .setValue ("SerialParams/BaudRate" , params .BaudRate );
192256 out .setValue ("SerialParams/WordLength" , params .WordLength );
193257 out .setValue ("SerialParams/Parity" , params .Parity );
258+ out .setValue ("SerialParams/StopBits" , params .StopBits );
194259 out .setValue ("SerialParams/FlowControl" , params .FlowControl );
195260 out .setValue ("SerialParams/DTR" , params .SetDTR );
196261 out .setValue ("SerialParams/RTS" , params .SetRTS );
@@ -210,6 +275,7 @@ inline QSettings& operator >>(QSettings& in, SerialConnectionParams& params)
210275 params .BaudRate = (QSerialPort ::BaudRate )in .value ("SerialParams/BaudRate" , 9600 ).toUInt ();
211276 params .WordLength = (QSerialPort ::DataBits )in .value ("SerialParams/WordLength" , 8 ).toUInt ();
212277 params .Parity = (QSerialPort ::Parity )in .value ("SerialParams/Parity" , 0 ).toUInt ();
278+ params .StopBits = (QSerialPort ::StopBits )in .value ("SerialParams/StopBits" , 1 ).toUInt ();
213279 params .FlowControl = (QSerialPort ::FlowControl )in .value ("SerialParams/FlowControl" , 0 ).toUInt ();
214280 params .SetDTR = in .value ("SerialParams/DTR" , false).toBool ();
215281 params .SetRTS = in .value ("SerialParams/RTS" , false).toBool ();
@@ -218,6 +284,82 @@ inline QSettings& operator >>(QSettings& in, SerialConnectionParams& params)
218284 return in ;
219285}
220286
287+ ///
288+ /// \brief operator <<
289+ /// \param xml
290+ /// \param params
291+ /// \return
292+ ///
293+ inline QXmlStreamWriter & operator <<(QXmlStreamWriter & xml , const SerialConnectionParams & params )
294+ {
295+ xml .writeStartElement ("SerialConnectionParams" );
296+
297+ xml .writeAttribute ("PortName" , params .PortName );
298+ xml .writeAttribute ("BaudRate" , QString ::number (params .BaudRate ));
299+ xml .writeAttribute ("DataBits" , QString ::number (params .WordLength ));
300+ xml .writeAttribute ("Parity" , enumToString (params .Parity ));
301+ xml .writeAttribute ("StopBits" , enumToString (params .StopBits ));
302+ xml .writeAttribute ("FlowControl" , enumToString (params .FlowControl ));
303+ xml .writeAttribute ("SetDTR" , boolToString (params .SetDTR ));
304+ xml .writeAttribute ("SetRTS" , boolToString (params .SetRTS ));
305+
306+ xml .writeEndElement ();
307+ return xml ;
308+ }
309+
310+ ///
311+ /// \brief operator >>
312+ /// \param xml
313+ /// \param params
314+ /// \return
315+ ///
316+ inline QXmlStreamReader & operator >>(QXmlStreamReader & xml , SerialConnectionParams & params )
317+ {
318+ if (xml .isStartElement () && xml .name () == QLatin1String ("SerialConnectionParams" )) {
319+ const QXmlStreamAttributes attributes = xml .attributes ();
320+
321+ if (attributes .hasAttribute ("PortName" )) {
322+ params .PortName = attributes .value ("PortName" ).toString ();
323+ }
324+
325+ if (attributes .hasAttribute ("BaudRate" )) {
326+ bool ok ; const auto baudRate = attributes .value ("ServicePort" ).toUInt (& ok );
327+ if (ok ) params .BaudRate = static_cast < QSerialPort ::BaudRate > (baudRate );
328+ }
329+
330+ if (attributes .hasAttribute ("DataBits" )) {
331+ bool ok ; const auto wordLength = attributes .value ("DataBits" ).toUInt (& ok );
332+ if (ok ) params .WordLength = static_cast < QSerialPort ::DataBits > (wordLength );
333+ }
334+
335+ if (attributes .hasAttribute ("Parity" )) {
336+ params .Parity = enumFromString < QSerialPort ::Parity > (attributes .value ("Parity" ).toString ());
337+ }
338+
339+ if (attributes .hasAttribute ("StopBits" )) {
340+ params .StopBits = enumFromString < QSerialPort ::StopBits > (attributes .value ("StopBits" ).toString ());
341+ }
342+
343+ if (attributes .hasAttribute ("FlowControl" )) {
344+ params .FlowControl = enumFromString < QSerialPort ::FlowControl > (attributes .value ("FlowControl" ).toString ());
345+ }
346+
347+ if (attributes .hasAttribute ("SetDTR" )) {
348+ params .SetDTR = stringToBool (attributes .value ("SetDTR" ).toString ());
349+ }
350+
351+ if (attributes .hasAttribute ("SetRTS" )) {
352+ params .SetRTS = stringToBool (attributes .value ("SetRTS" ).toString ());
353+ }
354+
355+ xml .skipCurrentElement ();
356+
357+ params .normalize ();
358+ }
359+
360+ return xml ;
361+ }
362+
221363///
222364/// \brief The ConnectionDetails class
223365///
@@ -311,4 +453,62 @@ inline QSettings& operator >>(QSettings& in, ConnectionDetails& params)
311453 return in ;
312454}
313455
456+ ///
457+ /// \brief operator <<
458+ /// \param xml
459+ /// \param cd
460+ /// \return
461+ ///
462+ inline QXmlStreamWriter & operator <<(QXmlStreamWriter & xml , const ConnectionDetails & cd )
463+ {
464+ xml .writeStartElement ("ConnectionDetails" );
465+ xml .writeAttribute ("ConnectionType" , enumToString (cd .Type ));
466+
467+ switch (cd .Type ) {
468+ case ConnectionType ::Tcp :
469+ xml << cd .TcpParams ;
470+ break ;
471+ case ConnectionType ::Serial :
472+ xml << cd .SerialParams ;
473+ break ;
474+ }
475+
476+ xml .writeEndElement ();
477+ return xml ;
478+ }
479+
480+ ///
481+ /// \brief operator >>
482+ /// \param xml
483+ /// \param cd
484+ /// \return
485+ ///
486+ inline QXmlStreamReader & operator >>(QXmlStreamReader & xml , ConnectionDetails & cd )
487+ {
488+ if (xml .isStartElement () && xml .name () == QLatin1String ("ConnectionDetails" )) {
489+ const QXmlStreamAttributes attributes = xml .attributes ();
490+
491+ if (attributes .hasAttribute ("ConnectionType" )) {
492+ cd .Type = enumFromString < ConnectionType > (attributes .value ("ConnectionType" ).toString ());
493+ }
494+
495+ switch (cd .Type ) {
496+ case ConnectionType ::Tcp :
497+ if (xml .readNextStartElement () && xml .name () == QLatin1String ("TcpConnectionParams" )) {
498+ xml >> cd .TcpParams ;
499+ }
500+ break ;
501+ case ConnectionType ::Serial :
502+ if (xml .readNextStartElement () && xml .name () == QLatin1String ("SerialConnectionParams" )) {
503+ xml >> cd .SerialParams ;
504+ }
505+ break ;
506+ }
507+
508+ xml .skipCurrentElement ();
509+ }
510+
511+ return xml ;
512+ }
513+
314514#endif // CONNECTIONDETAILS_H
0 commit comments