source: fact/tools/FSC/simple_daq/simple_daq.cpp@ 16840

Last change on this file since 16840 was 10918, checked in by neise, 13 years ago
File size: 7.6 KB
Line 
1/********************************************************************\
2
3 Read NUMBER_OF_SOCKETS 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 "FSCFormat.h"
21#include "../SocketFunctions/SocketFunctions.h"
22
23#include "iostream"
24
25int revision = atoi(REVISION) * (strchr(REVISION,'M')==NULL ? 1:-1);
26int fad_firmware_revision;
27
28int init_fad();
29int get_firmware_revision();
30int *SocketDescriptor;
31using namespace std;
32
33
34
35int main(int argc, char *argv[])
36{
37 // what kind of buffer is this?
38 char Buffer[MAX_COM_SIZE];
39
40 int read_return;
41
42 fd_set ReadFileDescriptor;
43 int max_fd = 0;
44
45 FILE* outfile[NUMBER_OF_SOCKETS];
46 char outfilename[PATH_MAX];
47
48 FSC_STATUS *answer;
49
50 SimpleDaqConfiguration *conf;
51 // Get configuration file path from command line
52 // Get configurarion from configurationfile.
53 // such as IP Adress
54 if (argc > 1)
55 {
56 conf = getConfig(argv[1]);
57 } else {
58 conf = getConfig(CONFIG_FILE_PATH);
59 }
60 if (conf == NULL) {
61 fprintf ( stderr, "Error: Config Error. \n");
62 exit_program (EXIT_FAILURE);
63 }
64
65 // Open sockets
66 SocketDescriptor = OpenSockets(NUMBER_OF_SOCKETS);
67 if (SocketDescriptor == NULL) {
68 fprintf(stderr, "Error: While opening sockets. \n");
69 exit_program (EXIT_FAILURE);
70 }
71 max_fd = GetMaxFileDescriptor(NUMBER_OF_SOCKETS, SocketDescriptor);
72
73 // Connect to server
74 if( Connect2Server( SocketDescriptor, NUMBER_OF_SOCKETS, FIRST_DAQPORT, conf->FADIPAddress, 1) != NUMBER_OF_SOCKETS) {
75 // Connect2Server prints some error messages in case of exceptions...
76 printf ("Error in Connect2Server()\n");
77 exit_program (EXIT_FAILURE);
78 }
79
80 // Open files for output
81 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
82 {
83 sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
84 if ((outfile[i] = fopen (outfilename, "w")) == NULL)
85 {
86 printf ("Error: Could not open output-file %s\n", outfilename);
87 exit_program (EXIT_FAILURE);
88 }
89 }
90
91
92
93 //fad_firmware_revision = get_firmware_revision();
94 printf ("software revision is: %s or %d\n",REVISION,revision);
95 //printf ("firmware revision is: %d\n",fad_firmware_revision);
96
97
98
99 signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
100 signal (SIGINT, int_handler); // Cleanup after SIGINT (CTRL-C)
101
102 // Main loop
103 while (true)
104 {
105 fflush (stdout);
106
107 FD_ZERO (&ReadFileDescriptor);
108 FD_SET(STDIN_FILENO, &ReadFileDescriptor);
109 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
110 {
111 FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
112 }
113
114 // Wait for data from sockets
115 if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
116 {
117 perror ("Error with select()\n");
118 break;
119 }
120
121 memset (Buffer, 0, sizeof (Buffer));
122
123
124 // Data from STDIN
125 if (FD_ISSET (STDIN_FILENO, &ReadFileDescriptor))
126 {
127 fgets (Buffer, MAX_COM_SIZE, stdin);
128 // Send command to socket 0
129 cmd_send (Buffer, SocketDescriptor[0]);
130 }
131 // Data from sockets
132 else
133 {
134 // Check all sockets
135 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
136 {
137 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
138 {
139 // Only for testing
140 memset (Buffer, 0, sizeof (Buffer));
141 if ((read_return = read (SocketDescriptor[i], Buffer, MAX_COM_SIZE)) == 0)
142 {
143 printf ("Error: Server not existing anymore, exiting...\n");
144 exit_program (EXIT_FAILURE);
145 }
146
147 if (read_return > 0)
148 {
149 printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
150 printf ("\n\n%s\n", Buffer);
151
152 //answer = Buffer;
153
154
155
156 fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
157 // Important!!!
158 fflush (outfile[i]);
159 }
160 }
161 }
162 }
163 } // while (TRUE)
164
165 exit (EXIT_SUCCESS);
166}
167
168// close sockets and exit
169void exit_program (int exit_status)
170{
171 printf ("\nClosing Sockets...");
172 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
173 {
174 close (SocketDescriptor[i]);
175 }
176 printf (" done\n");
177
178
179
180 exit (exit_status);
181}
182
183// SIGINT
184void int_handler (int sig)
185{
186 exit_program (EXIT_SUCCESS);
187}
188
189
190// note: verbose is not used, but anyway defined.
191SimpleDaqConfiguration *getConfig (const char *path, int verbose) {
192FILE* ConfigFile;
193// try to open config file
194// if not exists return NULL
195ConfigFile = fopen (path, "r");
196if (ConfigFile == NULL) {
197 fprintf ( stderr, "Error: Config Error. \n File not found %s", path);
198 return NULL;
199}
200
201//create SimpleDaqConfiguration
202SimpleDaqConfiguration *conf = new SimpleDaqConfiguration();
203
204// read config data from file and fill in SimpleDaqConfiguration
205fscanf( ConfigFile , "%s" , conf->FADIPAddress );
206fscanf( ConfigFile , "%s" , conf->outfilepath );
207fscanf( ConfigFile , "%s" , conf->outfilename );
208fscanf( ConfigFile , "%s" , conf->outfileext );
209
210return conf;
211}
212
213int init_fad(){
214
215// current FSC needs no init procedure ... it's not smart enough :-)
216/*
217 cmd_send ("ds\n", SocketDescriptor[0]);
218 cmd_send ("dd\n", SocketDescriptor[0]);
219sleep (1);
220 cmd_send ("sd 0 21000\n", SocketDescriptor[0]);
221 cmd_send ("sd 1 0\n", SocketDescriptor[0]);
222 cmd_send ("sd 2 5000\n", SocketDescriptor[0]);
223 cmd_send ("sd 3 5000\n", SocketDescriptor[0]);
224 cmd_send ("sd 4 28800\n", SocketDescriptor[0]);
225 cmd_send ("sd 5 28800\n", SocketDescriptor[0]);
226 cmd_send ("sd 6 28800\n", SocketDescriptor[0]);
227 cmd_send ("sd 7 28800\n", SocketDescriptor[0]);
228
229sleep (1);
230 cmd_send ("sra 10\n", SocketDescriptor[0]);
231 cmd_send ("sa 44 29\n", SocketDescriptor[0]);
232sleep (1);
233 cmd_send ("t\n", SocketDescriptor[0]);
234sleep (1);
235 cmd_send ("sa 44 30\n", SocketDescriptor[0]);
236sleep (1);
237 cmd_send ("t\n", SocketDescriptor[0]);
238sleep (1);
239 cmd_send ("sa 44 0\n", SocketDescriptor[0]);
240sleep (1);
241 cmd_send ("t\n", SocketDescriptor[0]);
242 cmd_send ("sclkoff\n", SocketDescriptor[0]);
243 cmd_send ("de\n", SocketDescriptor[0]);
244 cmd_send ("dr\n", SocketDescriptor[0]);
245 cmd_send ("sra 1024\n", SocketDescriptor[0]);
246sleep (1);
247
248 EmptySockets(SocketDescriptor, 8, 750000L);
249
250
251
252 printf ( "\n\n FAD initialised. \n "
253 "ROI is 1024. \n"
254 "DRS shift registers are initialised.\n"
255 "DRS is up and running.\n"
256 "SPI SCLK was swithced off, TEMP readout makes no sense.\n"
257 "DAC changes will neighter work. SWITCH SCLK back on, when trying to change DAC values\n"
258 );
259*/
260return 0;
261}
262
263int get_firmware_revision(){
264
265fd_set ReadFileDescriptor;
266int max_fd = 0;
267
268int read_return;
269unsigned char buffer[984];
270unsigned short rev;
271
272max_fd = GetMaxFileDescriptor(NUMBER_OF_SOCKETS, SocketDescriptor);
273
274 cmd_send ("sra 10\n", SocketDescriptor[0]);
275 cmd_send ("t\n", SocketDescriptor[0]);
276 sleep (1);
277
278
279 FD_ZERO (&ReadFileDescriptor);
280 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
281 {
282 FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
283 }
284
285 // Wait for data from sockets
286 if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
287 {
288 perror ("Error with select()\n");
289
290 }
291
292
293 // Check all sockets
294 for (int i = 0; i < NUMBER_OF_SOCKETS; i++)
295 {
296 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
297 {
298 if ((read_return = read (SocketDescriptor[i], buffer, 984)) == 0)
299 {
300 printf ("Error: Server not existing anymore, exiting...\n");
301 return -1;
302 }
303 if (read_return > 0)
304 {
305 printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
306 }
307 }
308 }
309
310 rev = (unsigned char)buffer[4]<<8 | (unsigned char)buffer [5];
311
312return rev;
313}
314
Note: See TracBrowser for help on using the repository browser.