Skip to content

Commit 4f55540

Browse files
authored
PAX C++ sample (#40)
* PAX C++ sample * update for OPM7.0, SCPI docu and TLPMX examples * update power meter examples for OPM7.0
1 parent b72a9fc commit 4f55540

28 files changed

+21730
-5219
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
//Example Date of Creation(YYYY - MM - DD) 2025 - 10 - 07
2+
//Example Date of Last Modification on Github 2025 - 10 - 07
3+
//Version of C++ used for Testing and IDE: C++ 14, Visual Studio 2022
4+
//Version of the Thorlabs SDK used : PAX1000 Software version 1.7
5+
//Example Description: The sample code shows how to control a PAX1000 in C++.
6+
//In the example the available PAX1000 are found, a connection is established,
7+
//measurement mode and scan rate are set and scan data is read. Azimuth, ellipticity and DOP are printed to the console.
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <visa.h>
11+
#include "TLPAX.h"
12+
#include <windows.h>
13+
14+
#define PI_VAL (3.1415926535897932384626433832795f)
15+
16+
ViStatus find_instrument(ViChar** resource);
17+
void error_exit(ViSession instrHdl, ViStatus err);
18+
void waitKeypress(void);
19+
ViStatus get_device_id(ViSession ihdl);
20+
ViStatus set_measurement_mode(ViSession ihdl);
21+
ViStatus set_basic_scan_rate(ViSession ihdl);
22+
ViStatus get_scan(ViSession ihdl);
23+
24+
int main()
25+
{
26+
ViStatus err;
27+
ViChar* rscPtr;
28+
ViSession instrHdl = VI_NULL;
29+
int c, done;
30+
31+
printf("PAX1000 sample\n");
32+
33+
// Find resources
34+
err = find_instrument(&rscPtr);
35+
if (err) error_exit(VI_NULL, err);
36+
if (rscPtr == NULL) exit(EXIT_SUCCESS); // No instrument found
37+
38+
39+
// Open session to PAX series instrumentset
40+
printf("Opening session to '%s' ...\n\n", rscPtr);
41+
err = TLPAX_init(rscPtr, VI_OFF, VI_ON, &instrHdl);
42+
if (err) error_exit(instrHdl, err);
43+
44+
if ((err = get_device_id(instrHdl))) error_exit(instrHdl, err);
45+
if ((err = set_measurement_mode(instrHdl))) error_exit(instrHdl, err);
46+
if ((err = set_basic_scan_rate(instrHdl))) error_exit(instrHdl, err);
47+
48+
Sleep(5000); // Wait 5 s until rotation stabilizes
49+
50+
for (int i = 0; i < 5; i++)
51+
{
52+
if ((err = get_scan(instrHdl))) error_exit(instrHdl, err);
53+
}
54+
55+
if (instrHdl != VI_NULL) TLPAX_close(instrHdl);
56+
return 0;
57+
}
58+
59+
void clearInputBuffer()
60+
{
61+
int ch;
62+
while ((ch = getchar()) != '\n' && ch != EOF);
63+
}
64+
65+
void error_exit(ViSession instrHdl, ViStatus err)
66+
{
67+
ViChar buf[TLPAX_ERR_DESCR_BUFFER_SIZE];
68+
69+
// Print error
70+
TLPAX_errorMessage(instrHdl, err, buf);
71+
fprintf(stderr, "ERROR: %s\n", buf);
72+
73+
// Close instrument handle if open
74+
if (instrHdl != VI_NULL) TLPAX_close(instrHdl);
75+
76+
// Exit program
77+
waitKeypress();
78+
exit(EXIT_FAILURE);
79+
}
80+
81+
void waitKeypress(void)
82+
{
83+
printf("Press <ENTER> to exit\n");
84+
while (getchar() == EOF);
85+
}
86+
87+
ViStatus find_instrument(ViChar** resource)
88+
{
89+
ViStatus err;
90+
static ViChar rscBuf[TLPAX_BUFFER_SIZE];
91+
ViUInt32 findCnt;
92+
93+
printf("Scanning for instruments ...\n");
94+
95+
*resource = NULL;
96+
97+
err = TLPAX_findRsrc(0, &findCnt);
98+
if (err) return err;
99+
100+
if (findCnt < 1)
101+
{
102+
printf("No matching instruments found\n\n");
103+
return VI_ERROR_RSRC_NFOUND;
104+
}
105+
106+
// connect to first device
107+
err = TLPAX_getRsrcName(0, 0, rscBuf);
108+
if (!err) *resource = rscBuf;
109+
return err;
110+
}
111+
112+
ViStatus get_device_id(ViSession ihdl)
113+
{
114+
ViStatus err;
115+
ViChar nameBuf[TLPAX_BUFFER_SIZE];
116+
ViChar snBuf[TLPAX_BUFFER_SIZE];
117+
ViChar revBuf[TLPAX_BUFFER_SIZE];
118+
119+
err = TLPAX_identificationQuery(ihdl, VI_NULL, nameBuf, snBuf, revBuf);
120+
if (err) return err;
121+
printf("Instrument: %s\n", nameBuf);
122+
printf("Serial number: %s\n", snBuf);
123+
printf("Firmware: V%s\n", revBuf);
124+
if ((err = TLPAX_revisionQuery(ihdl, revBuf, VI_NULL))) return err;
125+
printf("Driver: V%s\n", revBuf);
126+
127+
return VI_SUCCESS;
128+
}
129+
130+
char const* get_measurement_mode_label(ViUInt32 mode)
131+
{
132+
char const* str;
133+
134+
switch (mode)
135+
{
136+
case TLPAX_MEASMODE_IDLE: str = "Idle, no measurements are taken"; break;
137+
case TLPAX_MEASMODE_HALF_512: str = "0.5 revolutions for one measurement, 512 points for FFT"; break;
138+
case TLPAX_MEASMODE_HALF_1024: str = "0.5 revolutions for one measurement, 1024 points for FFT"; break;
139+
case TLPAX_MEASMODE_HALF_2048: str = "0.5 revolutions for one measurement, 2048 points for FFT"; break;
140+
case TLPAX_MEASMODE_FULL_512: str = "1 revolution for one measurement, 512 points for FFT"; break;
141+
case TLPAX_MEASMODE_FULL_1024: str = "1 revolution for one measurement, 1024 points for FFT"; break;
142+
case TLPAX_MEASMODE_FULL_2048: str = "1 revolution for one measurement, 2048 points for FFT"; break;
143+
case TLPAX_MEASMODE_DOUBLE_512: str = "2 revolutions for one measurement, 512 points for FFT"; break;
144+
case TLPAX_MEASMODE_DOUBLE_1024: str = "2 revolutions for one measurement, 1024 points for FFT"; break;
145+
case TLPAX_MEASMODE_DOUBLE_2048: str = "2 revolutions for one measurement, 2048 points for FFT"; break;
146+
default: str = "unknown"; break;
147+
}
148+
return str;
149+
}
150+
151+
152+
ViStatus set_measurement_mode(ViSession ihdl)
153+
{
154+
ViUInt32 mode;
155+
156+
printf("Set Measurement Mode...\n");
157+
for (mode = TLPAX_MEASMODE_IDLE; mode <= TLPAX_MEASMODE_DOUBLE_2048; mode++)
158+
{
159+
printf("(%d) %s\n", mode, get_measurement_mode_label(mode));
160+
}
161+
162+
printf("\nPlease select: ");
163+
while ((mode = getchar()) == EOF);
164+
mode -= '0';
165+
clearInputBuffer();
166+
printf("\n");
167+
168+
return TLPAX_setMeasurementMode(ihdl, mode);
169+
}
170+
171+
ViStatus set_basic_scan_rate(ViSession ihdl)
172+
{
173+
ViStatus err;
174+
ViReal64 bsr, min, max;
175+
char buf[1000];
176+
177+
err = TLPAX_getBasicScanRateLimits(ihdl, &min, &max);
178+
if (err) return err;
179+
printf("Set Basic Sample Rate in 1/s...\n");
180+
printf("Enter new Basic Sample rate (%.1f ... %.1f 1/s): ", min, max);
181+
182+
fgets(buf, sizeof(buf), stdin);
183+
sscanf_s(buf, "%lf", &bsr);
184+
err = TLPAX_setBasicScanRate(ihdl, bsr);
185+
printf("\n\n");
186+
return err;
187+
}
188+
189+
190+
ViStatus get_scan(ViSession ihdl)
191+
{
192+
ViStatus err;
193+
ViSession scanId;
194+
ViReal64 azimuth;
195+
ViReal64 ellipticity;
196+
ViReal64 DOP = 0.0;
197+
198+
err = TLPAX_getLatestScan(ihdl, &scanId);
199+
if (err) return err;
200+
printf("Scan Data:\n");
201+
err = TLPAX_getPolarization(VI_NULL, scanId, &azimuth, &ellipticity);
202+
if (!err) err = TLPAX_getDOP(VI_NULL, scanId, &DOP, NULL, NULL);
203+
TLPAX_releaseScan(VI_NULL, scanId);
204+
205+
if (err) return err;
206+
printf("Azimuth: %.1f degree\n", azimuth * 180.0 / PI_VAL);
207+
printf("Ellipticity: %.1f degree\n", ellipticity * 180.0 / PI_VAL);
208+
printf("DOP: %.1f %%\n\n", DOP * 100.0);
209+
return VI_SUCCESS;
210+
}
211+
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
232+
233+
234+
235+
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>17.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{27d1ede8-7999-4aed-bf0a-11da69176880}</ProjectGuid>
25+
<RootNamespace>PAX1000sample</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<SDLCheck>true</SDLCheck>
77+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
78+
<ConformanceMode>true</ConformanceMode>
79+
</ClCompile>
80+
<Link>
81+
<SubSystem>Console</SubSystem>
82+
<GenerateDebugInformation>true</GenerateDebugInformation>
83+
</Link>
84+
</ItemDefinitionGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<FunctionLevelLinking>true</FunctionLevelLinking>
89+
<IntrinsicFunctions>true</IntrinsicFunctions>
90+
<SDLCheck>true</SDLCheck>
91+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
92+
<ConformanceMode>true</ConformanceMode>
93+
</ClCompile>
94+
<Link>
95+
<SubSystem>Console</SubSystem>
96+
<GenerateDebugInformation>true</GenerateDebugInformation>
97+
</Link>
98+
</ItemDefinitionGroup>
99+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
100+
<ClCompile>
101+
<WarningLevel>Level3</WarningLevel>
102+
<SDLCheck>true</SDLCheck>
103+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
<ConformanceMode>true</ConformanceMode>
105+
<AdditionalIncludeDirectories>C:\Program Files\IVI Foundation\VISA\Win64\include</AdditionalIncludeDirectories>
106+
</ClCompile>
107+
<Link>
108+
<SubSystem>Console</SubSystem>
109+
<GenerateDebugInformation>true</GenerateDebugInformation>
110+
<AdditionalLibraryDirectories>C:\Program Files\IVI Foundation\VISA\Win64\Lib_x64\msc</AdditionalLibraryDirectories>
111+
<AdditionalDependencies>TLPAX_64.lib;%(AdditionalDependencies);visa64.lib</AdditionalDependencies>
112+
</Link>
113+
</ItemDefinitionGroup>
114+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
115+
<ClCompile>
116+
<WarningLevel>Level3</WarningLevel>
117+
<FunctionLevelLinking>true</FunctionLevelLinking>
118+
<IntrinsicFunctions>true</IntrinsicFunctions>
119+
<SDLCheck>true</SDLCheck>
120+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121+
<ConformanceMode>true</ConformanceMode>
122+
</ClCompile>
123+
<Link>
124+
<SubSystem>Console</SubSystem>
125+
<GenerateDebugInformation>true</GenerateDebugInformation>
126+
</Link>
127+
</ItemDefinitionGroup>
128+
<ItemGroup>
129+
<ClCompile Include="PAX1000_sample.cpp" />
130+
</ItemGroup>
131+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
132+
<ImportGroup Label="ExtensionTargets">
133+
</ImportGroup>
134+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Quelldateien">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Headerdateien">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Ressourcendateien">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="PAX1000_sample.cpp">
19+
<Filter>Quelldateien</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

0 commit comments

Comments
 (0)