Ignore:
Timestamp:
07/22/10 08:07:51 (14 years ago)
Author:
dneise
Message:
simple daq now opens outfiles in path given in config.txt
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/FAD/simple_daq/simple_daq.cpp

    r248 r256  
    11/********************************************************************\
    22
    3   Read 8 Channels from FAD-Board
     3  Read SOCKETS_PER_FAD Channels from FAD-Board
    44  Write Commands to Socket 0, Commands must be (n * 16) Bit long
    55 
     
    1818#endif
    1919#include "simple_daq.h"
     20#include "../SocketFunctions/SocketFunctions.h"
    2021
    21 int SocketDescriptor[8];
     22
     23int *SocketDescriptor;
    2224
    2325int main(int argc, char *argv[])
    2426{
     27        // what kind of buffer is this?
    2528        char Buffer[MAX_COM_SIZE];
    2629       
    27         // default server
    28         char ServerName[IP_ADDR_LENGTH] = IP_ADDR_DEFAULT; // default IP-Address
     30        int read_return;
    2931
    30         int i, read_return;
     32        fd_set ReadFileDescriptor;
     33        int max_fd = 0;
     34       
     35        FILE* outfile[SOCKETS_PER_FAD];
     36        char outfilename[PATH_MAX];
    3137
    32         int DAQPort = FIRST_DAQPORT;
    33         struct sockaddr_in SocketAddress[8];
    34         struct in_addr Serveripv4addr;
    35         fd_set ReadFileDescriptor;
    36 
    37         FILE* fhandle[8];
    38         char fname[PATH_MAX];
    39 
    40         int max_fd = 0;
    41 
    42         // Get IP-Address from command line
     38       
     39                SimpleDaqConfiguration *conf;
     40        // Get configuration file path from command line
     41        // Get configurarion from configurationfile.
     42        // such as FAD IP Adress
    4343        if (argc > 1)
    4444        {
    45                 strncpy (ServerName, argv[1], IP_ADDR_LENGTH);
     45                conf = getConfig(argv[1]);
     46        } else {
     47                conf = getConfig(CONFIG_FILE_PATH);
    4648        }
    4749       
    48         // Convert IP-Addr to binary
    49         if (inet_pton (AF_INET, ServerName, &Serveripv4addr) <= 0)
    50         {
    51                 printf ("Error: network address not valid\n");
     50        // Open sockets
     51        SocketDescriptor = OpenSockets(SOCKETS_PER_FAD);
     52        if (SocketDescriptor == NULL) {
    5253                exit_program (EXIT_FAILURE);
    5354        }
     55        max_fd = GetMaxFileDescriptor(SOCKETS_PER_FAD, SocketDescriptor);
     56       
     57        // Connect to server
     58        if( Connect2Server(     SocketDescriptor, SOCKETS_PER_FAD, FIRST_DAQPORT, conf->FADIPAddress, 1) != SOCKETS_PER_FAD) {
     59                        // Connect2Server prints some error messages in case of exceptions...
     60                        printf ("Error in Connect2Server()\n");
     61                        exit_program (EXIT_FAILURE);
     62        }
    5463
    55         // Open sockets
    56         for (i = 0; i < 8; i++)
     64        // Open files for output
     65        for (int i = 0; i < SOCKETS_PER_FAD; i++)
    5766        {
    58                 if ((SocketDescriptor[i] = socket (PF_INET, SOCK_STREAM, 0)) == -1)
     67                sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
     68                if ((outfile[i] = fopen (outfilename, "w")) == NULL)
    5969                {
    60                         printf ("Error: Could not open socket Nr.: %d\n", i);
    61                         exit_program (EXIT_FAILURE);
    62                 }
    63                 else
    64                 {
    65                         if (SocketDescriptor[i] > max_fd)
    66                         {
    67                                 max_fd = SocketDescriptor[i];
    68                         }
    69                 }
    70         }
    71        
    72         // Open files for output
    73         for (i = 0; i < 8; i++)
    74         {
    75                 sprintf (fname, "socket-%d.dat", i);
    76                 if ((fhandle[i] = fopen (fname, "w")) == NULL)
    77                 {
    78                         printf ("Error: Could not open file %s\n", fname);
     70                        printf ("Error: Could not open file %s\n", outfilename);
    7971                        exit_program (EXIT_FAILURE);
    8072                }
    8173        }
    8274
    83         // Connect to server
    84         printf ("Trying to connect to %s...\n", ServerName);
    85         for (i = 0; i < 8; i++)
    86         {
    87                 SocketAddress[i].sin_family = PF_INET;
    88                 SocketAddress[i].sin_port = htons ((unsigned short) DAQPort + i);
    89                 SocketAddress[i].sin_addr = Serveripv4addr;
    90                
    91                 if (connect (SocketDescriptor[i], (struct sockaddr *) &SocketAddress[i], sizeof (SocketAddress[i])) == -1)
    92                 {
    93                         printf ("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort + i);
    94                         exit_program (EXIT_FAILURE);
    95                 }
    96                 else
    97                 {
    98                         printf ("Connected to %s:%d\n", ServerName, DAQPort + i);
    99                 }
    100         }
    10175
    10276        signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
     
    10882                fflush (stdout);
    10983
    110                 FD_ZERO (&ReadFileDescriptor);
    111                
     84                FD_ZERO (&ReadFileDescriptor);         
    11285                FD_SET(STDIN_FILENO, &ReadFileDescriptor);
    113                
    114                 for (i = 0; i < 8; i++)
     86                for (int i = 0; i < SOCKETS_PER_FAD; i++)
    11587                {
    11688                        FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
     
    137109                {
    138110                        // Check all sockets
    139                         for (i = 0; i < 8; i++)
     111                        for (int i = 0; i < SOCKETS_PER_FAD; i++)
    140112                        {
    141113                                if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
     
    153125                                                printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
    154126                                       
    155                                                 fwrite (Buffer, 1, (size_t) read_return, fhandle[i]);
     127                                                fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
    156128                                                // Important!!!
    157                                                 fflush (fhandle[i]);
     129                                                fflush (outfile[i]);
    158130                                        }
    159131                                }
     
    167139// close sockets and exit
    168140void exit_program (int exit_status)
    169 {
    170         int i;
    171        
     141{       
    172142        printf ("\nClosing Sockets...");
    173         for (i = 0; i < 8; i++)
     143        for (int i = 0; i < SOCKETS_PER_FAD; i++)
    174144        {
    175145                close (SocketDescriptor[i]);
     
    185155        exit_program (EXIT_SUCCESS);
    186156}
     157
     158
     159// note: verbose is not used, but anyway defined.
     160SimpleDaqConfiguration *getConfig (char *path, int verbose) {
     161FILE* ConfigFile;
     162// try to open config file
     163// if not exists return NULL
     164ConfigFile = fopen (path, "r");
     165if (ConfigFile == NULL) {
     166        return NULL;
     167}
     168
     169//create SimpleDaqConfiguration
     170SimpleDaqConfiguration *conf = new SimpleDaqConfiguration();
     171
     172// read config data from file and fill in SimpleDaqConfiguration
     173fscanf( ConfigFile , "%s" , conf->FADIPAddress );
     174fscanf( ConfigFile , "%s" , conf->outfilepath );
     175fscanf( ConfigFile , "%s" , conf->outfilename );
     176fscanf( ConfigFile , "%s" , conf->outfileext );
     177
     178return conf;
     179}
Note: See TracChangeset for help on using the changeset viewer.