#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
static void confirm_close(void);
static void terminate(const char *format, ...);
#define CASE1(prefix,c1) case prefix##c1: return #c1;
#define CASE2(prefix,c1,c2) CASE1(prefix,c1) CASE1(prefix,c2)
#define CASE3(prefix,c1,c2,c3) CASE2(prefix,c1,c2) CASE1(prefix,c3)
#define CASE4(prefix,c1,c2,c3,c4) CASE3(prefix,c1,c2,c3) CASE1(prefix,c4)
#define CASE5(prefix,c1,c2,c3,c4,c5) CASE4(prefix,c1,c2,c3,c4) CASE1(prefix,c5)
#define CASE6(prefix,c1,c2,c3,c4,c5,c6) CASE5(prefix,c1,c2,c3,c4,c5) CASE1(prefix,c6)
#define CASE7(prefix,c1,c2,c3,c4,c5,c6,c7) CASE6(prefix,c1,c2,c3,c4,c5,c6) CASE1(prefix,c7)
#define CASE8(prefix,c1,c2,c3,c4,c5,c6,c7,c8) CASE7(prefix,c1,c2,c3,c4,c5,c6,c7) CASE1(prefix,c8)
#define CASE9(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9) CASE8(prefix,c1,c2,c3,c4,c5,c6,c7,c8) CASE1(prefix,c9)
#define CASE10(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10) CASE9(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9) CASE1(prefix,c10)
#define CASE11(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11) CASE10(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10) CASE1(prefix,c11)
#define CASE12(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12) CASE11(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11) CASE1(prefix,c12)
#define CASE13(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13) CASE12(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12) CASE1(prefix,c13)
#define CASE14(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14) CASE13(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13) CASE1(prefix,c14)
#define CASE15(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15) CASE14(prefix,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14) CASE1(prefix,c15)
#define SWITCH(value,cases) switch(value) { cases; default: return "UNKNOWN"; }
#define SWITCH3(value,cases,cases2,cases3) switch(value) { cases; cases2; cases3; default: return "UNKNOWN"; }
int main(void)
{
char* ipaddress = "192.168.1.1";
unsigned short portno = 8006;
uint32_t serverAPIVersion;
int channelIndex = 0, channelIndex2 = 22;
char *channelName = NULL, *technicalUnit = NULL;
double span, offset, tuMultiplier, tuOffset, filterFrequency, excitationValue;
double primaryLevel, secondaryLevel, hysteresis;
double gateTime, upperValue, lowerValue;
printf("Connecting to %s:%u\n", ipaddress, portno);
returnCode =
GHSConnect(ipaddress, portno, &connection, &serverAPIVersion);
{
terminate("Failed: client has API version %u, while mainframe has API version %u.\n",
}
{
terminate("Failed: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("Connected\n");
{
terminate("Failed on GHSGetChannelName: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetChannelName: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetChannelType: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetChannelStorageEnabled: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetChannelStorageEnabled: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\nRecorder slot: %c\n", slotID);
printf("Channel index: %d\n", channelIndex);
printf("Channel name: %s\n", channelName);
printf("Type: %s\n", channelTypeToString(channelType));
printf(
"Channel %s\n", channelEnabled==
GHS_Enable?
"enabled":
"disabled");
returnCode =
GHSGetTriggerSettings(connection, slotID, channelIndex, &triggerMode, &primaryLevel, &secondaryLevel, &hysteresis, &direction);
{
terminate("Failed on GHSGetTriggerSettings: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
returnCode =
GHSSetTriggerSettings(connection, slotID, channelIndex, triggerMode, primaryLevel, secondaryLevel, hysteresis, direction);
{
terminate("Failed on GHSSetTriggerSettings: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\n=== Trigger ===\n");
printf("Trigger mode: %s\n", triggerModeToString(triggerMode));
printf("Primary level (V): %.2f\n", primaryLevel);
printf("Secondary level (V): %.2f\n", secondaryLevel);
printf("Hysteresis (V): %.2f\n", hysteresis);
printf("Direction: %s\n", directionToString(direction));
{
terminate("Failed on GHSGetAmplifierMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetAmplifierMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetSignalCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetSignalCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetInputCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetInputCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetSpanAndOffset: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetSpanAndOffset: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
returnCode =
GHSGetTechnicalUnits(connection, slotID, channelIndex, &technicalUnit, &tuMultiplier, &tuOffset);
{
terminate("Failed on GHSGetTechnicalUnits: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
returnCode =
GHSSetTechnicalUnits(connection, slotID, channelIndex, technicalUnit, tuMultiplier, tuOffset);
{
terminate("Failed on GHSSetTechnicalUnits: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetFilterTypeAndFrequency: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetFilterTypeAndFrequency: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
returnCode =
GHSGetExcitation(connection, slotID, channelIndex, &excitationType, &excitationValue);
{
terminate("Failed on GHSGetExcitation: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
returnCode =
GHSSetExcitation(connection, slotID, channelIndex, excitationType, excitationValue);
{
terminate("Failed on GHSSetExcitation: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\n=== Input Basic Voltage ===\n");
printf("Amplifier mode: %s\n", amplifierModeToString(amplifierMode));
printf("Signal coupling: %s\n", signalCouplingToString(signalCoupling));
printf("Input coupling: %s\n", inputCouplingToString(inputCoupling));
printf("Span (V): %.2f\n", span);
printf("Offset (V): %.2f\n", offset);
printf("Technical unit: %s\n", technicalUnit);
printf("Technical unit multiplier: %.2f\n", tuMultiplier);
printf("Technical unit offset: %.2f\n", tuOffset);
printf("Filter type: %s\n", filterTypeToString(filterType));
printf("Filter frequency (Hz): %.2f\n", filterFrequency);
printf("Excitation type: %s\n", excitationTypeToString(excitationType));
printf("Excitation value: %.2f\n", excitationValue);
{
terminate("Failed on GHSGetTimerCounterMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetTimerCounterMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetTimerCounterGateTime: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetTimerCounterGateTime: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSGetTimerCounterRange: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
{
terminate("Failed on GHSSetTimerCounterRange: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\n=== Input Timer Counter ===\n");
printf("Mode: %s\n", timerCounterModeToString(tcMode));
printf("Gate time (s): %.2f\n", gateTime);
printf("Range upper value (V): %.2f\n", upperValue);
printf("Range lower value (V): %.2f\n", lowerValue);
{
terminate("Failed on GHSDisconnect: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\nDisconnected\n");
confirm_close();
return EXIT_SUCCESS;
}
{
SWITCH(mode, CASE9(GHSAmplifierMode_, None, Basic, Bridge, Icp, ThermoCouple, BasicSensor, Charge, Current4_20, ThermoResistor));
}
{
SWITCH(type, CASE4(GHSChannelType_, Invalid, Analog, Event, TimerCounter));
}
void confirm_close(void)
{
printf("Press ENTER to close this example program.\n");
(void)getchar();
}
{
SWITCH(direction, CASE2(GHSDirection_, RisingEdge, FallingEdge));
}
{
SWITCH(type, CASE6(GHSExcitationType_, Voltage, Voltage_Sense, Current, Voltage_Strobed, Voltage_Sense_Strobed, Current_Strobed));
}
{
SWITCH(type, CASE12(GHSFilterType_, Bessel, Butterworth, Elliptic, FIR, IIR, Wideband, Bessel_AA, Butterworth_AA, SigmaDeltaWB, SigmaDelta, BandPass, FIR3dB));
}
{
SWITCH(coupling, CASE5(GHSInCpl_, SingleEndedPositive, SingleEndedNegative, Differential, Current, FloatingDifferential));
}
{
SWITCH3(value, CASE15(GHSReturnValue_, Reserved, OK, NOK, EmptySlot, NullPtrArgument, InvalidSlotID, SystemNotIdle, SystemNotRecording, SystemNotPaused, InvalidSampleRate, InvalidHandle, APIMismatch, ConnectionFailed, InvalidIP, MainframeTimeout), CASE15(GHSReturnValue_, InsufficientDiskSpace, CreateRecordingFailed, NoConnection, IncompatibleStorageLocation, RecordingNotFound, SystemNotInPreview, AlreadyConnected, InvalidRecordingName, InvalidChannelIndex, InvalidUserMode, InvalidChannelType, InvalidTriggerPosition, InvalidSweepMode, NoRecordersInMainframe, InvalidContinuousMode), CASE15(GHSReturnValue_, InvalidModeForTriggerPosition, Adapted, InvalidUTF8Character, DuplicateChannelName, InvalidDataType, MethodNotFound, InvalidJSONFormat, UnkownErrorMessage, FieldBusError_NotConfigured, FieldBusError_FieldBusAlready_Enabled, FieldBusError_NullFieldBusRingBuffer, FieldBusError_InvalidFieldBusPacket, FieldBusError_NotConnected, FieldBusError_BufferSizeInvalid, FieldBusError_FieldBusNotSupported));
}
{
SWITCH(coupling, CASE15(GHSSigCpl_, GND, DC, AC, DC_RMS, AC_RMS, DC_Frequency, AC_Frequency, DC_TrueRMS, AC_TrueRMS, DC_ExternalProbe, AC_ExternalProbe, Reference, ZeroReset, SinglePrecision, DoublePrecision));
}
{
SWITCH(mode, CASE15(GHSTimerCounterMode_, RPMUniDirectional, RPMBiDirectional, RPMQuadrature, FrequencyUniDirectional, FrequencyBiDirectional, FrequencyQuadrature, CountUniDirectional, CountBiDirectional, CountQuadrature, AngleQuadrature, AngleQuadratureWithRefPos, AngleUniDirectional, AngleUniDirectionalWithRefPos, AngleBiDirectional, AngleBiDirectionalWithRefPos));
}
{
SWITCH(mode, CASE8(GHSTriggerMode_, Off, Basic, Dual, Window, DualWindow, Sequential, QualifierBasic, QualifierDual));
}
void terminate(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
confirm_close();
exit(EXIT_FAILURE);
}