/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  MODEM.CPP                          by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Member definitions for the Modem class.                   */
/*                                                                           */
/*  Contents:      Modem::Modem()                                            */
/*                 Modem::~Modem()                                           */
/*                 Modem::IsModem()                                          */
/*                 Modem::Init()                                             */
/*                 Modem::SendATCmd()                                        */
/*                 Modem::SetDSR()                                           */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  05 Sep 93 acp       Initial Creation                                     */
/*                                                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#include "stddefs.h"
#include "modem.hpp"

//
// ----- Data Global to this class
//



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::Modem()                     by Alan Partis , Copyright (C)        */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Constructor for a Modem object.  Does nothing special.    */
/*                                                                           */
/*  Input:    none                                                           */
/*                                                                           */
/*  Output:   none                                                           */
/*                                                                           */
/*  Return:   n/a                                                            */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  05 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Modem::Modem()

{



}   /* end of function Modem::Modem() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::~Modem()                    by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Destructor for a Modem object.  Does nothing special.     */
/*                                                                           */
/*  Input:    none                                                           */
/*                                                                           */
/*  Output:   none                                                           */
/*                                                                           */
/*  Return:   n/a                                                            */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  05 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Modem::~Modem()

{



}   /* end of function Modem::~Modem() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::IsModem()                   by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Writes "AT" to the port to listen for an "OK" response.   */
/*       If the "OK" is returned, we have a modem, otherwise we don't.       */
/*                                                                           */
/*  Input:    none                                                           */
/*                                                                           */
/*  Output:   none                                                           */
/*                                                                           */
/*  Return:   rc                       return code                           */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  06 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int Modem::IsModem()

{

    char      dta[32];
    int       rc;

    //
    // initialize local variables
    //
    memset(dta, NULL, 32);
    rc = FAIL;

    //
    // Initialize port
    //
    _bios_serialcom(_COM_INIT, port, _COM_1200 | COM_8N1);

    //
    // Write "AT" and look for the "OK" or "0" response
    //
    strcpy(dta, "AT");
    SendATCmd(dta);

    //
    // Attempt to read "OK" or "0"
    //
    if (strstr(dta, "OK") != NULL)
    {
         rc = PASS;
    }
    else if (strstr(dta, "0") != NULL)
    {
         rc = PASS;
    }


    return (rc);

}   /* end of function Modem::IsModem() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::Init()                      by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Attempts to open the Modem device.  Return the results to */
/*       the calling process.                                                */
/*                                                                           */
/*  Input:    Port                     target port                           */
/*            Address                  port I/O address                      */
/*                                                                           */
/*  Output:   none                                                           */
/*                                                                           */
/*  Return:   rc                       return code                           */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  05 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int Modem::Init(int Port, word Address)
{

    int       rc;
    unsigned  status;

    //
    // initialize local variables
    //
    rc = FAIL;

    //
    // initialize private data member
    //
    port    = Port;
    address = Address;

    //
    // get status
    //
    status = _bios_serialcom(_COM_STATUS, port, NULL);

    //
    // If the DSR line is not set, try to set it with "AT&S0".
    //  Then get status again, if this does not work, then
    //  either this is not a modem, or the BIOS will not allow
    //  communications with a non-DSR port.  We're out of luck.
    //
    if ((status & COM_CTS) && !(status & COM_DSR))
    {
         SetDSR();
         status = _bios_serialcom(_COM_STATUS, port, NULL);
    }

    if ((status & COM_TSR_EMPTY) && (status & COM_DSR))
    {
         rc = IsModem();
    }

    return (rc);

}   /* end of function Modem::Init() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::SendATCmd()                 by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Sends the given 'AT' command to the modem.  The returned  */
/*       info is placed back in the given buffer.                            */
/*                                                                           */
/*  Input:    dta                      'AT' command to be sent               */
/*                                                                           */
/*  Output:   dta                      output from modem                     */
/*                                                                           */
/*  Return:   none                                                           */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  11 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void Modem::SendATCmd(char *dta)
{

    int       i;
    int       status;

    //
    // Send the command in the buffer
    //
    i = 0;
    while (dta[i] != NULL)
    {
         _bios_serialcom(_COM_SEND, port, dta[i]);
         i++;
    }

    //
    // Send the cr/lf pair to signal the end of the command string
    //
    _bios_serialcom(_COM_SEND, port, '\r');
    _bios_serialcom(_COM_SEND, port, '\n');

    //
    // Retrieve the modem's response and place back in the dta
    //
    i = 0;
    status = _bios_serialcom(_COM_RECEIVE, port, NULL);

    while (!(status & COM_TIMEOUT))
    {
         dta[i] = LOBYTE(status);
         status = _bios_serialcom(_COM_RECEIVE, port, NULL);
         i++;
    }

}   /* end of function Modem::SendATCmd() */



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*  Modem::SetDSR()                    by Alan Partis, Copyright (C)         */
/*                                     International Trade Systems, Inc.     */
/*                                     1993 All rights reserved.             */
/*                                                                           */
/*  Description:   Attempts to request that the modem assert the DSR line by */
/*       writing "AT&S0" directly to the port w/o going through BIOS.        */
/*                                                                           */
/*  Input:    Port                     COM port in question                  */
/*                                                                           */
/*  Output:   none                                                           */
/*                                                                           */
/*  Return:   none                                                           */
/*                                                                           */
/*  Date      Initials  Comments                                             */
/*  -----------------------------------------------------------------------  */
/*  16 Sep 93 acp       Initial Creation                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void     Modem::SetDSR()
{

    char dta[10];
    char i;

    //
    // fill in xmit string
    //
    strcpy(dta, "AT&S0\r\n");

    //
    // xmit string directly to port (go directly to port, do
    //  not pass 'go', do not collect $200)
    //
    i = 0;
    while (dta[i] != NULL)
    {
         outp(address, dta[i]);
         i++;

         //
         // We must delay for a short time here to allow
         //  the modem to process the character, it may be 
         //  working at only 300 baud at this point, it 
         //  hasn't been initialized yet.
         //
         delay(50);                    //slow down! I can't see!
    }

}   /* end of function Modem::SetDSR() */


  Copyright © Thundernet Development Group Inc., 2003.
All rights reserved.