GEN DAQ API  4.0
Controlling GEN Series tethered Mainframes
examples/fieldbus-windows-threading.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 SOFTWAREwire OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "GEN_DAQ_API.h"
#define RECEIVING 1
#define STOP_RECEIVING 0
static void confirm_close(void);
static void terminate(const char *format, ...);
int g_ReceivingFlag = STOP_RECEIVING;
DWORD WINAPI FieldBusReceivingTask(LPVOID arg)
{
GHSReturnValue returnVar;
GHSConnectionHandle connection = *((GHSConnectionHandle *)arg);
while(g_ReceivingFlag == RECEIVING)
{
/* API Call that receives from socket and then Writes on Ring Buffer */
returnVar = GHSReceiveFieldBusData(connection);
if (returnVar != GHSReturnValue_OK)
{
printf("Failed: return code is %d.\n", returnVar);
}
}
return 0;
}
int main(void)
{
/**************************************/
/* THREADING USAGE EXAMPLE (WINDOWS) */
/**************************************/
/* GEN DAQ connection variables */
char* ipaddress = "192.168.1.1";
unsigned short portno = 8006;
GHSConnectionHandle connection = 0;
GHSReturnValue returnCode;
uint32_t serverAPIVersion;
/* FieldBus variables */
int updateRate = 2000; /* Update Rate desired by the user (Hz) */
int dataCount = 1; /* Number of data values */
int overrun = 0; /* Overrun of the Ring Buffer flag */
int i = 0;
int k = 0;
double dTimeStamp;
float *fValues;
HANDLE fieldBusReceive; /* Thread handle */
/* User starts by connecting Gen DAQ Connect API */
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.\n", returnCode);
}
/* User Initiates the FieldBus Transfer */
/* Connects to the FieldBus socket port 8007 and creates an internal handle */
/* Ring Buffer is initialized */
returnCode = GHSInitiateFieldBusDataTransfer(connection, &updateRate, &dataCount);
/* User starts Preview */
GHSStartPreview(connection);
/* Allocate memory to the array according to the number of Formulas */
fValues = malloc(sizeof(float)*dataCount);
/* Switch state of the receiving from FieldBus flag used in FieldBusReceivingTask */
g_ReceivingFlag = RECEIVING;
/* Create thread on FieldBusReceivingTask */
fieldBusReceive = CreateThread(NULL, 0, FieldBusReceivingTask, &connection, 0, NULL);
/* User desires 2000 updates (1 second) */
for (i=0; i<2000; i++)
{
/* API Call that retrieves the values from the Buffer -
in this case Blocking call is used (blocks until new fieldBus data is available */
GHSReadNextSnapshot(connection, GHS_BlockingCall, &dTimeStamp, fValues, &overrun);
/* FieldBus Values Printing */
printf("Timestamp is: %f \n", dTimeStamp);
for (k=0; k<dataCount; k++)
{
printf("\nValue of Formula %d is %f", k, fValues[k]);
}
printf("\n ----------------------------- \n");
}
/* Stops the Receiving Loop */
g_ReceivingFlag = STOP_RECEIVING;
/* Waits for the thread to finish */
WaitForSingleObject(fieldBusReceive, INFINITE);
/* Close the thread handle */
CloseHandle(fieldBusReceive);
/* User must call this API to end the FieldBus transfer and connection */
/* Stops Preview Mode */
GHSStopPreview(connection);
/* Frees the allocated value array */
free(fValues);
/* User disconnects from the Mainframe */
GHSDisconnect(connection);
return 1;
}
void confirm_close(void)
{
printf("Press ENTER to close this example program.\n");
(void)getchar();
}
void terminate(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
confirm_close();
exit(EXIT_FAILURE);
}