/********************************************************************\ Read NUMBER_OF_SOCKETS 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; 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[NUMBER_OF_SOCKETS]; char outfilename[PATH_MAX]; FSC_STATUS *answer; SimpleDaqConfiguration *conf; // Get configuration file path from command line // Get configurarion from configurationfile. // such as IP Adress if (argc > 1) { conf = getConfig(argv[1]); } else { conf = getConfig(CONFIG_FILE_PATH); } if (conf == NULL) { fprintf ( stderr, "Error: Config Error. \n"); exit_program (EXIT_FAILURE); } // Open sockets SocketDescriptor = OpenSockets(NUMBER_OF_SOCKETS); if (SocketDescriptor == NULL) { fprintf(stderr, "Error: While opening sockets. \n"); exit_program (EXIT_FAILURE); } max_fd = GetMaxFileDescriptor(NUMBER_OF_SOCKETS, SocketDescriptor); // Connect to server if( Connect2Server( SocketDescriptor, NUMBER_OF_SOCKETS, FIRST_DAQPORT, conf->FADIPAddress, 1) != NUMBER_OF_SOCKETS) { // 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 < NUMBER_OF_SOCKETS; 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 output-file %s\n", outfilename); exit_program (EXIT_FAILURE); } } //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) // Main loop while (true) { fflush (stdout); FD_ZERO (&ReadFileDescriptor); FD_SET(STDIN_FILENO, &ReadFileDescriptor); for (int i = 0; i < NUMBER_OF_SOCKETS; 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)); // 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 < NUMBER_OF_SOCKETS; i++) { if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor)) { // Only for testing memset (Buffer, 0, 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); printf ("\n\n%s\n", Buffer); //answer = Buffer; 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 < NUMBER_OF_SOCKETS; 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) { fprintf ( stderr, "Error: Config Error. \n File not found %s", path); 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(NUMBER_OF_SOCKETS, SocketDescriptor); cmd_send ("sra 10\n", SocketDescriptor[0]); cmd_send ("t\n", SocketDescriptor[0]); sleep (1); FD_ZERO (&ReadFileDescriptor); for (int i = 0; i < NUMBER_OF_SOCKETS; 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 < NUMBER_OF_SOCKETS; 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; }