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,26 @@ 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+ { QSerialPort ::UnknownParity ,"UNKNOWN" }
194+ )
195+ DECLARE_ENUM_STRINGS (QSerialPort ::StopBits ,
196+ { QSerialPort ::OneStop , "1" },
197+ { QSerialPort ::OneAndHalfStop , "1.5" },
198+ { QSerialPort ::TwoStop , "2" },
199+ { QSerialPort ::UnknownStopBits , "UNKNOWN" },
200+ )
201+ DECLARE_ENUM_STRINGS (QSerialPort ::FlowControl ,
202+ { QSerialPort ::NoFlowControl , "NO" },
203+ { QSerialPort ::HardwareControl , "HARDWARE" },
204+ { QSerialPort ::SoftwareControl , "SOFTWARE" },
205+ { QSerialPort ::UnknownFlowControl , "UNKNOWN" },
206+ )
140207
141208///
142209/// \brief operator <<
@@ -191,6 +258,7 @@ inline QSettings& operator <<(QSettings& out, const SerialConnectionParams& para
191258 out .setValue ("SerialParams/BaudRate" , params .BaudRate );
192259 out .setValue ("SerialParams/WordLength" , params .WordLength );
193260 out .setValue ("SerialParams/Parity" , params .Parity );
261+ out .setValue ("SerialParams/StopBits" , params .StopBits );
194262 out .setValue ("SerialParams/FlowControl" , params .FlowControl );
195263 out .setValue ("SerialParams/DTR" , params .SetDTR );
196264 out .setValue ("SerialParams/RTS" , params .SetRTS );
@@ -210,6 +278,7 @@ inline QSettings& operator >>(QSettings& in, SerialConnectionParams& params)
210278 params .BaudRate = (QSerialPort ::BaudRate )in .value ("SerialParams/BaudRate" , 9600 ).toUInt ();
211279 params .WordLength = (QSerialPort ::DataBits )in .value ("SerialParams/WordLength" , 8 ).toUInt ();
212280 params .Parity = (QSerialPort ::Parity )in .value ("SerialParams/Parity" , 0 ).toUInt ();
281+ params .StopBits = (QSerialPort ::StopBits )in .value ("SerialParams/StopBits" , 1 ).toUInt ();
213282 params .FlowControl = (QSerialPort ::FlowControl )in .value ("SerialParams/FlowControl" , 0 ).toUInt ();
214283 params .SetDTR = in .value ("SerialParams/DTR" , false).toBool ();
215284 params .SetRTS = in .value ("SerialParams/RTS" , false).toBool ();
@@ -218,6 +287,82 @@ inline QSettings& operator >>(QSettings& in, SerialConnectionParams& params)
218287 return in ;
219288}
220289
290+ ///
291+ /// \brief operator <<
292+ /// \param xml
293+ /// \param params
294+ /// \return
295+ ///
296+ inline QXmlStreamWriter & operator <<(QXmlStreamWriter & xml , const SerialConnectionParams & params )
297+ {
298+ xml .writeStartElement ("SerialConnectionParams" );
299+
300+ xml .writeAttribute ("PortName" , params .PortName );
301+ xml .writeAttribute ("BaudRate" , QString ::number (params .BaudRate ));
302+ xml .writeAttribute ("DataBits" , QString ::number (params .WordLength ));
303+ xml .writeAttribute ("Parity" , enumToString (params .Parity ));
304+ xml .writeAttribute ("StopBits" , enumToString (params .StopBits ));
305+ xml .writeAttribute ("FlowControl" , enumToString (params .FlowControl ));
306+ xml .writeAttribute ("SetDTR" , boolToString (params .SetDTR ));
307+ xml .writeAttribute ("SetRTS" , boolToString (params .SetRTS ));
308+
309+ xml .writeEndElement ();
310+ return xml ;
311+ }
312+
313+ ///
314+ /// \brief operator >>
315+ /// \param xml
316+ /// \param params
317+ /// \return
318+ ///
319+ inline QXmlStreamReader & operator >>(QXmlStreamReader & xml , SerialConnectionParams & params )
320+ {
321+ if (xml .isStartElement () && xml .name () == QLatin1String ("SerialConnectionParams" )) {
322+ const QXmlStreamAttributes attributes = xml .attributes ();
323+
324+ if (attributes .hasAttribute ("PortName" )) {
325+ params .PortName = attributes .value ("PortName" ).toString ();
326+ }
327+
328+ if (attributes .hasAttribute ("BaudRate" )) {
329+ bool ok ; const auto baudRate = attributes .value ("ServicePort" ).toUInt (& ok );
330+ if (ok ) params .BaudRate = static_cast < QSerialPort ::BaudRate > (baudRate );
331+ }
332+
333+ if (attributes .hasAttribute ("DataBits" )) {
334+ bool ok ; const auto wordLength = attributes .value ("DataBits" ).toUInt (& ok );
335+ if (ok ) params .WordLength = static_cast < QSerialPort ::DataBits > (wordLength );
336+ }
337+
338+ if (attributes .hasAttribute ("Parity" )) {
339+ params .Parity = enumFromString < QSerialPort ::Parity > (attributes .value ("Parity" ).toString ());
340+ }
341+
342+ if (attributes .hasAttribute ("StopBits" )) {
343+ params .StopBits = enumFromString < QSerialPort ::StopBits > (attributes .value ("StopBits" ).toString ());
344+ }
345+
346+ if (attributes .hasAttribute ("FlowControl" )) {
347+ params .FlowControl = enumFromString < QSerialPort ::FlowControl > (attributes .value ("FlowControl" ).toString ());
348+ }
349+
350+ if (attributes .hasAttribute ("SetDTR" )) {
351+ params .SetDTR = stringToBool (attributes .value ("SetDTR" ).toString ());
352+ }
353+
354+ if (attributes .hasAttribute ("SetRTS" )) {
355+ params .SetRTS = stringToBool (attributes .value ("SetRTS" ).toString ());
356+ }
357+
358+ xml .skipCurrentElement ();
359+
360+ params .normalize ();
361+ }
362+
363+ return xml ;
364+ }
365+
221366///
222367/// \brief The ConnectionDetails class
223368///
@@ -311,4 +456,62 @@ inline QSettings& operator >>(QSettings& in, ConnectionDetails& params)
311456 return in ;
312457}
313458
459+ ///
460+ /// \brief operator <<
461+ /// \param xml
462+ /// \param cd
463+ /// \return
464+ ///
465+ inline QXmlStreamWriter & operator <<(QXmlStreamWriter & xml , const ConnectionDetails & cd )
466+ {
467+ xml .writeStartElement ("ConnectionDetails" );
468+ xml .writeAttribute ("ConnectionType" , enumToString (cd .Type ));
469+
470+ switch (cd .Type ) {
471+ case ConnectionType ::Tcp :
472+ xml << cd .TcpParams ;
473+ break ;
474+ case ConnectionType ::Serial :
475+ xml << cd .SerialParams ;
476+ break ;
477+ }
478+
479+ xml .writeEndElement ();
480+ return xml ;
481+ }
482+
483+ ///
484+ /// \brief operator >>
485+ /// \param xml
486+ /// \param cd
487+ /// \return
488+ ///
489+ inline QXmlStreamReader & operator >>(QXmlStreamReader & xml , ConnectionDetails & cd )
490+ {
491+ if (xml .isStartElement () && xml .name () == QLatin1String ("ConnectionDetails" )) {
492+ const QXmlStreamAttributes attributes = xml .attributes ();
493+
494+ if (attributes .hasAttribute ("ConnectionType" )) {
495+ cd .Type = enumFromString < ConnectionType > (attributes .value ("ConnectionType" ).toString ());
496+ }
497+
498+ switch (cd .Type ) {
499+ case ConnectionType ::Tcp :
500+ if (xml .readNextStartElement () && xml .name () == QLatin1String ("TcpConnectionParams" )) {
501+ xml >> cd .TcpParams ;
502+ }
503+ break ;
504+ case ConnectionType ::Serial :
505+ if (xml .readNextStartElement () && xml .name () == QLatin1String ("SerialConnectionParams" )) {
506+ xml >> cd .SerialParams ;
507+ }
508+ break ;
509+ }
510+
511+ xml .skipCurrentElement ();
512+ }
513+
514+ return xml ;
515+ }
516+
314517#endif // CONNECTIONDETAILS_H
0 commit comments