00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <stdlib.h>
00032 #include <process.h>
00033 #include <conio.h>
00034
00035 #include "rs232.h"
00036 #include "error_management.h"
00037
00038 #define DEFAULT_COM_PORT "COM1"
00039 #define DEFAULT_BAUD_RATE 9600
00040 #define DEFAULT_BITS_NUMBER 8
00041 #define DEFAULT_PARITY RS232_PARITY_NOPARITY
00042 #define DEFAULT_STOPBIT RS232_STOP_BIT_ONE
00043
00044 #define MAX_ARGS 10
00045
00046 static volatile int sec = 0;
00047
00048 static int refresh_every_n_lines = 0;
00049 static int refresh_every_n_bytes = 0;
00050 static int iskbhit = FALSE;
00051 static HANDLE hthread1, hthread2;
00052
00053
00054 DWORD WINAPI sec_int(void *arg)
00055 {
00056 while(1)
00057 {
00058 sec++;
00059 Sleep(1000);
00060 }
00061 return 0;
00062 }
00063
00064
00065 DWORD WINAPI kb_event(void *arg)
00066 {
00067 while(1)
00068 {
00069 if (!iskbhit)
00070 iskbhit = (_getch())?TRUE:FALSE;
00071 }
00072 return 0;
00073 }
00074
00075
00076 void get_data(char *_filename, char *_port, int br, int nbits, int parity, int stopbits)
00077 {
00078 char c;
00079 int n;
00080 FILE *_file = NULL;
00081 int n_line = 0;
00082 int n_bytes = 0;
00083 int n_byte = 0;
00084 DWORD dwGenericThread;
00085
00086
00087 ASSERT(rs232_open(_port, br, nbits, parity, stopbits));
00088
00089
00090 if (_filename)
00091 {
00092
00093 ASSERT((_file = fopen(_filename, "w")) != NULL);
00094 fclose(_file);
00095 }
00096
00097
00098 hthread1 = CreateThread(NULL, 100, sec_int, NULL, 0, &dwGenericThread);
00099
00100
00101
00102 do
00103 {
00104 n = 0;
00105 sec = 0;
00106 ASSERT(rs232_read(&c, 1, &n));
00107 }while(n == 1 && sec < 1);
00108
00109 printf(".");
00110 fflush(stdout);
00111
00112
00113 do
00114 {
00115 sleep(1);
00116 n = 0;
00117 ASSERT(rs232_read(&c, 1, &n));
00118 }while(n != 1);
00119
00120 printf(".");
00121 fflush(stdout);
00122
00123
00124 n_line = refresh_every_n_lines;
00125 n_byte = refresh_every_n_bytes;
00126 sec = 0;
00127
00128
00129 if (_filename)
00130 ASSERT(_file = fopen(_filename, "wb"));
00131
00132
00133 hthread2 = CreateThread(NULL, 100, kb_event, NULL, 0, &dwGenericThread);
00134
00135 iskbhit = FALSE;
00136 do
00137 {
00138
00139 if (_filename)
00140 {
00141
00142 n_bytes++;
00143
00144 n_byte--;
00145
00146 printf("\rReceived: %i Byte(s) (%i b/s) <Press ENTER to stop>", n_bytes, (sec)?(n_bytes/sec):0);
00147 fflush(stdout);
00148
00149
00150 if (!_file && (refresh_every_n_lines || refresh_every_n_bytes))
00151 ASSERT(_file = fopen(_filename, "a+b"));
00152
00153
00154 fputc(c, _file);
00155
00156
00157 if (c == '\n' && refresh_every_n_lines)
00158 n_line--;
00159
00160
00161 if (!n_line && refresh_every_n_lines)
00162 {
00163 fclose(_file);
00164 _file = NULL;
00165 n_line = refresh_every_n_lines;
00166 }
00167
00168 else if (!n_byte && refresh_every_n_bytes)
00169 {
00170 fclose(_file);
00171 _file = NULL;
00172 n_byte = refresh_every_n_bytes;
00173 }
00174 }
00175
00176 else
00177 putchar(c);
00178 n = 0;
00179
00180 ASSERT(rs232_read(&c, 1, &n));
00181
00182 }while(n == 1 && !iskbhit);
00183
00184
00185 if (_file)
00186 fclose(_file);
00187
00188
00189 rs232_close();
00190 }
00191
00192
00193 void print_help()
00194 {
00195 printf("Usage: DataGet [-l number] [-b number] [-f filename] [-h] [%%PORT [%%BAUD_RATE [%%BITS_PER_CHAR [%%PARITY [%%STOPBIT]]]]]\n\r"
00196 "PARITY = 0: NO PARITY\n\r"
00197 " 1: PARITY EVEN\n\r"
00198 " 2: PARITY ODD\n\r"
00199 "STOPBIT = 1: ONE STOPBIT\n\r"
00200 " 2: ONE POINT FIVE STOPBIT\n\r"
00201 " 3: TWO STOPBITS\n\r"
00202 "OPTIONS:\n\r"
00203 " -l number Refresh the file once \"number\" lines have been received.\n\r"
00204 " -b number Refresh the file once \"number\" bytes have been received.\n\r"
00205 " -f filename Print the results in a file.\n\r"
00206 " -h Display these lines.\n\r"
00207 "By default, if no argument is passed to this program, the acquisition will be done on the " DEFAULT_COM_PORT ", at 9600 bauds (8-N-1).\n\r"
00208 );
00209 }
00210
00211 int main(int argc, char *argv[])
00212 {
00213 int parity[] = {RS232_PARITY_NOPARITY, RS232_PARITY_EVEN,RS232_PARITY_ODD};
00214 int stopbit[] = {0, RS232_STOP_BIT_ONE, RS232_STOP_BIT_ONE5, RS232_STOP_BIT_TWO};
00215 int i, argc_n;
00216 static char *argv_n[MAX_ARGS];
00217 char *_filename = NULL;
00218
00219 argc_n = 0;
00220
00221 for(i=1; i<argc; i++)
00222 {
00223
00224 if (argv[i][0] == '-')
00225 {
00226
00227 switch(argv[i][1])
00228 {
00229
00230 case 'l':
00231 if (i+1 < argc)
00232 refresh_every_n_lines = atoi(argv[++i]);
00233 else
00234 {
00235 print_help();
00236 return 0;
00237 }
00238 break;
00239
00240 case 'b':
00241 if (i+1 < argc)
00242 refresh_every_n_bytes = atoi(argv[++i]);
00243 else
00244 {
00245 print_help();
00246 return 0;
00247 }
00248 break;
00249
00250 case 'f':
00251 if (i+1 < argc)
00252 _filename = argv[++i];
00253 else
00254 {
00255 print_help();
00256 return 0;
00257 }
00258 break;
00259
00260 case 'h':
00261 print_help();
00262 return 0;
00263
00264 default:
00265 printf("Unknown option '-%c'\r\n", argv[i][1]);
00266 print_help();
00267 return 0;
00268 }
00269 }
00270
00271 else
00272 {
00273 if (argc_n < MAX_ARGS)
00274 {
00275 argv_n[argc_n] = argv[i];
00276 argc_n++;
00277 }
00278 }
00279 }
00280
00281
00282 if (argc_n == 0)
00283 get_data(_filename, DEFAULT_COM_PORT, DEFAULT_BAUD_RATE, DEFAULT_BITS_NUMBER, DEFAULT_PARITY, DEFAULT_STOPBIT);
00284 else if (argc_n == 1)
00285 get_data(_filename, argv_n[0], DEFAULT_BAUD_RATE, DEFAULT_BITS_NUMBER, DEFAULT_PARITY, DEFAULT_STOPBIT);
00286 else if (argc_n == 2)
00287 get_data(_filename, argv_n[0], atoi(argv_n[1]), DEFAULT_BITS_NUMBER, DEFAULT_PARITY, DEFAULT_STOPBIT);
00288 else if (argc_n == 3)
00289 get_data(_filename, argv_n[0], atoi(argv_n[1]), atoi(argv_n[2]), DEFAULT_PARITY, DEFAULT_STOPBIT);
00290 else if (argc_n == 4)
00291 get_data(_filename, argv_n[0], atoi(argv_n[1]), atoi(argv_n[2]), parity[atoi(argv_n[3])], DEFAULT_STOPBIT);
00292 else if (argc_n == 5)
00293 get_data(_filename, argv_n[0], atoi(argv_n[1]), atoi(argv_n[2]), parity[atoi(argv_n[3])], stopbit[atoi(argv_n[4])]);
00294 else
00295 print_help();
00296
00297 TerminateThread(hthread1, 0);
00298 TerminateThread(hthread2, 0);
00299
00300 return 0;
00301 }
00302