| THM1176InstrumentManager 1.2 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
12#include "THM1176TypeConversions.h"
13#include "SCPIInstrumentBuffer.h"
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
31 using namespace MTL::Instrument;
32
33 namespace 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 //----------------------------------------------------------------------//
88 bool TestTHM1176Types()
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
Helpers.h
Collection of utility macros for error messages.
OSDefines.h
Platform Dependent Definitions.
I32
int I32
32-bit signed integer.
Definition OSDefines.h:27
I16
short I16
16-bit signed integer.
Definition OSDefines.h:26
SCPIInstrumentBuffer.h
IEEE488.2 / SCPI instrument I/O: Instrument buffer management.
THM1176TypeConversions.h
Type conversion routines for C++ API for Metrolab THM1176/TFM1186.
MTL::Instrument::THM1176Types
Definition THM1176TypeConversions.h:20
MTL::Instrument::THM1176Types::BinaryToI32
I32 BinaryToI32(const char pBinary[4])
Convert binary to I32, taking into account endedness.
Definition THM1176TypeConversions.cpp:48
MTL::Instrument::THM1176Types::UnitsToString
std::string UnitsToString(eUnits Units)
Convert measurement units from enumeration to string.
Definition THM1176TypeConversions.cpp:73
MTL::Instrument::THM1176Types::TestTHM1176Types
bool TestTHM1176Types()
Test the conversion routines.
Definition THM1176TypeConversions.cpp:88
MTL::Instrument::THM1176Types::BinaryToI16
I16 BinaryToI16(const char pBinary[2])
Convert binary to I16, taking into account endedness.
Definition THM1176TypeConversions.cpp:37
MTL::Instrument::THM1176Types::StringToUnits
bool StringToUnits(std::string SUnits, eUnits &rUnits)
Convert measurement units from string to enumeration.
Definition THM1176TypeConversions.cpp:59
MTL::Instrument::THM1176Types::eUnits
eUnits
Enumeration of possible measurement units.
Definition THM1176Types.h:182
MTL::Instrument::THM1176Types::knT
@ knT
Nano-Tesla.
Definition THM1176Types.h:186
MTL::Instrument::THM1176Types::kmT
@ kmT
Milli-Tesla.
Definition THM1176Types.h:184
MTL::Instrument::THM1176Types::kMHzp
@ kMHzp
Equivalent proton NMR resonant frequency, in Mega-Hertz.
Definition THM1176Types.h:190
MTL::Instrument::THM1176Types::kGauss
@ kGauss
Gauss.
Definition THM1176Types.h:187
MTL::Instrument::THM1176Types::kmGauss
@ kmGauss
Milli-Gauss.
Definition THM1176Types.h:189
MTL::Instrument::THM1176Types::kT
@ kT
Tesla.
Definition THM1176Types.h:183
MTL::Instrument::THM1176Types::kkGauss
@ kkGauss
Kilo-Gauss.
Definition THM1176Types.h:188
MTL::Instrument::THM1176Types::kuT
@ kuT
Micro-Tesla.
Definition THM1176Types.h:185
MTL::Instrument
Definition THM1176.h:74
MTL
Definition CTHM1176InstrumentManager.h:179