source: fact/tools/FAD/simple_daq/simple_daq.cpp@ 10113

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