THE MAIN LOOP

The main loop in DEVICE.C should not be changed unless you find it extremely necessary. Some facts should be recognized about the main loop.

        void main(int argc, char **argv)

        {

        int checkKeyBoard;

        COORD xy;


        hStdin = GetStdHandle(STD_INPUT_HANDLE);

        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

        xy.X = 80;

        xy.Y = 100;

        SetConsoleScreenBufferSize(hStdout, xy);

        SetConsoleTitle("Tech Pack for ONSPEC/Win32");

        if ((argc > 1 ) && strchr(argv[1],'?')) {

        if (monitor)

        usage(argv[0]);

        ( 0 );

        }

        if (monitor)

        banner();

        tailer( argc, argv );

        gaurdian();

        initialize();

        readCommands( comlist );

        checkKeyBoard = monitor;

        if (monitor) {

        printf("\n\nPress Q to quit");

        printf("\nPress D to detach");

        printf("\nPress P to pause Monitor Mode");

        printf("\nPress R to resume Monitor Mode\n");

        }

        else

        FreeConsole();

        while ( !timeToQuit() ) {

        if (checkKeyBoard)

        monitor = checkKeyPress(monitor);

        pollOnspecInput();

        execute(cmdList);

        watchdog();

        delay( -delayTime );

        }

        exit( 0 );

        }
 

The function tailer reads the command line and sets the options it must be called first so the other functions will know which options are used.

The function gaurdian checks if the port we want is in use and must be called immediately after we know which port we want (option returned by tailer). This will ensure that we don't interfere with the communications of another process.

The function initialize performs a number of tasks but most importantly it opens up the communication channels. It must be called before we try to communicate with ONSPEC or the device.

The function timeToQuit must be called each time through the loop and may be called more often through the program. This ensures that we abort if ONSPEC goes down or if our port has been taken.

Finally we must call the function exit before terminating. This will close communication, ensure that all threads of the I/O template will terminate, and release their resources.