main.c File Reference

#include <stdio.h>
#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 41 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by main().

#define DEFAULT_BITS_NUMBER   8

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

Referenced by main().

#define DEFAULT_COM_PORT   "COM1"

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

Referenced by main(), and print_help().

#define DEFAULT_PARITY   RS232_PARITY_NOPARITY

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

Referenced by main().

#define DEFAULT_STOPBIT   RS232_STOP_BIT_ONE

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

Referenced by main().

#define MAX_ARGS   10

Definition at line 46 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 78 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().

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

DWORD WINAPI kb_event ( void *  arg  ) 

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

References FALSE, iskbhit, and TRUE.

Referenced by get_data().

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

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

Definition at line 213 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.

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

void print_help (  ) 

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

References DEFAULT_COM_PORT.

Referenced by main().

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 }

DWORD WINAPI sec_int ( void *  arg  ) 

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

References sec.

Referenced by get_data().

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


Variable Documentation

HANDLE hthread1 [static]

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

Referenced by get_data(), and main().

HANDLE hthread2 [static]

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

Referenced by get_data(), and main().

int iskbhit = FALSE [static]

Definition at line 52 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 51 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 50 of file UTILS/PROGRAMS/WINDOWS/DATA_GET/main.c.

Referenced by get_data(), and main().

volatile int sec = 0 [static]

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

Referenced by get_data(), and sec_int().


Generated on Fri Feb 19 02:23:20 2010 for AVR32 UC3 - EVK1104 DSPLib Demo Documentation by  doxygen 1.5.5