- Timestamp:
- 07/22/10 08:07:51 (14 years ago)
- Location:
- tools/FAD
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/FAD/simple_daq/Makefile
r248 r256 2 2 CFLAGS=-c -Wall 3 3 LDFLAGS= 4 SOURCES=simple_daq.cpp cmd_send.cpp 4 SOURCES=simple_daq.cpp cmd_send.cpp ../SocketFunctions/SocketFunctions.cpp 5 5 OBJECTS=$(SOURCES:.cpp=.o) 6 6 EXECUTABLE=simple_daq -
tools/FAD/simple_daq/simple_daq.cpp
r248 r256 1 1 /********************************************************************\ 2 2 3 Read 8Channels from FAD-Board3 Read SOCKETS_PER_FAD Channels from FAD-Board 4 4 Write Commands to Socket 0, Commands must be (n * 16) Bit long 5 5 … … 18 18 #endif 19 19 #include "simple_daq.h" 20 #include "../SocketFunctions/SocketFunctions.h" 20 21 21 int SocketDescriptor[8]; 22 23 int *SocketDescriptor; 22 24 23 25 int main(int argc, char *argv[]) 24 26 { 27 // what kind of buffer is this? 25 28 char Buffer[MAX_COM_SIZE]; 26 29 27 // default server 28 char ServerName[IP_ADDR_LENGTH] = IP_ADDR_DEFAULT; // default IP-Address 30 int read_return; 29 31 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]; 31 37 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 43 43 if (argc > 1) 44 44 { 45 strncpy (ServerName, argv[1], IP_ADDR_LENGTH); 45 conf = getConfig(argv[1]); 46 } else { 47 conf = getConfig(CONFIG_FILE_PATH); 46 48 } 47 49 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) { 52 53 exit_program (EXIT_FAILURE); 53 54 } 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 } 54 63 55 // Open sockets56 for (i = 0; i < 8; i++)64 // Open files for output 65 for (int i = 0; i < SOCKETS_PER_FAD; i++) 57 66 { 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) 59 69 { 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); 79 71 exit_program (EXIT_FAILURE); 80 72 } 81 73 } 82 74 83 // Connect to server84 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 else97 {98 printf ("Connected to %s:%d\n", ServerName, DAQPort + i);99 }100 }101 75 102 76 signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket … … 108 82 fflush (stdout); 109 83 110 FD_ZERO (&ReadFileDescriptor); 111 84 FD_ZERO (&ReadFileDescriptor); 112 85 FD_SET(STDIN_FILENO, &ReadFileDescriptor); 113 114 for (i = 0; i < 8; i++) 86 for (int i = 0; i < SOCKETS_PER_FAD; i++) 115 87 { 116 88 FD_SET (SocketDescriptor[i], &ReadFileDescriptor); … … 137 109 { 138 110 // Check all sockets 139 for (i = 0; i < 8; i++)111 for (int i = 0; i < SOCKETS_PER_FAD; i++) 140 112 { 141 113 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor)) … … 153 125 printf ("Socket [%d]: Read %d Bytes\n", i, read_return); 154 126 155 fwrite (Buffer, 1, (size_t) read_return, fhandle[i]);127 fwrite (Buffer, 1, (size_t) read_return, outfile[i]); 156 128 // Important!!! 157 fflush ( fhandle[i]);129 fflush (outfile[i]); 158 130 } 159 131 } … … 167 139 // close sockets and exit 168 140 void exit_program (int exit_status) 169 { 170 int i; 171 141 { 172 142 printf ("\nClosing Sockets..."); 173 for (i = 0; i < 8; i++)143 for (int i = 0; i < SOCKETS_PER_FAD; i++) 174 144 { 175 145 close (SocketDescriptor[i]); … … 185 155 exit_program (EXIT_SUCCESS); 186 156 } 157 158 159 // note: verbose is not used, but anyway defined. 160 SimpleDaqConfiguration *getConfig (char *path, int verbose) { 161 FILE* ConfigFile; 162 // try to open config file 163 // if not exists return NULL 164 ConfigFile = fopen (path, "r"); 165 if (ConfigFile == NULL) { 166 return NULL; 167 } 168 169 //create SimpleDaqConfiguration 170 SimpleDaqConfiguration *conf = new SimpleDaqConfiguration(); 171 172 // read config data from file and fill in SimpleDaqConfiguration 173 fscanf( ConfigFile , "%s" , conf->FADIPAddress ); 174 fscanf( ConfigFile , "%s" , conf->outfilepath ); 175 fscanf( ConfigFile , "%s" , conf->outfilename ); 176 fscanf( ConfigFile , "%s" , conf->outfileext ); 177 178 return conf; 179 } -
tools/FAD/simple_daq/simple_daq.h
r248 r256 13 13 #define MAX_COM_SIZE 32000 14 14 #define IP_ADDR_LENGTH 16 15 //#define IP_ADDR_DEFAULT "192.33.99.225"16 #define IP_ADDR_DEFAULT "129.217.160.119"17 15 18 16 #define FIRST_DAQPORT 5000 17 #define SOCKETS_PER_FAD 8 19 18 20 19 // Commands for FAD … … 43 42 #define MAX_DACVAL 65535 44 43 44 #define CONFIG_FILE_PATH "config.txt" 45 46 class SimpleDaqConfiguration { 47 public: 48 char FADIPAddress[IP_ADDR_LENGTH]; 49 char outfilepath[200]; 50 char outfilename[50]; 51 char outfileext[10]; 52 }; 53 45 54 void cmd_send (char* Buffer, int Socket); // Send commands to socket 46 55 void int_handler (int sig); // Handle signal SIGINT (CTRL-C) 47 56 void exit_program (int exit_status); // Cleanup and exit 57 SimpleDaqConfiguration *getConfig (char *path, int verbose = 0); 48 58 49 59 #endif /*SOCKETCLIENT_H_*/
Note:
See TracChangeset
for help on using the changeset viewer.