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