GEN DAQ API  4.0
Controlling GEN Series tethered Mainframes
examples/channel.c
/*
* Copyright (C) 2009 HBM Netherlands B.V.
* Schutweg 15a
* 5145 NP Waalwijk
* The Netherlands
* http://www.hbm.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "GEN_DAQ_API.h"
static const char *amplifierModeToString(GHSAmplifierMode);
static const char *channelTypeToString(GHSChannelType);
static void confirm_close(void);
static const char *directionToString(GHSDirection);
static const char *excitationTypeToString(GHSExcitationType);
static const char *filterTypeToString(GHSFilterType);
static const char *inputCouplingToString(GHSInputCoupling);
static const char *returnCodeToString(GHSReturnValue);
static const char *signalCouplingToString(GHSSignalCoupling);
static const char *timerCounterModeToString(GHSTimerCounterMode);
static const char *triggerModeToString(GHSTriggerMode);
static void terminate(const char *format, ...);
/* Auxiliary macros for enum->string transform. See usage below. */
#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;
GHSConnectionHandle connection = 0;
GHSReturnValue returnCode;
uint32_t serverAPIVersion;
GHSSlotID slotID = 'A', slotID2 = 'B';
int channelIndex = 0, channelIndex2 = 22;
GHSChannelType channelType;
char *channelName = NULL, *technicalUnit = NULL;
GHSInputCoupling inputCoupling;
GHSAmplifierMode amplifierMode;
GHSSignalCoupling signalCoupling;
double span, offset, tuMultiplier, tuOffset, filterFrequency, excitationValue;
GHSFilterType filterType;
GHSExcitationType excitationType;
GHSTriggerMode triggerMode;
double primaryLevel, secondaryLevel, hysteresis;
GHSDirection direction;
GHSEnableDisable channelEnabled;
double gateTime, upperValue, lowerValue;
printf("Connecting to %s:%u\n", ipaddress, portno);
/* Connect to the mainframe "localhost" is also possible. */
returnCode = GHSConnect(ipaddress, portno, &connection, &serverAPIVersion);
if (returnCode == GHSReturnValue_APIMismatch)
{
terminate("Failed: client has API version %u, while mainframe has API version %u.\n",
GHSGetClientAPIVersion(), serverAPIVersion);
}
else if (returnCode != GHSReturnValue_OK)
{
terminate("Failed: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("Connected\n");
/* Get channel name. */
returnCode = GHSGetChannelName(connection, slotID, channelIndex, &channelName);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetChannelName: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set channel name. */
returnCode = GHSSetChannelName(connection, slotID, channelIndex, channelName);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetChannelName: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get channel type. */
returnCode = GHSGetChannelType(connection, slotID, channelIndex, &channelType);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetChannelType: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get channel state. */
returnCode = GHSGetChannelStorageEnabled(connection, slotID, channelIndex, &channelEnabled);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetChannelStorageEnabled: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set channel state. */
returnCode = GHSSetChannelStorageEnabled(connection, slotID, channelIndex, channelEnabled);
if (returnCode != GHSReturnValue_OK)
{
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");
/* Get trigger settings. */
returnCode = GHSGetTriggerSettings(connection, slotID, channelIndex, &triggerMode, &primaryLevel, &secondaryLevel, &hysteresis, &direction);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetTriggerSettings: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set trigger settings. */
returnCode = GHSSetTriggerSettings(connection, slotID, channelIndex, triggerMode, primaryLevel, secondaryLevel, hysteresis, direction);
if (returnCode != GHSReturnValue_OK)
{
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));
/* Get amplifier mode. */
returnCode = GHSGetAmplifierMode(connection, slotID, channelIndex, &amplifierMode);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetAmplifierMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set amplifier mode. */
returnCode = GHSSetAmplifierMode(connection, slotID, channelIndex, amplifierMode);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetAmplifierMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get signal coupling. */
returnCode = GHSGetSignalCoupling(connection, slotID, channelIndex, &signalCoupling);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetSignalCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set signal coupling. */
returnCode = GHSSetSignalCoupling(connection, slotID, channelIndex, signalCoupling);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetSignalCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get input coupling. */
returnCode = GHSGetInputCoupling(connection, slotID, channelIndex, &inputCoupling);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetInputCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set input coupling. */
returnCode = GHSSetInputCoupling(connection, slotID, channelIndex, inputCoupling);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetInputCoupling: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get span and offset. */
returnCode = GHSGetSpanAndOffset(connection, slotID, channelIndex, &span, &offset);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetSpanAndOffset: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set span and offset. */
returnCode = GHSSetSpanAndOffset(connection, slotID, channelIndex, span, offset);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetSpanAndOffset: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get technical units. */
returnCode = GHSGetTechnicalUnits(connection, slotID, channelIndex, &technicalUnit, &tuMultiplier, &tuOffset);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetTechnicalUnits: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set technical units. */
returnCode = GHSSetTechnicalUnits(connection, slotID, channelIndex, technicalUnit, tuMultiplier, tuOffset);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetTechnicalUnits: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get filter type and frequency. */
returnCode = GHSGetFilterTypeAndFrequency(connection, slotID, channelIndex, &filterType, &filterFrequency);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetFilterTypeAndFrequency: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set filter type and frequency. */
returnCode = GHSSetFilterTypeAndFrequency(connection, slotID, channelIndex, filterType, filterFrequency);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetFilterTypeAndFrequency: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get excitation type and value. */
returnCode = GHSGetExcitation(connection, slotID, channelIndex, &excitationType, &excitationValue);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetExcitation: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set excitation type and value. */
returnCode = GHSSetExcitation(connection, slotID, channelIndex, excitationType, excitationValue);
if (returnCode != GHSReturnValue_OK)
{
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);
/* Get timer counter mode. */
returnCode = GHSGetTimerCounterMode(connection, slotID2, channelIndex2, &tcMode);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetTimerCounterMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set timer counter mode. */
returnCode = GHSSetTimerCounterMode(connection, slotID2, channelIndex2, tcMode);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetTimerCounterMode: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get timer counter gate time. */
returnCode = GHSGetTimerCounterGateTime(connection, slotID2, channelIndex2, &gateTime);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetTimerCounterGateTime: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set timer counter gate time. */
returnCode = GHSSetTimerCounterGateTime(connection, slotID2, channelIndex2, gateTime);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSSetTimerCounterGateTime: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Get timer counter range. */
returnCode = GHSGetTimerCounterRange(connection, slotID2, channelIndex2, &lowerValue, &upperValue);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSGetTimerCounterRange: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
/* Set timer counter range. */
returnCode = GHSSetTimerCounterRange(connection, slotID2, channelIndex2, lowerValue, upperValue);
if (returnCode != GHSReturnValue_OK)
{
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);
/* Disconnect from the mainframe (this enables Perception or another GHS API client to connect). */
returnCode = GHSDisconnect(connection);
if (returnCode != GHSReturnValue_OK)
{
terminate("Failed on GHSDisconnect: return code is %d (%s).\n", returnCode, returnCodeToString(returnCode));
}
printf("\nDisconnected\n");
confirm_close();
return EXIT_SUCCESS;
}
const char *amplifierModeToString(GHSAmplifierMode mode)
{
SWITCH(mode, CASE9(GHSAmplifierMode_, None, Basic, Bridge, Icp, ThermoCouple, BasicSensor, Charge, Current4_20, ThermoResistor));
}
const char *channelTypeToString(GHSChannelType type)
{
SWITCH(type, CASE4(GHSChannelType_, Invalid, Analog, Event, TimerCounter));
}
void confirm_close(void)
{
printf("Press ENTER to close this example program.\n");
(void)getchar();
}
const char *directionToString(GHSDirection direction)
{
SWITCH(direction, CASE2(GHSDirection_, RisingEdge, FallingEdge));
}
const char *excitationTypeToString(GHSExcitationType type)
{
SWITCH(type, CASE6(GHSExcitationType_, Voltage, Voltage_Sense, Current, Voltage_Strobed, Voltage_Sense_Strobed, Current_Strobed));
}
const char *filterTypeToString(GHSFilterType type)
{
SWITCH(type, CASE12(GHSFilterType_, Bessel, Butterworth, Elliptic, FIR, IIR, Wideband, Bessel_AA, Butterworth_AA, SigmaDeltaWB, SigmaDelta, BandPass, FIR3dB));
}
const char *inputCouplingToString(GHSInputCoupling coupling)
{
SWITCH(coupling, CASE5(GHSInCpl_, SingleEndedPositive, SingleEndedNegative, Differential, Current, FloatingDifferential));
}
const char *returnCodeToString(GHSReturnValue value)
{
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));
}
const char *signalCouplingToString(GHSSignalCoupling coupling)
{
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));
}
const char *timerCounterModeToString(GHSTimerCounterMode mode)
{
SWITCH(mode, CASE15(GHSTimerCounterMode_, RPMUniDirectional, RPMBiDirectional, RPMQuadrature, FrequencyUniDirectional, FrequencyBiDirectional, FrequencyQuadrature, CountUniDirectional, CountBiDirectional, CountQuadrature, AngleQuadrature, AngleQuadratureWithRefPos, AngleUniDirectional, AngleUniDirectionalWithRefPos, AngleBiDirectional, AngleBiDirectionalWithRefPos));
}
const char *triggerModeToString(GHSTriggerMode mode)
{
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);
}