home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CPPCOM17.ZIP / SERIALPO.CPP < prev    next >
C/C++ Source or Header  |  1991-02-27  |  4KB  |  142 lines

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