home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cppcom.com / SERIALPO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-14  |  4.5 KB  |  169 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3.  
  4. All those who put this code or its derivatives in a commercial product MUST
  5. mention this copyright in their documentation for users of the products in
  6. which this code or its derivative classes are used.  Otherwise, this code
  7. may be freely distributed and freely used for any purpose.
  8.  
  9. Enhancements: 1991 by David Orme
  10.     *  General cleanup.
  11.             - I/O now takes advantage of C++ overloading.
  12.             - Serial port I/O functionality now only in Serial class.
  13.             - Modem functionality now only in Modem class.
  14.     *  Possible to easily implement file Xfr prots now.
  15.     *  CCITT CRC-16 class added                            -- 2-20-1991
  16.     *  BIOS Timer class added                                -- 2-22-1991
  17.     *  Optional timeout on all input routines added    -- 2-25-1991
  18.  
  19. ***************************************************************************/
  20.  
  21. // file serialpo.cpp class definitions for SerialPort.
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include "serialpo.hpp"
  27.  
  28. SerialPort::SerialPort(int portnum, int speed, parity_t p,
  29.                          int sbits, int dbits, boolean trans)
  30. {
  31.     port_number = portnum;
  32.     switch(port_number)
  33.     {
  34.         case 1: 
  35.             com = new COM1;
  36.             break;          
  37.         case 2: 
  38.             com = new COM2;
  39.             break;        
  40.         case 3: 
  41.             com = new COM3;
  42.             break;        
  43.         case 4: 
  44.             com = new COM4;
  45.             break;
  46.         default:
  47.             fputs("SerialPort error: constructor cannot initialize"
  48.                     " port number given", stderr);
  49.             exit(-1);
  50.     }    
  51.     com->SetBaudRate(speed);
  52.     com->SetParity(p);
  53.     com->SetStopBits(sbits);
  54.     com->SetDataBits(dbits);
  55.     translatenewline = trans;
  56.     lastin_wascr = false;
  57. }
  58.  
  59. SerialPort::~SerialPort() 
  60.     delete com;
  61. }
  62.  
  63. uart * SerialPort::GetUART() { return com; }
  64.  
  65. void SerialPort::SetSpeed(int s) { com->SetSpeed(s); }
  66. int SerialPort::GetSpeed() { return com->GetSpeed(); }
  67.  
  68. void SerialPort::SetParity(parity_t p) { com->SetParity(p); }
  69. parity_t SerialPort::GetParity() { return com->GetParity(); }
  70.  
  71. void SerialPort::SetStopBits(int s) { com->SetStopBits(s); }
  72. int SerialPort::GetStopBits() { return com->GetStopBits(); }
  73.  
  74. void SerialPort::SetDataBits(int s) { com->SetStopBits(s); }
  75. int SerialPort::GetDataBits() { return com->GetStopBits(); }
  76.  
  77. void SerialPort::PurgeInput() { com->PurgeInput(); }
  78. void SerialPort::FlushOutput() { com->FlushOutput(); }
  79. void SerialPort::PurgeOutput() { com->PurgeOutput(); }
  80.  
  81. boolean SerialPort::CarrierPresent() { return com->CarrierPresent(); }
  82.       
  83. void SerialPort::RaiseDTR() { com->SetDTR(true); }
  84. void SerialPort::DropDTR() { com->SetDTR(false); }
  85.  
  86.  
  87. void SerialPort::Pause(int ms) { com->Pause(ms); }
  88. void SerialPort::Break(int ms) { com->Break(ms); }
  89.  
  90. void SerialPort::SetDoIfNoCarrier(void (*f)()) { com->SetDoIfNoCarrier(f); }
  91. void SerialPort::SetDoOnRing(void (*f)()) { com->SetDoOnRing(f); }
  92.  
  93. boolean SerialPort::InputEmpty() { return com->InputEmpty(); }
  94. boolean SerialPort::OutputEmpty() { return com->OutputEmpty(); }
  95. boolean SerialPort::OutputReady() { return com->OutputReady(); }
  96.  
  97.  
  98. // These are the binary read/write functions -- they don't do
  99. // \r\n xlation.
  100.  
  101. void SerialPort::Write(void * p,int n)
  102. {
  103.     // will only write up to number that can fit into queue, up to max of n.
  104.     while(n > 0)
  105.     {
  106.         Send(*((char *)p)++);
  107.         --n;        
  108.     }
  109. }
  110.  
  111. int SerialPort::Read(char * p, int n, float secs)
  112. {
  113.     // will only read up to number in queue, up to max of n chars.
  114.     int i = 0;
  115.     T.Set(secs);                            // Set timeout timer
  116.     while(n-- > 0 && (!T.TimeOut() || com->InputEmpty()) )
  117.     {
  118.         while (!T.TimeOut() && com->InputEmpty());
  119.         if (com->InputEmpty())
  120.             break;
  121.         *p++ = com->GetChar();
  122.         i++;
  123.     }
  124.     return i;
  125.     // returns number actually read.
  126. }
  127.  
  128.  
  129. // The send and recieve functions below are set up to allow 
  130. // optional carriage return/linefeed translation of '\r\n' <--> '\n'
  131. // if translatenewline is true.
  132.  
  133. int SerialPort::Receive(char& ch, float secs)
  134. {
  135.     T.Set(secs);            // Set timeout timer
  136.  
  137.     while(!lastin_wascr && com->InputEmpty() && !T.TimeOut());
  138.  
  139.     if (!lastin_wascr && com->InputEmpty())
  140.         return -1;
  141.  
  142.     if(lastin_wascr)
  143.     {
  144.         ch = '\n';
  145.         lastin_wascr = false;
  146.     }
  147.     else
  148.     {
  149.         ch = char(com->GetChar());
  150.         if(translatenewline && ch == '\r')
  151.             lastin_wascr = true;
  152.     }
  153.     return 1;
  154. }
  155.  
  156. void SerialPort::Send(char ch)
  157. {
  158.     com->SendChar(ch);
  159.     if(translatenewline && ch == '\r')    
  160.         com->SendChar('\n'); 
  161. }
  162.  
  163. void SerialPort::Send(char * s) { while(*s) com->SendChar(*s++); }
  164.  
  165.  
  166. // end of file Serialpo.cpp
  167.  
  168.