source: tools/FAD/simple_daq/simple_daq.cpp@ 5452

Last change on this file since 5452 was 261, checked in by dneise, 14 years ago
FAD SPI SLCK can be controled via sclkon & sclkoff see help
File size: 4.2 KB
Line 
1/********************************************************************\
2
3 Read SOCKETS_PER_FAD Channels from FAD-Board
4 Write Commands to Socket 0, Commands must be (n * 16) Bit long
5
6 Usage: SocketClient [IP-Address]
7
8 kw, 05.10
9
10 based on:
11 Generic example for remote control via a socket interface
12
13 Oliver Grimm, March 2009
14
15\********************************************************************/
16#ifndef PATH_MAX
17#define PATH_MAX 1000
18#endif
19#include "simple_daq.h"
20#include "../SocketFunctions/SocketFunctions.h"
21
22
23int *SocketDescriptor;
24
25int main(int argc, char *argv[])
26{
27 // what kind of buffer is this?
28 char Buffer[MAX_COM_SIZE];
29
30 int read_return;
31
32 fd_set ReadFileDescriptor;
33 int max_fd = 0;
34
35 FILE* outfile[SOCKETS_PER_FAD];
36 char outfilename[PATH_MAX];
37
38
39 SimpleDaqConfiguration *conf;
40 // Get configuration file path from command line
41 // Get configurarion from configurationfile.
42 // such as FAD IP Adress
43 if (argc > 1)
44 {
45 conf = getConfig(argv[1]);
46 } else {
47 conf = getConfig(CONFIG_FILE_PATH);
48 }
49
50 // Open sockets
51 SocketDescriptor = OpenSockets(SOCKETS_PER_FAD);
52 if (SocketDescriptor == NULL) {
53 exit_program (EXIT_FAILURE);
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 }
63
64 // Open files for output
65 for (int i = 0; i < SOCKETS_PER_FAD; i++)
66 {
67 sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
68 if ((outfile[i] = fopen (outfilename, "w")) == NULL)
69 {
70 printf ("Error: Could not open file %s\n", outfilename);
71 exit_program (EXIT_FAILURE);
72 }
73 }
74
75
76 signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
77 signal (SIGINT, int_handler); // Cleanup after SIGINT (CTRL-C)
78
79 // Main loop
80 while (true)
81 {
82 fflush (stdout);
83
84 FD_ZERO (&ReadFileDescriptor);
85 FD_SET(STDIN_FILENO, &ReadFileDescriptor);
86 for (int i = 0; i < SOCKETS_PER_FAD; i++)
87 {
88 FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
89 }
90
91 // Wait for data from sockets
92 if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
93 {
94 perror ("Error with select()\n");
95 break;
96 }
97
98 memset (Buffer, 0, sizeof (Buffer));
99
100 // Data from STDIN
101 if (FD_ISSET (STDIN_FILENO, &ReadFileDescriptor))
102 {
103 fgets (Buffer, MAX_COM_SIZE, stdin);
104 // Send command to socket 0
105 cmd_send (Buffer, SocketDescriptor[0]);
106 }
107 // Data from sockets
108 else
109 {
110 // Check all sockets
111 for (int i = 0; i < SOCKETS_PER_FAD; i++)
112 {
113 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
114 {
115 // Only for testing
116 memset (Buffer, 0xAA, sizeof (Buffer));
117 if ((read_return = read (SocketDescriptor[i], Buffer, MAX_COM_SIZE)) == 0)
118 {
119 printf ("Error: Server not existing anymore, exiting...\n");
120 exit_program (EXIT_FAILURE);
121 }
122
123 if (read_return > 0)
124 {
125 printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
126
127 fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
128 // Important!!!
129 fflush (outfile[i]);
130 }
131 }
132 }
133 }
134 } // while (TRUE)
135
136 exit (EXIT_SUCCESS);
137}
138
139// close sockets and exit
140void exit_program (int exit_status)
141{
142 printf ("\nClosing Sockets...");
143 for (int i = 0; i < SOCKETS_PER_FAD; i++)
144 {
145 close (SocketDescriptor[i]);
146 }
147 printf (" done\n");
148
149 exit (exit_status);
150}
151
152// SIGINT
153void int_handler (int sig)
154{
155 exit_program (EXIT_SUCCESS);
156}
157
158
159// note: verbose is not used, but anyway defined.
160SimpleDaqConfiguration *getConfig (const 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 TracBrowser for help on using the repository browser.