|
| 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 | + |
0 commit comments