/********************************************************************/
/*                                                                  */
/*  Terminal.cpp: Sample UART Loopback Terminal Program             */
/*                                                                  */
/*  This is a sample terminal emulator for the UART based serial    */
/*  communications card. Please view the file abstract.txt for      */
/*  more information.                                               */
/*                                                                  */
/*  (c)Copyright Sealevel Systems, Inc., 1999                       */
/*                                                                  */
/*  SEALEVEL SYSTEMS INCORPORATED.                                  */
/*  155 Technology Place                                            */
/*  P.O. Box 830                                                    */
/*  Liberty, SC 29657  USA                                          */
/*  (864) 843-4343                                                  */
/*  (864) 843-3067 FAX                                              */
/*                                                                  */
/********************************************************************/


#include <windows.h>
#include <stdio.h>
#include <conio.h>

#define TIMEOUT_CONSTANT    50
#define ESC                 27


int main(int argc, char* argv[])
{
    int          key_pressed   = 0;
    char         outchar       = 0;
    char         inchar        = 0;
    DWORD        bytes_written = 0;    // Number of bytes written to port
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    COMMTIMEOUTS tempComTimeouts;      // Our temporary time-outs for COM1
    COMMTIMEOUTS savedComTimeouts;     // Stores the original time-outs
    HANDLE       comport       = NULL; // Handle for COM port
    DCB          comSettings;          // Contains various port settings

    printf("Sample UART Loopback Terminal Program v1.00.\n");
    printf("(c)Copyright Sealevel Systems, Inc., 1999.\n\n");
    
    // Open COM port
    if ((comport = 
         CreateFile("COM1", 
                    GENERIC_READ | GENERIC_WRITE, // for reading and writing
                    0,                            // exclusive access
                    NULL,                         // no security attributes
                    OPEN_EXISTING,              
                    FILE_ATTRIBUTE_NORMAL,
                    NULL)) == INVALID_HANDLE_VALUE)
    {
        printf("Unable to open COM1.\n\n");
        printf("Press any key to exit.");
        getch();
        return(1);
    }

    printf("COM1 opened.\n\n");

    // Save time-out parameters for COM1 
    GetCommTimeouts(comport,&savedComTimeouts);
    
    // Set our time-outs 
    tempComTimeouts.ReadIntervalTimeout         = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutMultiplier  = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutConstant    = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutConstant   = TIMEOUT_CONSTANT;
    SetCommTimeouts(comport,&tempComTimeouts);

    // Set Port parameters.
    // We make a call to GetCommState() first in order to fill
    // the comSettings structure with all the necessary values.
    // Then we change the ones we want and call SetCommState().
    GetCommState(comport, &comSettings);
    comSettings.BaudRate = 9600;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;
    SetCommState(comport, &comSettings);
    
 
    printf("Ready to send/receive data. Hit ESC to exit.\n\n");
            
    while(key_pressed != ESC)
    {
        if (kbhit())
        {
            key_pressed = getch();
            outchar = (char)key_pressed;

            if (key_pressed != ESC)
            {
                // Send data. if succesful, WriteFile() returns non-zero 
                WriteFile(comport,        // Handle
                          &outchar,       // Outgoing data
                          1,              // Number of bytes to write
                          &bytes_written, // Number of bytes written
                          NULL);
            }
        }
        
        // Read data. if succesful, ReadFile() returns non-zero
        ReadFile(comport,     // Handle
                 &inchar,     // Incoming data
                 1,           // Number of bytes to read
                 &bytes_read, // Number of bytes read
                 NULL);
            
        if (bytes_read == 1)
            if (inchar == 13)
                printf("\n");
            else
                printf("%c", inchar);
    }

    // Restore time-out parameters 
    SetCommTimeouts(comport,&savedComTimeouts);
    CloseHandle(comport);
    
    printf("\n");

    return(0);

}