C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
PT2026TypeConversions.cpp
Go to the documentation of this file.
1// Standard includes
2#include <sstream>
3#include <cfloat>
4#include <regex>
5
6// Personal includes
9#include "Helpers.h"
10#include "OSDefines.h"
11#include "SCPIParsing.h"
12
13using namespace MTL::Instrument;
14
15namespace MTL {
16 namespace Instrument {
17 namespace PT2026Types {
18
19 // NOTE:
20 // Arbitrary block format transmits numbers in binary serialized with Least Significant Bytes first
21 // Example:
22 // Type Value Binary (Human Representation) Serialized bytes (LSB first)
23 // float64_t 0.28 0x3FD1EB851EB851EC EC 51 B8 1E 85 EB D1 3F
24 // uint64_t 247895423648521547 0x0370B3930C03DD4B 4B DD 03 0C 93 B3 70 03
25 // float32_t 0.28 0x3E8F5C29 29 5C 8F 3E
26 // uint32_t 2478954231 0x93C1D6F7 F7 D6 C1 93
27 // uint16_t 43915 0xAB8B 8B AB
28
29 U16 BinaryToU16(const char pBinary[2])
30 {
31 return *reinterpret_cast<const U16*>(pBinary);
32 }
33 I16 BinaryToI16(const char pBinary[2])
34 {
35 return *reinterpret_cast<const I16*>(pBinary);
36 }
37 U32 BinaryToU32(const char pBinary[4])
38 {
39 return *reinterpret_cast<const U32*>(pBinary);
40 }
41 I32 BinaryToI32(const char pBinary[4])
42 {
43 return *reinterpret_cast<const I32*>(pBinary);
44 }
45 F32 BinaryToF32(const char pBinary[4])
46 {
47 return *reinterpret_cast<const F32*>(pBinary);
48 }
49 U64 BinaryToU64(const char pBinary[8])
50 {
51 return *reinterpret_cast<const U64*>(pBinary);
52 }
53 F64 BinaryToF64(const char pBinary[8], bool Swap32bits)
54 {
55 // In ARM firmwares <= 1.22, float64 are swapped
56 if (Swap32bits)
57 {
58 const char Binary64_Out[8] = { pBinary[4], pBinary[5], pBinary[6], pBinary[7], pBinary[0], pBinary[1], pBinary[2], pBinary[3] };
59 return *reinterpret_cast<const F64*>(Binary64_Out);
60 }
61 else
62 {
63 return *reinterpret_cast<const F64*>(pBinary);
64 }
65 }
66 bool StringToUnits(std::string SUnits, eUnits & rUnits)
67 {
68 if (SUnits == "T") rUnits = eUnits::kT;
69 else if (SUnits == "MT") rUnits = eUnits::kmT;
70 else if (SUnits == "GAUS") rUnits = eUnits::kGauss;
71 else if (SUnits == "KGAU") rUnits = eUnits::kkGauss;
72 else if (SUnits == "PPM") rUnits = eUnits::kppm;
73 else if (SUnits == "MAHZP") rUnits = eUnits::kMHzp;
74 else if (SUnits == "MAHZ") rUnits = eUnits::kMHz;
75 else return false;
76
77 return true;
78 }
79 std::string UnitsToString(eUnits Units)
80 {
81 return Units == eUnits::kT ? "T" :
82 Units == eUnits::kmT ? "MT" :
83 Units == eUnits::kGauss ? "GAUS" :
84 Units == eUnits::kkGauss ? "KGAU" :
85 Units == eUnits::kppm ? "PPM" :
86 Units == eUnits::kMHzp ? "MAHZP" :
87 Units == eUnits::kMHz ? "MAHZ" : "";
88 }
89 bool StringtoExtendedChannel(std::string sExtChan, sExtendedChannel & rExtChan)
90 {
91 // Separate elements
92 std::vector<std::string> l_Tokens;
93 SCPI::SplitString(sExtChan, l_Tokens, ',');
94 if (l_Tokens.size() != 6)
95 return false;
96 // Channel
97 std::vector<std::string> l_ChannelList;
98 SCPI::SplitString(l_Tokens[0], l_ChannelList, '!');
99 if (l_ChannelList.empty())
100 return false;
101 for (auto & ChanIt : l_ChannelList)
102 rExtChan.Channel.push_back(static_cast<unsigned char>(std::stoul(ChanIt)));
103 // Type
104 bool l_TypeRecognized = false;
105 for (int l_Index = 0; !l_TypeRecognized && l_Index < static_cast<std::underlying_type<ePeriphObjectType>::type>(ePeriphObjectType::NbTypes); l_Index++)
106 {
107 // Parse peripheral type
108 if (l_Tokens[1] == PeriphObjectTypeStr[l_Index])
109 {
110 rExtChan.Type = static_cast<ePeriphObjectType>(l_Index);
111 l_TypeRecognized = true;
112 }
113 }
114 if (!l_TypeRecognized) // In case of parsing failure above, try to parse as legacy format
115 { // Legacy support of ARM firmwares <= 1.22
116 if (l_Tokens[1] == "SinglePulsed")
117 { rExtChan.Type = ePeriphObjectType::kPwProbe; l_TypeRecognized = true; }
118 else if (l_Tokens[1] == "Multiplexer")
119 { rExtChan.Type = ePeriphObjectType::kPwMultiplexer; l_TypeRecognized = true; }
120 else if (l_Tokens[1] == "SwitchedPulsed")
121 { rExtChan.Type = ePeriphObjectType::kPwCameraHead; l_TypeRecognized = true; }
122 else if (l_Tokens[1] == "CameraPulsed")
123 { rExtChan.Type = ePeriphObjectType::kPwCamera; l_TypeRecognized = true; }
124 }
125 if (!l_TypeRecognized)
126 return false;
127 // Model
128 rExtChan.Model = std::stoul(l_Tokens[2]);
129 // Serial
130 rExtChan.SerialNumber = std::stoul(l_Tokens[3]);
131 // NoPorts
132 rExtChan.NoPorts = static_cast<U8>(std::stoul(l_Tokens[4]));
133 // CanMeasure
134 if (l_Tokens[5] == "1")
135 rExtChan.CanMeasure = true;
136 else if (l_Tokens[5] == "0")
137 rExtChan.CanMeasure = false;
138 else
139 return false;
140
141 return true;
142 }
143 bool StringToExtendedChannelList(std::string SExtChanList, CExtendedChannelList & rExtChanList)
144 {
145 for (size_t l_beg = 0, l_end = 0; l_beg < SExtChanList.size() && l_end < SExtChanList.size(); )
146 {
147 l_beg = SExtChanList.find_first_of('(', l_end);
148 l_end = SExtChanList.find_first_of(')', l_beg);
149 if (l_beg != std::string::npos && l_end > l_beg + 2)
150 {
151 sExtendedChannel l_ExtChan;
152 if (StringtoExtendedChannel(SExtChanList.substr(l_beg + 1, l_end - l_beg - 1), l_ExtChan))
153 rExtChanList.push_back(l_ExtChan);
154 else
155 return false;
156 }
157 }
158
159 return true;
160 }
161 bool ParsePT2026IDN(const std::string& rIdentification, std::string& rManufacturer, std::string& rModel, std::string& rSerialNumber, std::string& rFirmwareLevel)
162 {
163 // As defined by the IEEE488.2 standard, the response syntax for the Identification query is defined as follows:
164 // <Manufacturer>,<Model>,<SerialNumber>,<FirmwareLevel>
165 // <Manufacturer> : Required
166 // <Model> : Required
167 // <SerialNumber> : Character '0' if not available
168 // <FirmwareLevel> : Character '0' if not available
169 // Here is an example of a PT2026 IDN: "Metrolab Technology SA,PT2026,367,elC2D3C0-arm1.23-dsp2.10-cpld1.9-pa3.9-opt00000001"
170 static const char* StandardIDNResponse = "^([^,]+),([^,]+),(0|[^,]+),(0|.+)"; // Captures <Manufacturer>,<Model>,<SerialNumber>,<FirmwareLevel>
171 static const char* PT2026IDNResponse = "^(Metrolab Technology S\\.?A\\.?),(PT202[67][\\w\\+]?),([0-9]+),(.+)"; // Captures <Manufacturer>,<Model>,<SerialNumber>,<FirmwareLevel> for dedicated Legacy PT2026 IDN
172 std::smatch l_Matches;
173 if (std::regex_search(rIdentification, l_Matches, std::regex(PT2026IDNResponse)))
174 {
175 rManufacturer = l_Matches[1];
176 rModel = l_Matches[2];
177 rSerialNumber = l_Matches[3];
178 rFirmwareLevel = l_Matches[4];
179 return true;
180 }
181 return false;
182 }
183 bool ParsePT2026BIdnFirmwareVersion(const std::string& rIdnOrFirmwareLevel, U32& rMajor, U32& rMinor) //TODO validate it for PT2026+ for futur use
184 {
185 // This function parse for the PT2026+ the firmware level from IDN response.
186 // The firmware level structurd as: "<Major_6digits>_<Minor_6digits>" (for example: "260625_140657")
187 // It corresponds to the overall firmware version of the PT2026+ Instrument
188 // Only works with PT2026+
189
190 static const char* l_OverallVersRegexStruct = "([0-9]{6})[_]([0-9]{6})"; // "([0-9]+)[_]([0-9]+)" //Captures <Major>_<Minor> overall firmware versions of PT2026+
191 std::smatch l_Matches;
192 if (std::regex_search(rIdnOrFirmwareLevel, l_Matches, std::regex(l_OverallVersRegexStruct)))
193 {
194 rMajor = static_cast<U32>(std::stoul(l_Matches[1]));
195 rMinor = static_cast<U32>(std::stoul(l_Matches[2]));
196 return true;
197 }
198 return false;
199 }
200 bool ParseArmFirmwareVersion(const std::string& rIdnOrFirmwareLevel, U32& rMajor, U32& rMinor)
201 {
202 // This function only works with legacy PT2026, and not with PT2026+/B
203 static const char* PT2026ARMversions = "-arm([0-9]+)\\.([0-9]+)"; // Captures <Major>,<Minor> arm firmware versions of legacy PT2026
204 std::smatch l_Matches;
205 if (std::regex_search(rIdnOrFirmwareLevel, l_Matches, std::regex(PT2026ARMversions)))
206 {
207 rMajor = static_cast<U32>(std::stoul(l_Matches[1]));
208 rMinor = static_cast<U32>(std::stoul(l_Matches[2]));
209 return true;
210 }
211 return false;
212 }
213 bool VersionIsLowerOrEqual(const U32& rMajor, const U32& rMinor, const U32& rCompMajor, const U32& rCompMinor)
214 {
215 if (rMajor < rCompMajor)
216 return true;
217 else if (rMajor == rCompMajor)
218 return (rMinor <= rCompMinor);
219 return false;
220 }
221 bool IsYYMMDDDate(const std::string& rDateStr, std::string& rYY, std::string& rMM, std::string& rDD)
222 {
223 // Date string in the following format: YYMMDD
224 std::smatch l_M;
225 if (std::regex_match(rDateStr, l_M, std::regex("^([0-9]{2})([0-9]{2})([0-9]{2})$")))
226 {
227 rYY = l_M[1];
228 rMM = l_M[2];
229 rDD = l_M[3];
230 return true;
231 }
232 else
233 { rYY.clear();
234 rMM.clear();
235 rDD.clear();
236 return false;
237 }
238 }
239 bool IsISO8601Date(const std::string& rDateStr, std::string& rYYYY, std::string& rMM, std::string& rDD)
240 {
241 // Date string for legacy firmwares >= 1.24 is in the following format: YYYY-MM-DD
242 std::smatch l_M;
243 if (std::regex_match(rDateStr, l_M, std::regex("^([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])")))
244 {
245 rYYYY = l_M[1];
246 rMM = l_M[2];
247 rDD = l_M[3];
248 return true;
249 }
250 else
251 {
252 rYYYY.clear();
253 rMM.clear();
254 rDD.clear();
255 return false;
256 }
257 }
258
259 //----------------------------------------------------------------------//
260 // TESTS //
261 //----------------------------------------------------------------------//
263 {
264 try
265 {
266 //----------------------------------------------------------------------//
267 // Binary Numbers //
268 //----------------------------------------------------------------------//
269 unsigned char l_Binary16[2] = { 0x12, 0xF3 };
270 if (BinaryToU16(reinterpret_cast<char *>(l_Binary16)) != 62226)
271 throw "Invalid U16 conversion";
272 if (BinaryToI16(reinterpret_cast<char *>(l_Binary16)) != -3310)
273 throw "Invalid I16 conversion";
274 unsigned char l_Binary32[4] = { 0x12, 0x34, 0x56, 0xF7 };
275 if (BinaryToU32(reinterpret_cast<char *>(l_Binary32)) != 4149621778)
276 throw "Invalid U32 conversion";
277 if (BinaryToI32(reinterpret_cast<char *>(l_Binary32)) != -145345518)
278 throw "Invalid I32 conversion";
279 if (BinaryToF32(reinterpret_cast<char *>(l_Binary32)) >= -4.34456E+33F - FLT_EPSILON && BinaryToF32(reinterpret_cast<char *>(l_Binary32)) <= -4.34456E+33F + FLT_EPSILON)
280 throw "Invalid F32 conversion";
281 unsigned char l_Binary64[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF1 };
282 if (BinaryToU64(reinterpret_cast<char *>(l_Binary64)) != 17428574979600299026UL)
283 throw "Invalid U64 conversion";
284 if (BinaryToF64(reinterpret_cast<char *>(l_Binary64)) >= 4.69198E+271 - DBL_EPSILON && BinaryToF64(reinterpret_cast<char *>(l_Binary64)) <= 4.69198E+271 + DBL_EPSILON)
285 throw "Invalid F64 conversion";
286 CExtendedChannelList l_ExChL;
287 std::string l_SExChL("(1,Multiplexer,6026,6,4,0),(1!2,SinglePulsed,1226,57,0,1),(1!4,SinglePulsed,1226,0,0,1)");
288 if (!StringToExtendedChannelList(l_SExChL, l_ExChL) || l_ExChL.size() != 3)
289 throw "Invalid CExtendedChannelList conversion";
290 sExtendedChannel l_ExChan = l_ExChL[1];
291 if (l_ExChan.Channel != SCPI::tChannel({ 1,2 }) ||
292 l_ExChan.Type != ePeriphObjectType::kPwProbe ||
293 l_ExChan.Model != 1226 ||
294 l_ExChan.SerialNumber != 57 ||
295 l_ExChan.NoPorts != 0 ||
296 l_ExChan.CanMeasure != true)
297 throw "Invalid CExtendedChannelList conversion";
298 }
299 catch (const char * ErrorMsg)
300 {
301 COUT("Error! :" << ErrorMsg << std::endl);
302 return false;
303 }
304 COUT(MTL__FUNCTION_NAME__ << " Success" << std::endl);
305 return true;
306 }
307
308 } // namespace PT2026Types
309 } // namespace Instrument
310} // namespace MTL
Utilities to aid in sending SCPI commands and parsing of SCPI reponses.
Collection of utility macros for error messages.
#define COUT(__X__)
Definition Helpers.h:83
Platform Dependent Definitions.
bool IsYYMMDDDate(const std::string &rDateStr, std::string &rYY, std::string &rMM, std::string &rDD)
std::string UnitsToString(eUnits Units)
bool StringtoExtendedChannel(std::string sExtChan, sExtendedChannel &rExtChan)
I32 BinaryToI32(const char pBinary[4])
U32 BinaryToU32(const char pBinary[4])
U16 BinaryToU16(const char pBinary[2])
bool StringToExtendedChannelList(std::string SExtChanList, CExtendedChannelList &rExtChanList)
F64 BinaryToF64(const char pBinary[8], bool Swap32bits=false)
bool ParsePT2026BIdnFirmwareVersion(const std::string &rIdnOrFirmwareLevel, U32 &rMajor, U32 &rMinor)
bool IsISO8601Date(const std::string &rDateStr, std::string &rYYYY, std::string &rMM, std::string &rDD)
bool VersionIsLowerOrEqual(const U32 &rMajor, const U32 &rMinor, const U32 &rCompMajor, const U32 &rCompMinor)
F32 BinaryToF32(const char pBinary[4])
bool StringToUnits(std::string SUnits, eUnits &rUnits)
U64 BinaryToU64(const char pBinary[8])
I16 BinaryToI16(const char pBinary[2])
bool ParseArmFirmwareVersion(const std::string &rIdnOrFirmwareLevel, U32 &rMajor, U32 &rMinor)
bool ParsePT2026IDN(const std::string &rIdentification, std::string &rManufacturer, std::string &rModel, std::string &rSerialNumber, std::string &rFirmwareLevel)
std::vector< unsigned char > tChannel
SCPI channel
Definition SCPIParsing.h:25
void SplitString(const std::string &rStr, std::vector< std::string > &rStrings, char Sep)
Split a string into substrings separated by a given character.