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

Last change on this file since 9907 was 9907, checked in by neise, 14 years ago
fad_init() doesn't work propoerly without some sleep(1)s. I have to work it out, but it works now ... I hope.
File size: 5.3 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 init_fad();
25int *SocketDescriptor;
26using namespace std;
27
28int main(int argc, char *argv[])
29{
30 // what kind of buffer is this?
31 char Buffer[MAX_COM_SIZE];
32
33 int read_return;
34
35 fd_set ReadFileDescriptor;
36 int max_fd = 0;
37
38 FILE* outfile[SOCKETS_PER_FAD];
39 char outfilename[PATH_MAX];
40
41
42 SimpleDaqConfiguration *conf;
43 // Get configuration file path from command line
44 // Get configurarion from configurationfile.
45 // such as FAD IP Adress
46 if (argc > 1)
47 {
48 conf = getConfig(argv[1]);
49 } else {
50 conf = getConfig(CONFIG_FILE_PATH);
51 }
52
53 // Open sockets
54 SocketDescriptor = OpenSockets(SOCKETS_PER_FAD);
55 if (SocketDescriptor == NULL) {
56 exit_program (EXIT_FAILURE);
57 }
58 max_fd = GetMaxFileDescriptor(SOCKETS_PER_FAD, SocketDescriptor);
59
60 // Connect to server
61 if( Connect2Server( SocketDescriptor, SOCKETS_PER_FAD, FIRST_DAQPORT, conf->FADIPAddress, 1) != SOCKETS_PER_FAD) {
62 // Connect2Server prints some error messages in case of exceptions...
63 printf ("Error in Connect2Server()\n");
64 exit_program (EXIT_FAILURE);
65 }
66
67 // Open files for output
68 for (int i = 0; i < SOCKETS_PER_FAD; i++)
69 {
70 sprintf (outfilename, "%s/%s-%d.%s", conf->outfilepath, conf->outfilename, i, conf->outfileext);
71 if ((outfile[i] = fopen (outfilename, "w")) == NULL)
72 {
73 printf ("Error: Could not open file %s\n", outfilename);
74 exit_program (EXIT_FAILURE);
75 }
76 }
77 init_fad();
78
79
80 signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
81 signal (SIGINT, int_handler); // Cleanup after SIGINT (CTRL-C)
82
83// init_fad();
84// fflush (stdin);
85 // Main loop
86 while (true)
87 {
88 fflush (stdout);
89
90 FD_ZERO (&ReadFileDescriptor);
91 FD_SET(STDIN_FILENO, &ReadFileDescriptor);
92 for (int i = 0; i < SOCKETS_PER_FAD; i++)
93 {
94 FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
95 }
96
97 // Wait for data from sockets
98 if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
99 {
100 perror ("Error with select()\n");
101 break;
102 }
103
104 memset (Buffer, 0, sizeof (Buffer));
105
106
107 // init board -- should be switchable
108 // with a commandline switch, but ...
109 // TODO
110
111
112 // Data from STDIN
113 if (FD_ISSET (STDIN_FILENO, &ReadFileDescriptor))
114 {
115 fgets (Buffer, MAX_COM_SIZE, stdin);
116 // Send command to socket 0
117 cmd_send (Buffer, SocketDescriptor[0]);
118 }
119 // Data from sockets
120 else
121 {
122 // Check all sockets
123 for (int i = 0; i < SOCKETS_PER_FAD; i++)
124 {
125 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
126 {
127 // Only for testing
128 memset (Buffer, 0xAA, sizeof (Buffer));
129 if ((read_return = read (SocketDescriptor[i], Buffer, MAX_COM_SIZE)) == 0)
130 {
131 printf ("Error: Server not existing anymore, exiting...\n");
132 exit_program (EXIT_FAILURE);
133 }
134
135 if (read_return > 0)
136 {
137 printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
138
139 fwrite (Buffer, 1, (size_t) read_return, outfile[i]);
140 // Important!!!
141 fflush (outfile[i]);
142 }
143 }
144 }
145 }
146 } // while (TRUE)
147
148 exit (EXIT_SUCCESS);
149}
150
151// close sockets and exit
152void exit_program (int exit_status)
153{
154 printf ("\nClosing Sockets...");
155 for (int i = 0; i < SOCKETS_PER_FAD; i++)
156 {
157 close (SocketDescriptor[i]);
158 }
159 printf (" done\n");
160
161
162
163 exit (exit_status);
164}
165
166// SIGINT
167void int_handler (int sig)
168{
169 exit_program (EXIT_SUCCESS);
170}
171
172
173// note: verbose is not used, but anyway defined.
174SimpleDaqConfiguration *getConfig (const char *path, int verbose) {
175FILE* ConfigFile;
176// try to open config file
177// if not exists return NULL
178ConfigFile = fopen (path, "r");
179if (ConfigFile == NULL) {
180 return NULL;
181}
182
183//create SimpleDaqConfiguration
184SimpleDaqConfiguration *conf = new SimpleDaqConfiguration();
185
186// read config data from file and fill in SimpleDaqConfiguration
187fscanf( ConfigFile , "%s" , conf->FADIPAddress );
188fscanf( ConfigFile , "%s" , conf->outfilepath );
189fscanf( ConfigFile , "%s" , conf->outfilename );
190fscanf( ConfigFile , "%s" , conf->outfileext );
191
192return conf;
193}
194
195int init_fad(){
196 cmd_send ("ds\n", SocketDescriptor[0]);
197 cmd_send ("dd\n", SocketDescriptor[0]);
198sleep (1);
199 cmd_send ("sra 10\n", SocketDescriptor[0]);
200 cmd_send ("sa 44 29\n", SocketDescriptor[0]);
201sleep (1);
202 cmd_send ("t\n", SocketDescriptor[0]);
203sleep (1);
204 cmd_send ("sa 44 30\n", SocketDescriptor[0]);
205sleep (1);
206 cmd_send ("t\n", SocketDescriptor[0]);
207sleep (1);
208 cmd_send ("sa 44 0\n", SocketDescriptor[0]);
209sleep (1);
210 cmd_send ("t\n", SocketDescriptor[0]);
211// cmd_send ("sclkoff\n", SocketDescriptor[0]);
212 cmd_send ("de\n", SocketDescriptor[0]);
213 cmd_send ("dr\n", SocketDescriptor[0]);
214 cmd_send ("sra 1024\n", SocketDescriptor[0]);
215 EmptySockets(SocketDescriptor, 8, 750000L);
216
217 printf ( "\n\n FAD initialised. \n "
218 "ROI is 1024. \n"
219 "DRS shift registers are initialised.\n"
220 "DRS is up and running.\n");
221
222return 0;
223}
224
Note: See TracBrowser for help on using the repository browser.