main.c File Reference

#include <stdlib.h>
#include <process.h>
#include <conio.h>
#include "rs232.h"
#include "error_management.h"

Go to the source code of this file.

Defines

#define DEFAULT_BAUD_RATE   9600
#define DEFAULT_BITS_NUMBER   8
#define DEFAULT_COM_PORT   "COM1"
#define DEFAULT_PARITY   RS232_PARITY_NOPARITY
#define DEFAULT_STOPBIT   RS232_STOP_BIT_ONE
#define MAX_ARGS   10

Functions

void get_data (char *_filename, char *_port, int br, int nbits, int parity, int stopbits)
DWORD WINAPI kb_event (void *arg)
int main (int argc, char *argv[])
void print_help ()
DWORD WINAPI sec_int (void *arg)

Variables

static HANDLE hthread1
static HANDLE hthread2
static int iskbhit = FALSE
static int refresh_every_n_bytes = 0
static int refresh_every_n_lines = 0
static volatile int sec = 0


Define Documentation

#define DEFAULT_BAUD_RATE   9600

Definition at line 39 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().

#define DEFAULT_BITS_NUMBER   8

Definition at line 40 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().

#define DEFAULT_COM_PORT   "COM1"

Definition at line 38 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main(), and print_help().

#define DEFAULT_PARITY   RS232_PARITY_NOPARITY

Definition at line 41 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().

#define DEFAULT_STOPBIT   RS232_STOP_BIT_ONE

Definition at line 42 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().

#define MAX_ARGS   10

Definition at line 44 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().


Function Documentation

void get_data ( char *  _filename,
char *  _port,
int  br,
int  nbits,
int  parity,
int  stopbits 
)

Definition at line 76 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

References ASSERT, DWORD, FALSE, hthread1, hthread2, iskbhit, kb_event(), refresh_every_n_bytes, refresh_every_n_lines, rs232_close(), rs232_open(), rs232_read(), sec, and sec_int().

Referenced by main(), and WindowProcedure().

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   // Make sure the port is well opened
00087   ASSERT(rs232_open(_port, br, nbits, parity, stopbits));
00088 
00089   // If the result has to be stored in a file, then initialize the file
00090   if (_filename)
00091   {
00092     // void the file
00093     ASSERT((_file = fopen(_filename, "w")) != NULL);
00094     fclose(_file);
00095   }
00096 
00097   // start a thread to measure the duration of the transfer
00098   hthread1 = CreateThread(NULL, 100, sec_int, NULL, 0, &dwGenericThread);
00099 
00100   // Wait until no character is recieved on the line
00101   // This is to make sure we are well synchronized on the line
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   // Wait until the transfer restart/start
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   // Initialization
00124   n_line = refresh_every_n_lines;
00125   n_byte = refresh_every_n_bytes;
00126   sec = 0;
00127 
00128   // Open the file for writing
00129   if (_filename)
00130      ASSERT(_file = fopen(_filename, "wb"));
00131 
00132   // start a thread to measure the duration of the transfer
00133   hthread2 = CreateThread(NULL, 100, kb_event, NULL, 0, &dwGenericThread);
00134 
00135   iskbhit = FALSE;
00136   do
00137   {
00138      // If the result has to be stored in a file...
00139      if (_filename)
00140      {
00141          // increment the number of byte read
00142          n_bytes++;
00143          // Decrement the number of byte left until the file has to be updated
00144          n_byte--;
00145          // Message
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          // Open the file if it is not done yet
00150          if (!_file && (refresh_every_n_lines || refresh_every_n_bytes))
00151              ASSERT(_file = fopen(_filename, "a+b"));
00152 
00153          // Print the received character on it
00154          fputc(c, _file);
00155 
00156          // Decrement the number of line received once a '\n' is received
00157          if (c == '\n' && refresh_every_n_lines)
00158            n_line--;
00159 
00160          // Update the file if the number of line received matches with the -l argument
00161          if (!n_line && refresh_every_n_lines)
00162          {
00163                fclose(_file);
00164                _file = NULL;
00165                n_line = refresh_every_n_lines;
00166          }
00167          // Update the file if the number of line received matches with the -b argument
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      // Else just write the the character received on the default output
00176      else
00177          putchar(c);
00178      n = 0;
00179      // Read a new character
00180      ASSERT(rs232_read(&c, 1, &n));
00181 
00182   }while(n == 1 && !iskbhit);
00183 
00184   // Close the file
00185   if (_file)
00186     fclose(_file);
00187 
00188   // Close the RS232 port
00189   rs232_close();
00190 }

DWORD WINAPI kb_event ( void *  arg  ) 

Definition at line 65 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

References FALSE, iskbhit, and TRUE.

Referenced by get_data().

00066 {
00067   while(1)
00068   {
00069     if (!iskbhit)
00070        iskbhit = (_getch())?TRUE:FALSE;
00071   }
00072   return 0;
00073 }

int main ( int  argc,
char *  argv[] 
)

Definition at line 211 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

References _filename, DEFAULT_BAUD_RATE, DEFAULT_BITS_NUMBER, DEFAULT_COM_PORT, DEFAULT_PARITY, DEFAULT_STOPBIT, get_data(), hthread1, hthread2, MAX_ARGS, print_help(), refresh_every_n_bytes, refresh_every_n_lines, RS232_PARITY_EVEN, RS232_PARITY_NOPARITY, RS232_PARITY_ODD, RS232_STOP_BIT_ONE, RS232_STOP_BIT_ONE5, and RS232_STOP_BIT_TWO.

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   // Read the arguments
00221   for(i=1; i<argc; i++)
00222   {
00223     // If an option as been specified
00224     if (argv[i][0] == '-')
00225     {
00226       // Decode it
00227       switch(argv[i][1])
00228       {
00229       // Refresh every N lines
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       // Refresh every N bytes
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       // Print in a file
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       // Display help
00260       case 'h':
00261         print_help();
00262         return 0;
00263       // If the option is not recognized, print the usage help
00264       default:
00265         printf("Unknown option '-%c'\r\n", argv[i][1]);
00266         print_help();
00267         return 0;
00268       }
00269     }
00270     // else it is a classical argument
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   // Call the get_data function with default options according to the number of arguments passed to this program
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 }

void print_help (  ) 

Definition at line 193 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

References DEFAULT_COM_PORT.

Referenced by main().

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 }

DWORD WINAPI sec_int ( void *  arg  ) 

Definition at line 54 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

References sec.

Referenced by get_data().

00055 {
00056   while(1)
00057   {
00058     sec++;
00059     Sleep(1000);
00060   }
00061   return 0;
00062 }


Variable Documentation

HANDLE hthread1 [static]

Definition at line 51 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and main().

HANDLE hthread2 [static]

Definition at line 51 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and main().

int iskbhit = FALSE [static]

Definition at line 50 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and kb_event().

int refresh_every_n_bytes = 0 [static]

Definition at line 49 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and main().

int refresh_every_n_lines = 0 [static]

Definition at line 48 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and main().

volatile int sec = 0 [static]

Definition at line 46 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and sec_int().


Generated on Thu Dec 17 19:57:24 2009 for AVR32 UC3 - EVK1104 DSPLib Demo Documentation by  doxygen 1.5.5