Index: /fact/tools/FSC/SocketFunctions/SocketFunctions.cpp
===================================================================
--- /fact/tools/FSC/SocketFunctions/SocketFunctions.cpp	(revision 10894)
+++ /fact/tools/FSC/SocketFunctions/SocketFunctions.cpp	(revision 10894)
@@ -0,0 +1,297 @@
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <cstdlib>
+#include <time.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "SocketFunctions.h"
+
+/*
+OpenSockets opens NumberOfSockets TCP Sockets.
+a pointer to an array of SocketDescriptors is returned.
+In case of error, the NULL pointer is returned.
+
+In order to Close the Sockets and free memory call
+void CloseSockets(int * SocketDescriptors, unsigned int NumberOfSockets);
+*/
+
+int * OpenSockets(unsigned int NumberOfSockets){
+int verbose=0;
+
+if (verbose)
+
+	if (NumberOfSockets < 1) {
+		fprintf (	stderr, 
+							"Error: OpenSockets was called with a NumberOfSockets = %d.\n", 
+							NumberOfSockets );
+							return NULL;
+	}
+	if (NumberOfSockets > MAX_NUMBER_OF_OPENED_TCP_SOCKETS) {
+		fprintf (	stderr, 
+							"Warning: OpenSockets was called with a NumberOfSockets = %d.\n", 
+							NumberOfSockets );
+							return NULL;
+	}
+	
+	int *SocketDescriptor;
+	if (verbose) fprintf (stderr, "malloc\n");
+	SocketDescriptor = (int *) malloc( NumberOfSockets * sizeof(int) );
+	if (verbose) fprintf (stderr, "after malloc\n");
+	// what if malloc was not able to get enough memory?
+	// error chekc missing here
+	// TODO
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++) {
+		if ((SocketDescriptor[i] = socket (PF_INET, SOCK_STREAM, 0)) == -1) {
+		//
+		// what does PF_INET and SOCK_STREAM stand for?
+		//
+			printf ("Error: Could not open socket Nr.: %d\n", i);
+			return NULL;
+		} 
+	}
+	return SocketDescriptor;
+	if (verbose) printf ("end of OpenSockets\n");
+} // end of OpenSockets
+
+
+
+
+
+	/*
+	EmptySockets is used to read as much data from the TCP-Sockets in question
+	as possible. After waiting "timeout_usec" (default 125ms) -- see header file
+	without any data beeing present, the Socket is assumed to be empty.
+	
+	Hopefully it is.
+	
+	return is true if there was no error during the select statement.
+	false if select returned <0, which should not happen at all.
+	What to do if this happened?
+	*/
+	
+	
+bool EmptySockets(int * SocketDescriptor, int NumberOfDescriptors, long timeout_usec){
+	
+	fd_set Sockets; // select looks into this set, and checks which Sockets state changed
+	timeval timeout; // select needs a timeout to be deifned.
+	int max_fd=0; // select needs to know the maximum descriptor in fd_set Sockets.
+	int select_return; // used for debugging 
+	
+	unsigned char wastebin[WASTEBIN_SIZE]; // data ffrom sockets is dumped here
+	
+	// find the maximum of SocketDescriptors
+	// needed by select() -- see man select
+	
+	
+	FD_ZERO (&Sockets);
+	for (int j = 0; j < NumberOfDescriptors; j++)
+	{
+		if (max_fd<SocketDescriptor[j]) {
+			max_fd = SocketDescriptor[j];
+		}
+		FD_SET (SocketDescriptor[j], &Sockets);
+	}
+	max_fd ++;
+	 
+	while (1){
+		timeout.tv_sec = 0;
+		timeout.tv_usec = timeout_usec;
+		select_return = select ( max_fd, &Sockets, NULL, NULL, &timeout);
+		if (select_return == 0) { 
+			// none of the Sockets status changed
+			// nothing to read
+			break;
+		} else if (select_return <=0) {
+			return false; // this should never happen
+			fprintf(stderr, "error in EmptySockets: select returned %d abort. \n" , select_return);
+		} else { // select returned > 0
+			// now some Sockets might contain data
+			// it is checked for every Socket using: FD_ISSET
+			// then a pieces of WASTEBIN_SIZE chars is read out and discarded.
+			for (int j = 0; j < NumberOfDescriptors; j++) {		
+				if (FD_ISSET (SocketDescriptor[j], &Sockets)){						
+					read(SocketDescriptor[j], wastebin, WASTEBIN_SIZE);
+				}
+			}
+			//now every Socket, which had still some bytes, lost WASTEBIN_SIZE of it.
+			// still some might contain data -- so select is executed again.
+		}// end of else case: select returned > 0
+	} // end of while(1) loop -- jumped out if select returns 0
+return true;
+} 
+
+/*
+	GetMaxFileDescriptor returns the value of the maximal
+	filedescriptor from NumberOfSockets SocketDescriptors.
+	The maximal filedescriptor + 1 is needed for select().
+*/
+
+int GetMaxFileDescriptor(unsigned int NumberOfSockets, int *SocketDescriptor){
+	int max_fd;
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+			if (SocketDescriptor[i] > max_fd)
+			{
+				max_fd = SocketDescriptor[i];
+			}
+	}
+	return max_fd;
+}
+
+
+
+
+/*
+Connect2Server connects NumberOfSockets TCP Sockets
+defined by SocketDescriptor
+to host defined by ServerIPaddress
+starting at Port StartPort, by adding 1 for each Socket
+
+return value is NumberOfSockets if successful
+it is zero if no Server was found and negative 
+when connecting didn't work.
+The negative magnitude indicates which connection didn't work
+*/
+
+int Connect2Server(	int *SocketDescriptor, 
+											unsigned int NumberOfSockets,  
+											unsigned int StartPort , 
+											char *ServerIPaddress , 
+											int verbose){
+	
+	struct sockaddr_in SocketAddress[NumberOfSockets];
+	struct in_addr Serveripv4addr;
+	
+	// Convert IP-Address to binary
+	if (inet_pton (AF_INET, ServerIPaddress, &Serveripv4addr) <= 0)
+	{
+		fprintf (stderr, "Error: network address not valid\n");
+		return 0;
+	}
+
+	if (verbose > 0) fprintf (stdout, "Trying to connect to %s...\n", ServerIPaddress);
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+		SocketAddress[i].sin_family = PF_INET;
+		SocketAddress[i].sin_port = htons ((unsigned short) StartPort + i);
+		SocketAddress[i].sin_addr = Serveripv4addr;
+		
+		if (connect (SocketDescriptor[i], (struct sockaddr *) &SocketAddress[i], sizeof (SocketAddress[i])) == -1)
+		{
+			fprintf (stderr, "Error: Could not connect to server %s (port %d)\n", ServerIPaddress, StartPort + i);
+			return -i;
+		}
+		else if (verbose>0) {
+				printf ("Connected to %s:%d\n", ServerIPaddress, StartPort + i);
+		}
+	}
+	
+	return NumberOfSockets;
+}
+
+/*
+CloseSockets closes NumberOfSockets TCP Sockets
+indicated by SocketDescriptor and frees the associated memory.
+*/
+int CloseSockets (int * SocketDescriptor , 
+									unsigned int NumberOfSockets) 
+{
+	int close_return_val;
+	int return_val=0;
+	
+	for (unsigned int i = 0; i < NumberOfSockets; i++)
+	{
+		if (  (close_return_val = close (SocketDescriptor[i])) != 0) {
+		fprintf (stderr, "Error: while trying to close SocketDescriptor[%d] = %d, close returned %d\n",
+							i , 
+							SocketDescriptor[i],
+							close_return_val );
+							return_val = close_return_val;
+							// no return close_return_val 
+							// is performed here, because I want to try to close
+							// the other Ports before returning.
+		}
+		// ERRNO should better be checked and printed as well.
+		// TODO
+	}
+	
+	return return_val;
+
+} // end of CloseSockets
+	
+
+// returns Array of Sockethandles
+int * OpenServerSockets (unsigned int NumberOfSockets, unsigned int StartPort) {
+
+	// allocate memory for ServerSocket Descriptors
+//	int ServerSocket[NumberOfSockets];
+	int *ServerSocket;
+	ServerSocket = (int *) malloc( NumberOfSockets * sizeof(int) );
+
+	// Create Sockets
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+    if ((ServerSocket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+      printf("Could not open server socket Nr. %d, no remote connection possible .\n", i);
+      return NULL;
+    }
+  }
+
+  // Allows immediate reuse of socket after closing (circumvents TIME_WAIT)
+  int Value=1;
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+    if (setsockopt(ServerSocket[i], SOL_SOCKET, SO_REUSEADDR, (char *) &Value, sizeof (Value)) == -1) {
+      printf("Warning (ServerSocket Nr. %d): Could not set server socket option SO_REUSEADDR \n", i);
+    }
+  }
+  
+	struct sockaddr_in SocketAddress[NumberOfSockets];
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+    SocketAddress[i].sin_family = PF_INET;
+    SocketAddress[i].sin_port = htons((unsigned short) StartPort + i);
+    SocketAddress[i].sin_addr.s_addr = INADDR_ANY;
+  }
+
+
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+    if (bind(ServerSocket[i], (struct sockaddr *) &SocketAddress[i], sizeof(SocketAddress[i])) == -1) {
+      printf("Could not bind port %d to socket Nr.: %d\n", StartPort+i, i );
+      close(ServerSocket[i]);
+      return NULL;
+    }
+  }
+
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+    if (listen(ServerSocket[i], 0) == -1) {
+      printf("Could not set socket Nr. %d to listening \n", i);
+      close(ServerSocket[i]);
+      return NULL;
+    }
+  }
+
+	int *ConnectionSocket;
+	ConnectionSocket = (int *) malloc( NumberOfSockets * sizeof(int) );
+	struct sockaddr_in ClientAddress;
+  socklen_t SizeClientAddress=sizeof(ClientAddress);
+  
+
+//  while (1) { // Looping to wait for incoming connection
+  	printf ("Waiting for connection...\n");
+	  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+			if ((ConnectionSocket[i] = accept(ServerSocket[i], (struct sockaddr *) &ClientAddress, &SizeClientAddress)) == -1){
+				close(ServerSocket[i]);
+				return NULL;
+			}
+   	}
+//	}
+  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
+	//	close(ServerSocket[i]); 
+	}
+
+	printf("Connected to client at %s \n", inet_ntoa(ClientAddress.sin_addr));
+	return ConnectionSocket;
+} // end of OpenServerSockets
Index: /fact/tools/FSC/SocketFunctions/SocketFunctions.h
===================================================================
--- /fact/tools/FSC/SocketFunctions/SocketFunctions.h	(revision 10894)
+++ /fact/tools/FSC/SocketFunctions/SocketFunctions.h	(revision 10894)
@@ -0,0 +1,61 @@
+#define MAX_NUMBER_OF_OPENED_TCP_SOCKETS 100
+
+/*
+OpenSockets opens NumberOfSockets TCP Sockets.
+a pointer to an array of SocketDescriptors is returned.
+In case of error, the NULL pointer is returned.
+
+In order to Close the Sockets and free memory call
+void CloseSockets(int * SocketDescriptors, unsigned int NumberOfSockets);
+*/
+int * OpenSockets(unsigned int NumberOfSockets);
+
+/*
+	EmptySockets is used to read as much data from the TCP-Sockets in question
+	as possible. After waiting "timeout_usec" (default 125ms) -- see header file
+	without any data beeing present, the Socket is assumed to be empty.
+	
+	Hopefully it is.
+	
+	return is true if there was no error during the select statement.
+	false if select returned <0, which should not happen at all.
+	What to do if this happened?
+	*/
+	#define WASTEBIN_SIZE 1000
+bool EmptySockets(int *SocketDescriptor, int NumberOfDescriptors, long timeout_usec = 125000L);
+
+/*
+	GetMaxFileDescriptor returns the value of the maximal
+	filedescriptor from NumberOfSockets SocketDescriptors.
+	The maximal filedescriptor + 1 is needed for select().
+*/
+int GetMaxFileDescriptor(unsigned int NumberOfSockets, int *SocketDescriptor);
+
+/*
+Connect2Server connects NumberOfSockets TCP Sockets
+defined by SocketDescriptor
+to host defined by ServerIPaddress
+starting at Port StartPort, by adding 1 to each Socket
+
+return value is NumberOfSockets if successful
+it is zero if no Server was found and negative 
+when connecting didn't work.
+The negative magnitude indicates which connection didn't work
+*/
+int Connect2Server(	int *SocketDescriptor, 
+											unsigned int NumberOfSockets,  
+											unsigned int StartPort , 
+											char *ServerIPaddress , 
+											int verbose = 0);
+											
+											
+/*
+CloseSockets closes NumberOfSockets TCP Sockets
+indicated by SocketDescriptor and frees the associated memory.
+*/
+int CloseSockets (int * SocketDescriptor , 
+									unsigned int NumberOfSockets);
+
+									
+int * OpenServerSockets (unsigned int NumberOfSockets, unsigned int StartPort) ;
+
Index: /fact/tools/FSC/simple_daq/FSCFormat.h
===================================================================
--- /fact/tools/FSC/simple_daq/FSCFormat.h	(revision 10894)
+++ /fact/tools/FSC/simple_daq/FSCFormat.h	(revision 10894)
@@ -0,0 +1,27 @@
+#ifndef FSCFORMAT_H_
+#define FSCFORMAT_H_
+
+typedef struct
+{
+	uint_32t 	status;
+	uint_32t 	time_sec;
+	uint_16t 	time_ms;
+	uint_16t 	FR_period;
+	uint_16t 	rref;
+	uint_8t		ad7719_readings_sincelast_muxing;
+	uint_8t		ad7719_current_channel;
+	uint_32t	ad7719_current_reading;
+	uint_8t		adc_readings_sincelast_muxing;
+	uint_8t		adc_current_channel;
+	uint_32t	adc_current_reading;
+	uint_8t		reserved[6];
+	uint_8t		resistence_en[8];
+	uint_8t		voltage_en[10];
+	uint_8t		resistence_done[8];
+	uint_8t		voltage_done[10];
+	uint_32t	resisntace[64];
+	uint_16t	voltage[76];
+}  __attribute__((__packed__)) FSC_STATUS;
+
+
+#endif //FSCFORMAT_H_
Index: /fact/tools/FSC/simple_daq/Makefile
===================================================================
--- /fact/tools/FSC/simple_daq/Makefile	(revision 10894)
+++ /fact/tools/FSC/simple_daq/Makefile	(revision 10894)
@@ -0,0 +1,21 @@
+CC=g++
+REVISION = $(shell svnversion -n)
+CFLAGS=-c -Wall -DREVISION='"$(REVISION)"'
+LDFLAGS=
+SOURCES=simple_daq.cpp cmd_send.cpp ../SocketFunctions/SocketFunctions.cpp
+OBJECTS=$(SOURCES:.cpp=.o)
+EXECUTABLE=simple_daq
+
+
+
+
+all: $(SOURCES) $(EXECUTABLE)
+	
+$(EXECUTABLE): $(OBJECTS) 
+	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
+
+.cpp.o:
+	$(CC) $(CFLAGS) $< -o $@
+
+clean: 
+	rm -f $(OBJECTS) $(EXECUTABLE)
Index: /fact/tools/FSC/simple_daq/cmd_send.cpp
===================================================================
--- /fact/tools/FSC/simple_daq/cmd_send.cpp	(revision 10894)
+++ /fact/tools/FSC/simple_daq/cmd_send.cpp	(revision 10894)
@@ -0,0 +1,349 @@
+#include "simple_daq.h"
+
+// Send commands to socket
+void cmd_send (const char* Buffer, int Socket)
+{
+/*
+	int i;
+	unsigned short CMD_Buffer[512];
+	unsigned char CMD_Str[16];
+	unsigned short CMD_Num = 1;
+	unsigned int val, num;
+*/
+
+
+	printf("cmd_send is called with \"%s\" for socket %d.\n",Buffer,Socket);
+	
+	write (Socket, Buffer, 4 * sizeof (char));
+
+/*
+	if (strncmp (Buffer, "r\n", 2) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_Start);
+		printf ("# Start Run ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+
+	else if (strncmp (Buffer, "s\n", 2) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_Stop);
+		printf ("# Stop Run ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+	else if (strncmp (Buffer, "sz\n", 3) == 0)
+	{
+		CMD_Buffer[0] = 0x0000;
+		printf ("# Send 0x0000 ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+	else if (strncmp (Buffer, "t\n", 2) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_Trigger);
+		printf ("# Trigger ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "de\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_DENABLE);
+		printf ("# domino wave enabled ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "psup\n", 5) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_PS_DIRINC);
+		printf ("# increasing phase shift ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "psdn\n", 5) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_PS_DIRDEC);
+		printf ("# decreasing phase shift ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "psreset\n", 8) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_PS_RESET);
+		printf ("# resetting phase shift ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+    else if (strncmp (Buffer, "ps\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_PS_DO);
+		printf ("# phase shifting once! ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+
+	
+    else if (strncmp (Buffer, "dd\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_DDISABLE);
+		printf ("# domino wave disabled ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "dr\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_DWRITE_RUN);
+		printf ("# DWRITE HIGH->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+    else if (strncmp (Buffer, "ds\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_DWRITE_STOP);
+		printf ("# DWRITE LOW ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+
+	else if (strncmp (Buffer, "srclkon\n", 8) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_SRCLK_ON);
+		printf ("# DRS readoutclock SRCLK ENABLED ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	else if (strncmp (Buffer, "srclkoff\n", 9) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_SRCLK_OFF);
+		printf ("# DRS readoutclock SRCLK DISABLED ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}	
+	else if (strncmp (Buffer, "sclkon\n", 7) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_SCLK_ON);
+		printf ("# SCLK ENABLED ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	else if (strncmp (Buffer, "sclkoff\n", 8) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_SCLK_OFF);
+		printf ("# SCLK DISABLED ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+	else if (strncmp (Buffer, "tc\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_Trigger_C);
+		printf ("# Continuous Trigger ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+	else if (strncmp (Buffer, "ts\n", 3) == 0)
+	{
+		CMD_Buffer[0] = htons (CMD_Trigger_S);
+		printf ("# Stop Trigger ->\n");
+		printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+	}
+	
+	// Set ROI value
+	else if (strncmp (Buffer, "sr ", 3) == 0)
+	{
+		CMD_Num = 0;
+		if (sscanf (Buffer, "%3s %i %i", CMD_Str, &num, &val) == 3)
+		{
+			if ((num >= 0) & (num <= MAX_ROINUM) & (val >= 0) & (val <= MAX_ROIVAL))
+			{
+				printf ("# Set ROI %d to %d ->\n", num, val);
+				CMD_Buffer[0] = htons (CMD_Write | (BADDR_ROI + num));
+				CMD_Buffer[1] = htons (val);
+				printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
+				CMD_Num = 2;
+			}
+			else
+			{
+				printf ("Out of range\n");
+				printf ("Usage: sr NUM VAL: Set ROI NUM (0-%d) to VAL (0-%d)\n", MAX_ROINUM, MAX_ROIVAL);
+			}
+		}
+		else
+		{
+			printf ("Usage: sr NUM VAL: Set ROI NUM (0-%d) to VAL (0-%d)\n", MAX_ROINUM, MAX_ROIVAL);
+		}
+	}
+	
+	// Set all ROIs
+	else if (strncmp (Buffer, "sra ", 4) == 0)
+	{
+		CMD_Num = 0;
+		if (sscanf (Buffer, "%4s %i", CMD_Str, &val) == 2)
+		{
+			if ((val >= 0) & (val <= MAX_ROIVAL))
+			{
+				printf ("# Set all ROIs to %d ->\n", val);
+				for (i = 0; i < (MAX_ROINUM + 1); i++)
+				{
+					CMD_Buffer[i * 2] = htons (CMD_Write | (BADDR_ROI + i));
+					CMD_Buffer[(i * 2) + 1] = htons (val);
+					CMD_Num += 2;
+				}
+				for (i = 0; i < ((MAX_ROINUM + 1) * 2); i += 8)
+				{
+					printf ("# 0x%.4X 0x%.4X    0x%.4X 0x%.4X    0x%.4X 0x%.4X    0x%.4X 0x%.4X\n",
+							ntohs (CMD_Buffer[i]), ntohs (CMD_Buffer[i + 1]), 
+							ntohs (CMD_Buffer[i + 2]), ntohs (CMD_Buffer[i + 3]),
+							ntohs (CMD_Buffer[i + 4]), ntohs (CMD_Buffer[i + 5]),
+							ntohs (CMD_Buffer[i + 6]), ntohs (CMD_Buffer[i + 7]));
+				}
+			}
+			else
+			{
+				printf ("Out of range\n");
+				printf ("Usage: sra VAL: Set all ROIs to VAL (0-%d)\n", MAX_ROIVAL);
+			}
+		}
+		else
+		{
+			printf ("Usage: sra VAL: Set all ROIs to VAL (0-%d)\n", MAX_ROIVAL);
+		}
+	}
+
+	// Set DAC value
+	else if (strncmp (Buffer, "sd ", 3) == 0)
+	{
+		CMD_Num = 0;
+		if (sscanf (Buffer, "%3s %i %i", CMD_Str, &num, &val) == 3)
+		{
+			if ((num >= 0) & (num <= MAX_DACNUM) & (val >= 0) & (val <= MAX_DACVAL))
+			{
+				printf ("# Set DAC %d to %d ->\n", num, val);
+				CMD_Buffer[0] = htons (CMD_Write | (BADDR_DAC + num));
+				CMD_Buffer[1] = htons (val);
+				printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
+				CMD_Num = 2;
+			}
+			else
+			{
+				printf ("Out of range\n");
+				printf ("Usage: sd NUM VAL: Set DAC NUM (0-%d) to VAL (0-%d)\n", MAX_DACNUM, MAX_DACVAL);
+			}
+		}
+		else
+		{
+			printf ("Usage: sd NUM VAL: Set DAC NUM (0-%d) to VAL (0-%d)\n", MAX_DACNUM, MAX_DACVAL);
+		}
+	}
+
+	// Set address to value
+	else if (strncmp (Buffer, "sa ", 3) == 0)
+	{
+		CMD_Num = 0;
+		if (sscanf (Buffer, "%4s %i %i", CMD_Str, &num, &val) == 3)
+		{
+			if ((num >= 0) & (num <= MAX_ADDR) & (val >= 0) & (val <= MAX_VAL))
+			{
+				printf ("# Set ADDR %d to %d ->\n", num, val);
+				CMD_Buffer[0] = htons (CMD_Write | num);
+				CMD_Buffer[1] = htons (val);
+				printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
+				CMD_Num = 2;
+			}
+			else
+			{
+				printf ("Out of range\n");
+				printf ("Usage: sa NUM VAL: Set addr NUM (0-%d) to VAL (0-%d)\n", MAX_ADDR, MAX_VAL);
+			}
+		}
+		else
+		{
+			printf ("Usage: raw NUM VAL: Set addr NUM (0-%d) to VAL (0-%d)\n", MAX_ADDR, MAX_VAL);
+		}
+	}
+
+	// Send value
+	else if (strncmp (Buffer, "sv ", 3) == 0)
+	{
+		CMD_Num = 0;
+		if (sscanf (Buffer, "%4s %i", CMD_Str, &val) == 2)
+		{
+			if ((val >= 0) & (val <= MAX_VAL))
+			{
+				printf ("# Send %d ->\n", val);
+				CMD_Buffer[0] = htons (val);
+				printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
+				CMD_Num = 1;
+			}
+			else
+			{
+				printf ("Out of range\n");
+				printf ("Usage: sv VAL: Send VAL (0-%d)\n", MAX_VAL);
+			}
+		}
+		else
+		{
+			printf ("Usage: sv VAL: Send VAL (0-%d)\n", MAX_VAL);
+		}
+	}
+
+	else if (strncmp (Buffer, "q\n", 2) == 0)
+	{
+		// Exit program
+		printf ("# Quit\n");
+		exit_program (EXIT_SUCCESS);
+	}
+	
+	// Anything else... , print help
+	else
+	{
+		printf ("Commands:\n");
+		printf ("    t:          Single Trigger\n\n");
+		printf ("    tc:         Continuous Trigger\n");
+		printf ("    ts:         Stop Trigger\n");
+
+		printf ("\n");
+		printf ("    sclkon:     Switch SPI SLCK on\n");
+		printf ("    sclkoff:    Switch SPI SLCK off\n");
+
+		printf ("\n");
+		printf ("    srclkon:     Switch DRS SRLCK on\n");
+		printf ("    srclkoff:    Switch DRS SRLCK off\n");
+
+		printf ("\n");
+		printf ("    ps:         Phase shift ADCCLK against SRCLK - one step\n");
+		printf ("    psup:       'increse' phase shift - default\n");
+		printf ("    psdn:       'decrease' phase shift\n");
+		printf ("    psreset:    reset phase shift\n");
+
+
+		printf ("\n");
+		printf ("    de:         DENABLE HIGH\n");
+		printf ("    dd:         DENABLE LOW\n");
+		printf ("    dr:         DWRITE HIGH\n");
+		printf ("    ds:         DWRITE LOW\n");
+
+		printf ("\n");
+		printf ("    sr NUM VAL: Set ROI NUM (0-35) to VAL (0-1024)\n");
+		printf ("    sra VAL:    Set all ROIs to VAL (0-1024)\n");
+		printf ("    sd NUM VAL: Set DAC NUM (0-7) to VAL (0-65535)\n");
+		printf ("    q:          Quit\n");
+		printf ("\n");
+		printf ("    Only for debugging:\n");
+		printf ("        sa NUM VAL: Set addr NUM (0-255) to VAL (0-65535)\n");
+		printf ("        sv VAL:     Send VAL\n");
+		printf ("        sz:         Send 0x0000\n");
+		printf ("\n");
+		printf ("    r:          Start Run -- TODO\n");
+		printf ("    s:          Stop Run  -- TODO\n");
+
+		CMD_Num = 0;
+	}
+	
+	// Send commands
+	if (CMD_Num > 0)
+	{
+		if (write (Socket, CMD_Buffer, CMD_Num * sizeof (short)) < 1)
+		{
+			printf ("Error: could not write to socket\n");
+		}
+	}
+*/
+
+}
+
Index: /fact/tools/FSC/simple_daq/config.txt
===================================================================
--- /fact/tools/FSC/simple_daq/config.txt	(revision 10894)
+++ /fact/tools/FSC/simple_daq/config.txt	(revision 10894)
@@ -0,0 +1,4 @@
+192.168.0.2
+./
+socket
+dat
Index: /fact/tools/FSC/simple_daq/simple_daq.cpp
===================================================================
--- /fact/tools/FSC/simple_daq/simple_daq.cpp	(revision 10894)
+++ /fact/tools/FSC/simple_daq/simple_daq.cpp	(revision 10894)
@@ -0,0 +1,310 @@
+/********************************************************************\
+
+  Read SOCKETS_PER_FAD Channels from FAD-Board
+  Write Commands to Socket 0, Commands must be (n * 16) Bit long
+  
+  Usage: SocketClient [IP-Address]
+  
+  kw, 05.10
+  
+  based on:
+  Generic example for remote control via a socket interface
+
+  Oliver Grimm, March 2009
+
+\********************************************************************/
+#ifndef PATH_MAX
+#define PATH_MAX 1000
+#endif
+#include "simple_daq.h"
+#include "FSCFormat.h"
+#include "../SocketFunctions/SocketFunctions.h"
+
+#include "iostream"
+
+int revision = atoi(REVISION) * (strchr(REVISION,'M')==NULL ? 1:-1);
+int fad_firmware_revision;
+
+int init_fad();
+int get_firmware_revision();
+int *SocketDescriptor;
+using namespace std;
+
+FSC_STATUS *answer;
+
+int main(int argc, char *argv[])
+{
+	// what kind of buffer is this?
+	char Buffer[MAX_COM_SIZE];
+	
+	int read_return;
+
+	fd_set ReadFileDescriptor;
+	int max_fd = 0;
+	
+	FILE* outfile[SOCKETS_PER_FAD];
+	char outfilename[PATH_MAX];
+
+	
+		SimpleDaqConfiguration *conf;
+	// Get configuration file path from command line
+	// Get configurarion from configurationfile.
+	// such as FAD IP Adress
+	if (argc > 1)
+	{
+		conf = getConfig(argv[1]);
+	} else {
+		conf = getConfig(CONFIG_FILE_PATH);
+	}
+	
+	// Open sockets
+	SocketDescriptor = OpenSockets(SOCKETS_PER_FAD);
+	if (SocketDescriptor == NULL) {
+		exit_program (EXIT_FAILURE);
+	}
+	max_fd = GetMaxFileDescriptor(SOCKETS_PER_FAD, SocketDescriptor);
+	
+	// Connect to server
+	if( Connect2Server(	SocketDescriptor, SOCKETS_PER_FAD, FIRST_DAQPORT, conf->FADIPAddress, 1) != SOCKETS_PER_FAD) {
+			// Connect2Server prints some error messages in case of exceptions...
+			printf ("Error in Connect2Server()\n");
+			exit_program (EXIT_FAILURE);
+	}
+
+	// Open files for output
+	for (int i = 0; i < SOCKETS_PER_FAD; i++)
+	{
+		sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
+		if ((outfile[i] = fopen (outfilename, "w")) == NULL)
+		{
+			printf ("Error: Could not open file %s\n", outfilename);
+			exit_program (EXIT_FAILURE);
+		}
+	}
+	
+	//init_fad();
+	
+	//fad_firmware_revision = get_firmware_revision();	
+	printf ("software revision is: %s or %d\n",REVISION,revision);
+	//printf ("firmware revision is: %d\n",fad_firmware_revision);
+	
+
+
+	signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
+	signal (SIGINT, int_handler); // Cleanup after SIGINT (CTRL-C)
+
+//	init_fad();
+//	fflush (stdin);
+	// Main loop
+	while (true)
+	{
+		fflush (stdout);
+
+		FD_ZERO (&ReadFileDescriptor);		
+		FD_SET(STDIN_FILENO, &ReadFileDescriptor);
+		for (int i = 0; i < SOCKETS_PER_FAD; i++)
+		{
+			FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
+		}
+
+		// Wait for data from sockets
+		if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
+		{
+			perror ("Error with select()\n");
+			break;
+		}
+		
+		memset (Buffer, 0, sizeof (Buffer));
+ 
+
+		// init board -- should be switchable 
+		// with a commandline switch, but ... 
+		// TODO
+
+
+		// Data from STDIN
+		if (FD_ISSET (STDIN_FILENO, &ReadFileDescriptor))
+		{
+			fgets (Buffer, MAX_COM_SIZE, stdin);
+			// Send command to socket 0
+			cmd_send (Buffer, SocketDescriptor[0]);			
+		}
+		// Data from sockets
+		else
+		{
+			// Check all sockets
+			for (int i = 0; i < SOCKETS_PER_FAD; i++)
+			{
+				if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
+				{ 
+					// Only for testing
+					memset (Buffer, 0xAA, sizeof (Buffer));
+					if ((read_return = read (SocketDescriptor[i], Buffer, MAX_COM_SIZE)) == 0)
+					{
+						printf ("Error: Server not existing anymore, exiting...\n");
+						exit_program (EXIT_FAILURE);
+					}
+	
+					if (read_return > 0)
+					{
+						printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
+						
+						
+						fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
+						// Important!!!
+						fflush (outfile[i]);
+					}
+				}
+			}
+		}
+	} // while (TRUE)
+
+	exit (EXIT_SUCCESS);
+}
+
+// close sockets and exit
+void exit_program (int exit_status)
+{	
+	printf ("\nClosing Sockets...");
+	for (int i = 0; i < SOCKETS_PER_FAD; i++)
+	{
+		close (SocketDescriptor[i]);
+	}
+	printf (" done\n");
+
+
+	
+	exit (exit_status);
+}
+
+// SIGINT
+void int_handler (int sig)
+{
+	exit_program (EXIT_SUCCESS);
+}
+
+
+// note: verbose is not used, but anyway defined.
+SimpleDaqConfiguration *getConfig (const char *path, int verbose) {
+FILE* ConfigFile;
+// try to open config file
+// if not exists return NULL
+ConfigFile = fopen (path, "r");
+if (ConfigFile == NULL) {
+	return NULL;
+}
+
+//create SimpleDaqConfiguration
+SimpleDaqConfiguration *conf = new SimpleDaqConfiguration();
+
+// read config data from file and fill in SimpleDaqConfiguration
+fscanf( ConfigFile , "%s" , conf->FADIPAddress );
+fscanf( ConfigFile , "%s" , conf->outfilepath );
+fscanf( ConfigFile , "%s" , conf->outfilename );
+fscanf( ConfigFile , "%s" , conf->outfileext );
+
+return conf;
+}
+
+int init_fad(){
+
+// current FSC needs no init procedure ... it's not smart enough :-)
+/*
+	cmd_send ("ds\n", SocketDescriptor[0]);			
+	cmd_send ("dd\n", SocketDescriptor[0]);				
+sleep (1);
+	cmd_send ("sd 0 21000\n", SocketDescriptor[0]);
+	cmd_send ("sd 1 0\n", SocketDescriptor[0]);
+	cmd_send ("sd 2 5000\n", SocketDescriptor[0]);
+	cmd_send ("sd 3 5000\n", SocketDescriptor[0]);
+	cmd_send ("sd 4 28800\n", SocketDescriptor[0]);
+	cmd_send ("sd 5 28800\n", SocketDescriptor[0]);
+	cmd_send ("sd 6 28800\n", SocketDescriptor[0]);
+	cmd_send ("sd 7 28800\n", SocketDescriptor[0]);
+
+sleep (1);
+	cmd_send ("sra 10\n", SocketDescriptor[0]);
+	cmd_send ("sa 44 29\n", SocketDescriptor[0]);			
+sleep (1);
+	cmd_send ("t\n", SocketDescriptor[0]);			
+sleep (1);
+	cmd_send ("sa 44 30\n", SocketDescriptor[0]);			
+sleep (1);
+	cmd_send ("t\n", SocketDescriptor[0]);			
+sleep (1);
+	cmd_send ("sa 44 0\n", SocketDescriptor[0]);			
+sleep (1);
+	cmd_send ("t\n", SocketDescriptor[0]);
+	cmd_send ("sclkoff\n", SocketDescriptor[0]);			
+	cmd_send ("de\n", SocketDescriptor[0]);			
+	cmd_send ("dr\n", SocketDescriptor[0]);			
+	cmd_send ("sra 1024\n", SocketDescriptor[0]);			
+sleep (1);
+
+	EmptySockets(SocketDescriptor, 8, 750000L);
+
+
+
+	printf (	"\n\n FAD initialised. \n "
+						"ROI is 1024. \n"
+						"DRS shift registers are initialised.\n"
+						"DRS is up and running.\n"
+						"SPI SCLK was swithced off, TEMP readout makes no sense.\n" 
+						"DAC changes will neighter work. SWITCH SCLK back on, when trying to change DAC values\n"
+						);
+*/
+return 0;
+}
+
+int get_firmware_revision(){
+
+fd_set ReadFileDescriptor;
+int max_fd = 0;
+	
+int read_return;
+unsigned char buffer[984];
+unsigned short rev;
+
+max_fd = GetMaxFileDescriptor(SOCKETS_PER_FAD, SocketDescriptor);
+
+	cmd_send ("sra 10\n", SocketDescriptor[0]);	
+	cmd_send ("t\n", SocketDescriptor[0]);					
+	sleep (1);
+
+
+		FD_ZERO (&ReadFileDescriptor);		
+		for (int i = 0; i < SOCKETS_PER_FAD; i++)
+		{
+			FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
+		}
+
+		// Wait for data from sockets
+		if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
+		{
+			perror ("Error with select()\n");
+			
+		}
+		
+ 
+			// Check all sockets
+			for (int i = 0; i < SOCKETS_PER_FAD; i++)
+			{
+				if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
+				{ 
+					if ((read_return = read (SocketDescriptor[i], buffer, 984)) == 0)
+					{
+						printf ("Error: Server not existing anymore, exiting...\n");
+						return -1;
+					}
+					if (read_return > 0)
+					{
+						printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
+					}
+				}
+			}
+
+	rev = (unsigned char)buffer[4]<<8 | (unsigned char)buffer [5];
+
+return rev;
+}
+
Index: /fact/tools/FSC/simple_daq/simple_daq.h
===================================================================
--- /fact/tools/FSC/simple_daq/simple_daq.h	(revision 10894)
+++ /fact/tools/FSC/simple_daq/simple_daq.h	(revision 10894)
@@ -0,0 +1,72 @@
+#ifndef SOCKETCLIENT_H_
+#define SOCKETCLIENT_H_
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <signal.h>
+
+#define MAX_COM_SIZE 476
+#define IP_ADDR_LENGTH 16
+
+#define FIRST_DAQPORT 5000
+#define SOCKETS_PER_FAD 1
+
+// Commands for FAD
+#define CMD_Start 0xC000		// Start Run
+#define CMD_Stop 0x3000			// Stop Run
+#define CMD_Trigger 0xA000 		// single trigger
+#define CMD_DENABLE 0x0600      // DENABLE line HIGH
+#define CMD_DDISABLE 0x0700     // DENABLE line LOW
+#define CMD_DWRITE_RUN 0x0800   // DWRITE possibly HIGH
+#define CMD_DWRITE_STOP 0x0900  // DWRITE always LOW
+
+#define CMD_SCLK_ON 0x1000  
+#define CMD_SCLK_OFF 0x1100
+
+#define CMD_PS_DIRINC 0x1200
+#define CMD_PS_DIRDEC 0x1300
+#define CMD_PS_DO 0x1400
+#define CMD_PS_RESET 0x1700
+
+
+#define CMD_SRCLK_ON 0x1500
+#define CMD_SRCLK_OFF 0x1600
+
+#define CMD_Trigger_C 0xB000 	// continous trigger
+#define CMD_Trigger_S 0x2000 	// stop continous trigger
+#define CMD_Read 0x0A00 		// read from Config-RAM
+#define CMD_Write 0x0500 		// write to Config-RAM
+
+// Addresses in FAD Config-RAM
+#define MAX_ADDR 0xFF // highest address in Config-RAM
+#define BADDR_ROI 0x00 // Baseaddress ROI-Values
+#define BADDR_DAC 0x24 // Baseaddress DAC-Values
+
+// Max-Values
+#define MAX_VAL 65535
+#define MAX_ROINUM 35
+#define MAX_ROIVAL 1024
+#define MAX_DACNUM 7
+#define MAX_DACVAL 65535
+
+#define CONFIG_FILE_PATH "config.txt"
+
+class SimpleDaqConfiguration {
+public:
+	char FADIPAddress[IP_ADDR_LENGTH];
+	char outfilepath[200];
+	char outfilename[50];
+	char outfileext[10];
+};
+
+void cmd_send (const char* Buffer, int Socket); // Send commands to socket
+void int_handler (int sig); // Handle signal SIGINT (CTRL-C)
+void exit_program (int exit_status); // Cleanup and exit
+SimpleDaqConfiguration *getConfig (const char *path, int verbose = 0);
+
+#endif /*SOCKETCLIENT_H_*/
