THM1176InstrumentManager 1.1
Qt Object abstraction for Metrolab THM1176
Loading...
Searching...
No Matches
THM1176TypeConversions.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 Metrolab Technology S.A., Geneva, Switzerland (www.metrolab.com)
2// See the included file LICENSE.txt for the licensing conditions.
3
7
8// Standard includes
9#include <cfloat>
10
11// Personal includes
14#include "Helpers.h"
15#include "OSDefines.h"
16
17// Microsoft compilers don't define endianness.
18#if _WIN32
19 #define __LITTLE_ENDIAN__ 1
20
21// GNU compilers define it in <endian.h>
22#elif __linux__
23 #include <endian.h>
24 #if __BYTE_ORDER == __LITTLE_ENDIAN
25 #define __LITTLE_ENDIAN__ 1
26 #else
27 #define __BIG_ENDIAN__ 1
28 #endif
29#endif
30
31using namespace MTL::Instrument;
32
33namespace MTL {
34 namespace Instrument {
35 namespace THM1176Types {
36
37 I16 BinaryToI16(const char pBinary[2])
38 {
39#if __LITTLE_ENDIAN__
40 const char Binary16_Out[2] = { pBinary[1], pBinary[0] };
41 return *(I16*)Binary16_Out;
42#elif __BIG_ENDIAN__
43 return *(I16*)pBinary;
44#else
45 #error Neither __LITTLE_ENDIAN__ nor __BIG_ENDIAN__ is defined.
46#endif
47 }
48 I32 BinaryToI32(const char pBinary[4])
49 {
50#if __LITTLE_ENDIAN__
51 const char Binary32_Out[4] = { pBinary[3], pBinary[2], pBinary[1], pBinary[0] };
52 return *(I32*)Binary32_Out;
53#elif __BIG_ENDIAN__
54 return *(I32*)pBinary;
55#else
56 #error Neither __LITTLE_ENDIAN__ nor __BIG_ENDIAN__ is defined.
57#endif
58 }
59 bool StringToUnits(std::string SUnits, eUnits & rUnits)
60 {
61 if (SUnits == "T") rUnits = kT;
62 else if (SUnits == "MT") rUnits = kmT;
63 else if (SUnits == "UT") rUnits = kuT;
64 else if (SUnits == "NT") rUnits = knT;
65 else if (SUnits == "GAUSs") rUnits = kGauss;
66 else if (SUnits == "KGAUss") rUnits = kkGauss;
67 else if (SUnits == "MGAUss") rUnits = kmGauss;
68 else if (SUnits == "MAHZp") rUnits = kMHzp;
69 else return false;
70
71 return true;
72 }
73 std::string UnitsToString(eUnits Units)
74 {
75 return Units == kT ? "T" :
76 Units == kmT ? "MT" :
77 Units == kuT ? "UT" :
78 Units == knT ? "NT" :
79 Units == kGauss ? "GAUSs" :
80 Units == kkGauss ? "KGAUss" :
81 Units == kmGauss ? "MGAUss" :
82 Units == kMHzp ? "MAHZP" : "";
83 }
84
85 //----------------------------------------------------------------------//
86 // TESTS //
87 //----------------------------------------------------------------------//
89 {
90 try
91 {
92 //----------------------------------------------------------------------//
93 // Binary Numbers //
94 //----------------------------------------------------------------------//
95 char Binary16[2] = { '\x12', '\xF3' };
96 if (BinaryToI16(Binary16) != 4851)
97 throw "Invalid I16 conversion";
98 char Binary32[4] = { '\x12', '\x34', '\x56', '\xF7' };
99 if (BinaryToI32(Binary32) != 305420023)
100 throw "Invalid I32 conversion";
101 }
102 catch (const char * ErrorMsg)
103 {
104 std::cout << "Error! :" << ErrorMsg << std::endl;
105 return false;
106 }
107 std::cout << MTL__FUNCTION_NAME__ << " Success" << std::endl;
108 return true;
109 }
110
111 } // namespace THM1176Types
112 } // namespace Instrument
113} // namespace MTL
Collection of utility macros for error messages.
Platform Dependent Definitions.
int I32
32-bit signed integer.
Definition OSDefines.h:27
short I16
16-bit signed integer.
Definition OSDefines.h:26
IEEE488.2 / SCPI instrument I/O: Instrument buffer management.
Type conversion routines for C++ API for Metrolab THM1176/TFM1186.
I32 BinaryToI32(const char pBinary[4])
Convert binary to I32, taking into account endedness.
std::string UnitsToString(eUnits Units)
Convert measurement units from enumeration to string.
bool TestTHM1176Types()
Test the conversion routines.
I16 BinaryToI16(const char pBinary[2])
Convert binary to I16, taking into account endedness.
bool StringToUnits(std::string SUnits, eUnits &rUnits)
Convert measurement units from string to enumeration.
eUnits
Enumeration of possible measurement units.
@ kMHzp
Equivalent proton NMR resonant frequency, in Mega-Hertz.