@@ -6,235 +6,6 @@ int id_mbClientMessageParams = qRegisterMetaType<mbClientMessageParams>();
66
77namespace mb {
88
9- QStringList saveClientMessages (const QList<mbClientMessageParamsOLD> messages)
10- {
11- QStringList res;
12- Q_FOREACH (const mbClientMessageParamsOLD &p, messages)
13- {
14- res.append (saveClientMessageParams (p));
15- }
16- return res;
17- }
18-
19- QList<mbClientMessageParamsOLD> restoreClientMessages (const QStringList &messages)
20- {
21- QList<mbClientMessageParamsOLD> res;
22- bool ok = false ;
23- Q_FOREACH (const QString &s, messages)
24- {
25- auto p = restoreClientMessageParams (s, &ok);
26- if (ok)
27- res.append (p);
28- }
29- return res;
30- }
31-
32- QString saveClientMessageParams (const mbClientMessageParamsOLD ¶ms, bool useFunc, bool useData)
33- {
34- QString res;
35- switch (params.func )
36- {
37- case MBF_READ_COILS :
38- case MBF_READ_DISCRETE_INPUTS :
39- case MBF_READ_HOLDING_REGISTERS :
40- case MBF_READ_INPUT_REGISTERS :
41- if (useData)
42- res = QString (" offset=%1;count=%2;format=%3" ).arg (params.offset ).arg (params.count ).arg (mb::enumFormatKey (params.format ));
43- else
44- res = QString (" offset=%1;count=%2" ).arg (params.offset ).arg (params.count );
45- break ;
46- case MBF_WRITE_SINGLE_COIL :
47- case MBF_WRITE_SINGLE_REGISTER :
48- if (useData)
49- res = QString (" offset=%1;format=%2;data=%3" ).arg (params.offset ).arg (mb::enumFormatKey (params.format ), params.data .toString ());
50- else
51- res = QString (" offset=%1" ).arg (params.offset );
52- break ;
53- case MBF_READ_EXCEPTION_STATUS :
54- case MBF_REPORT_SERVER_ID :
55- if (useData)
56- res = QString (" format=%1" ).arg (mb::enumFormatKey (params.format ));
57- break ;
58- case MBF_DIAGNOSTICS :
59- if (useData)
60- res = QString (" subfunc=%1;format=%2;data=%3" ).arg (params.subfunc ).arg (mb::enumFormatKey (params.format ), params.data .toString ());
61- else
62- res = QString (" subfunc=%1" ).arg (params.subfunc );
63- break ;
64- case MBF_WRITE_MULTIPLE_COILS :
65- case MBF_WRITE_MULTIPLE_REGISTERS :
66- if (useData)
67- res = QString (" offset=%1;count=%2;format=%3;data=%4" )
68- .arg (params.offset ).arg (params.count ).arg (mb::enumFormatKey (params.format ), params.data .toString ());
69- else
70- res = QString (" offset=%1;count=%2" )
71- .arg (params.offset ).arg (params.count );
72- break ;
73- case MBF_MASK_WRITE_REGISTER :
74- res += QString (" offset=%1;and=%2;or=%3" ).arg (params.offset ).arg (params.andMask ).arg (params.orMask );
75- break ;
76- case MBF_READ_WRITE_MULTIPLE_REGISTERS :
77- if (useData)
78- res += QString (" readoffset=%1;readcount=%2;readformat=%3;writeoffset=%4;writecount=%5;writeformat=%6;data=%7" )
79- .arg (params.offset )
80- .arg (params.count )
81- .arg (mb::enumFormatKey (params.format ))
82- .arg (params.writeOffset )
83- .arg (params.writeCount )
84- .arg (mb::enumFormatKey (params.writeFormat ), params.data .toString ());
85- else
86- res += QString (" readoffset=%1;readcount=%2;writeoffset=%3;writecount=%5" )
87- .arg (params.offset )
88- .arg (params.count )
89- .arg (params.writeOffset )
90- .arg (params.writeCount );
91- break ;
92- case MBF_READ_FIFO_QUEUE :
93- if (useData)
94- res += QString (" offset=%1;format=%2" ).arg (params.offset ).arg (mb::enumFormatKey (params.format ));
95- else
96- res += QString (" offset=%1" ).arg (params.offset );
97- break ;
98- case MBF_ENCAPSULATED_INTERFACE_TRANSPORT :
99- if (useData)
100- res += QString (" deviceId=%1;objectId=%2;format=%3" ).arg (params.deviceId ).arg (params.objectId ).arg (mb::enumFormatKey (params.format ));
101- else
102- res += QString (" deviceId=%1;objectId=%2" ).arg (params.deviceId ).arg (params.objectId );
103- break ;
104- default :
105- break ;
106- }
107- if (useFunc)
108- {
109- if (res.isEmpty ())
110- res = QString (" FC%1" ).arg (params.func , 2 , 10 , QLatin1Char (' 0' ));
111- else
112- res = QString (" FC%1;%2" ).arg (params.func , 2 , 10 , QLatin1Char (' 0' )).arg (res);
113- }
114- return res;
115- }
116-
117- mbClientMessageParamsOLD restoreClientMessageParams (const QString ¶ms, bool *ok, uint8_t func)
118- {
119- mbClientMessageParamsOLD res;
120- QHash<QString, QString> map = getClientParamMap (params);
121- if (func == 0 )
122- {
123- bool funcOk = false ;
124- Q_FOREACH (const QString &key, map.keys ())
125- {
126- if (key.startsWith (QLatin1String (" FC" )))
127- {
128- func = static_cast <uint8_t >(key.midRef (2 ).toInt (&funcOk));
129- if (!funcOk)
130- {
131- if (ok)
132- *ok = false ;
133- return res;
134- }
135- break ;
136- }
137- }
138- if (!funcOk)
139- {
140- // Support old format where func was a separate parameter
141- func = static_cast <uint8_t >(map.value (QStringLiteral (" func" )).toInt (&funcOk));
142- if (!funcOk)
143- {
144- if (ok)
145- *ok = false ;
146- return res;
147- }
148- }
149- }
150- res.func = func;
151- switch (func)
152- {
153- case MBF_READ_COILS :
154- case MBF_READ_DISCRETE_INPUTS :
155- case MBF_READ_HOLDING_REGISTERS :
156- case MBF_READ_INPUT_REGISTERS :
157- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" offset" ), " 0" ).toInt ());
158- res.count = static_cast <uint16_t >(map.value (QStringLiteral (" count" ), " 1" ).toInt ());
159- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
160- return res;
161- case MBF_WRITE_SINGLE_COIL :
162- case MBF_WRITE_SINGLE_REGISTER :
163- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" offset" ), " 0" ).toInt ());
164- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
165- res.data = map.value (QStringLiteral (" data" ));
166- return res;
167- case MBF_READ_EXCEPTION_STATUS :
168- case MBF_REPORT_SERVER_ID :
169- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
170- return res;
171- case MBF_DIAGNOSTICS :
172- res.subfunc = static_cast <uint16_t >(map.value (QStringLiteral (" subfunc" ), " 0" ).toInt ());
173- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
174- res.data = map.value (QStringLiteral (" data" ));
175- return res;
176- case MBF_GET_COMM_EVENT_COUNTER :
177- case MBF_GET_COMM_EVENT_LOG :
178- return res;
179- case MBF_WRITE_MULTIPLE_COILS :
180- case MBF_WRITE_MULTIPLE_REGISTERS :
181- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" offset" ), " 0" ).toInt ());
182- res.count = static_cast <uint16_t >(map.value (QStringLiteral (" count" ), " 1" ).toInt ());
183- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
184- res.data = map.value (QStringLiteral (" data" ));
185- return res;
186- case MBF_MASK_WRITE_REGISTER :
187- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" offset" ), " 0" ).toInt ());
188- res.andMask = static_cast <uint16_t >(map.value (QStringLiteral (" and" ), " 0" ).toInt ());
189- res.orMask = static_cast <uint16_t >(map.value (QStringLiteral (" or" ), " 0" ).toInt ());
190- return res;
191- case MBF_READ_WRITE_MULTIPLE_REGISTERS :
192- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" readoffset" ), " 0" ).toInt ());
193- res.count = static_cast <uint16_t >(map.value (QStringLiteral (" readcount" ), " 1" ).toInt ());
194- res.format = mb::enumFormatValue (map.value (QStringLiteral (" readformat" ), " Dec16" ));
195- res.writeOffset = static_cast <uint16_t >(map.value (QStringLiteral (" writeoffset" ), " 0" ).toInt ());
196- res.writeCount = static_cast <uint16_t >(map.value (QStringLiteral (" writecount" ), " 1" ).toInt ());
197- res.writeFormat = mb::enumFormatValue (map.value (QStringLiteral (" writeformat" ), " Dec16" ));
198- res.data = map.value (QStringLiteral (" data" ));
199- return res;
200- case MBF_READ_FIFO_QUEUE :
201- res.offset = static_cast <uint16_t >(map.value (QStringLiteral (" offset" ), " 0" ).toInt ());
202- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " Dec16" ));
203- return res;
204- case MBF_ENCAPSULATED_INTERFACE_TRANSPORT :
205- res.deviceId = static_cast <uint16_t >(map.value (QStringLiteral (" deviceId" ), " 1" ).toInt ());
206- res.objectId = static_cast <uint16_t >(map.value (QStringLiteral (" objectId" ), " 0" ).toInt ());
207- res.format = mb::enumFormatValue (map.value (QStringLiteral (" format" ), " String" ));
208- return res;
209- default :
210- if (ok)
211- *ok = false ;
212- return res;
213- }
214- if (ok)
215- *ok = true ;
216- return res;
217- }
218-
219- QHash<QString, QString> getClientParamMap (const QString ¶ms)
220- {
221- QHash<QString, QString> map;
222- QStringList parts = params.split (QLatin1Char (' ;' ), Qt::SkipEmptyParts);
223- Q_FOREACH (const QString &part, parts)
224- {
225- QStringList keyValue = part.split (QLatin1Char (' =' ), Qt::KeepEmptyParts);
226- if (keyValue.size () == 2 )
227- {
228- map.insert (keyValue.at (0 ), keyValue.at (1 ));
229- }
230- else
231- {
232- map.insert (part, QString ());
233- }
234- }
235- return map;
236- }
237-
2389} // namespace mb
23910
24011mbClientMessageConverter::mbClientMessageConverter ()
0 commit comments