How-To: Provide life data from C/C++

From FactorityWiki

Jump to: navigation, search

Contents

Purpose

This How-To gives you the information how to provide life data for factority out of C or C++ programs. The samples are designed and tested for linux.

ToDo: Please verify if it runs on Windows as well.

Source

Main program (w2server.cpp)

#include "WireWare/API_CORE.h"
#include "WireWare/API_DATAPTI.h"
 
#define DEF_TO 5000
 
int main (int argc, char** argv)
{
	WIREWARE_MACHINEI_HANDLE machineI;
	WIREWARE_DATAPOINTI_HANDLE datapointI_1;
	WIREWARE_DATAPOINTI_HANDLE datapointI_2;
	WIREWARE_DATAPOINTI_HANDLE datapointI_3;
 
	// Initialize the system (needs to be done only once)
	WireWare_Initialize ("", "", DEF_TO);
 
	// Create the named machine object
	WireWare_CreateMachine_I ("Test", 0, &machineI, DEF_TO);
 
	// Create two integer datapoints and one boolean
	WireWare_CreateDatapoint_I (machineI, "test1", WIREWARE_DATAPOINT_TYPE_INT, 0, &datapointI_1, DEF_TO);
	WireWare_CreateDatapoint_I (machineI, "test2", WIREWARE_DATAPOINT_TYPE_INT, 0, &datapointI_2, DEF_TO);
	WireWare_CreateDatapoint_I (machineI, "test3", WIREWARE_DATAPOINT_TYPE_BOOLEAN, 0, &datapointI_3, DEF_TO);
 
	// Loop for a very long time	
	int cnt1 = 1024;
	int cnt2 = 0;
	int bool3 = 0;
	while ( true ) {
		if ( cnt1 > 1000000 ) {
			break;
		}
		cnt1++;
		cnt2++;
		bool3 = !bool3;
		// Update the datapoints
		WireWare_SetDatapointValue_I (datapointI_1, &cnt1, DEF_TO);
		WireWare_SetDatapointValue_I (datapointI_2, &cnt2, DEF_TO);
		WireWare_SetDatapointValue_I (datapointI_3, &bool3, DEF_TO);
		// Sleep for a short time
		sleep(1);
	}
 
	// All open handles have to be closed before terminating	
	WireWare_CloseDatapoint_I (datapointI_1, DEF_TO);
	WireWare_CloseDatapoint_I (datapointI_2, DEF_TO);
	WireWare_CloseDatapoint_I (datapointI_3, DEF_TO);
	WireWare_CloseMachine_I (machineI, DEF_TO);
	WireWare_Shutdown (DEF_TO);
 
	return 0;
}


Makefile

OBJ = w2server.o
 
CC=g++
CFLAGS=-D__GCC__ -Wall -I${FACTORITY_HOME}/usr/include
LDFLAGS=-L${FACTORITY_HOME}/usr/lib -lWireWare -lACE -lxerces-c
 
w2server : ${OBJ}
	${CC} -o $@ $^ $(LDFLAGS)
 
%.pre : %.cpp Makefile
	${CC} -E -c ${CFLAGS} -o $@ $<
 
%.o : %.cpp Makefile 
	$(CC) -c ${CFLAGS} -o $@ $<
 
clean :
	rm -f w2server ${OBJ}


Startup script (run.sh)

#!/bin/bash
 
if [ -n "$FACTORITY_HOME" ]; then
        source $FACTORITY_HOME/usr/bin/fy-settings
else
        echo "Was not able to locate Factority (set FACTORITY_HOME)!"
        exit 1;
fi
 
./w2server


Compiling the sample

  • Call "make" in the directory, which contains the files from above


Running the sample

  • Call "run.sh" in the directory, which contains the files from above


References

How-To: Create a simple machine park view: This describes how to create a view in the machine park, which shows data, which is provided in this example.

Personal tools