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

Last change on this file since 248 was 248, checked in by dneise, 14 years ago
initial check in of FAD tools simple daq - simple command line interface to FAD will produce binary socket_x.dat files ddd_for_fad - simple GUI able to plot FAD raw data.
File size: 4.0 KB
Line 
1/********************************************************************\
2
3 Read 8 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
21int SocketDescriptor[8];
22
23int main(int argc, char *argv[])
24{
25 char Buffer[MAX_COM_SIZE];
26
27 // default server
28 char ServerName[IP_ADDR_LENGTH] = IP_ADDR_DEFAULT; // default IP-Address
29
30 int i, read_return;
31
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
43 if (argc > 1)
44 {
45 strncpy (ServerName, argv[1], IP_ADDR_LENGTH);
46 }
47
48 // Convert IP-Addr to binary
49 if (inet_pton (AF_INET, ServerName, &Serveripv4addr) <= 0)
50 {
51 printf ("Error: network address not valid\n");
52 exit_program (EXIT_FAILURE);
53 }
54
55 // Open sockets
56 for (i = 0; i < 8; i++)
57 {
58 if ((SocketDescriptor[i] = socket (PF_INET, SOCK_STREAM, 0)) == -1)
59 {
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);
79 exit_program (EXIT_FAILURE);
80 }
81 }
82
83 // Connect to server
84 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 else
97 {
98 printf ("Connected to %s:%d\n", ServerName, DAQPort + i);
99 }
100 }
101
102 signal (SIGPIPE, SIG_IGN); // Do not kill process if writing to closed socket
103 signal (SIGINT, int_handler); // Cleanup after SIGINT (CTRL-C)
104
105 // Main loop
106 while (true)
107 {
108 fflush (stdout);
109
110 FD_ZERO (&ReadFileDescriptor);
111
112 FD_SET(STDIN_FILENO, &ReadFileDescriptor);
113
114 for (i = 0; i < 8; i++)
115 {
116 FD_SET (SocketDescriptor[i], &ReadFileDescriptor);
117 }
118
119 // Wait for data from sockets
120 if (select (((int) max_fd) + 1, &ReadFileDescriptor, NULL, NULL, NULL) == -1)
121 {
122 perror ("Error with select()\n");
123 break;
124 }
125
126 memset (Buffer, 0, sizeof (Buffer));
127
128 // Data from STDIN
129 if (FD_ISSET (STDIN_FILENO, &ReadFileDescriptor))
130 {
131 fgets (Buffer, MAX_COM_SIZE, stdin);
132 // Send command to socket 0
133 cmd_send (Buffer, SocketDescriptor[0]);
134 }
135 // Data from sockets
136 else
137 {
138 // Check all sockets
139 for (i = 0; i < 8; i++)
140 {
141 if (FD_ISSET (SocketDescriptor[i], &ReadFileDescriptor))
142 {
143 // Only for testing
144 memset (Buffer, 0xAA, sizeof (Buffer));
145 if ((read_return = read (SocketDescriptor[i], Buffer, MAX_COM_SIZE)) == 0)
146 {
147 printf ("Error: Server not existing anymore, exiting...\n");
148 exit_program (EXIT_FAILURE);
149 }
150
151 if (read_return > 0)
152 {
153 printf ("Socket [%d]: Read %d Bytes\n", i, read_return);
154
155 fwrite (Buffer, 1, (size_t) read_return, fhandle[i]);
156 // Important!!!
157 fflush (fhandle[i]);
158 }
159 }
160 }
161 }
162 } // while (TRUE)
163
164 exit (EXIT_SUCCESS);
165}
166
167// close sockets and exit
168void exit_program (int exit_status)
169{
170 int i;
171
172 printf ("\nClosing Sockets...");
173 for (i = 0; i < 8; i++)
174 {
175 close (SocketDescriptor[i]);
176 }
177 printf (" done\n");
178
179 exit (exit_status);
180}
181
182// SIGINT
183void int_handler (int sig)
184{
185 exit_program (EXIT_SUCCESS);
186}
Note: See TracBrowser for help on using the repository browser.