/* * This Software is the original work of Daniel TUNG * This Software is submitted in partial fulfillment of the * requirements for the degree of BCSE. * Dept. Computer Science, Monash University 2000 * * Copyright (c) 2000 beetung@cs.monash.edu.au * beetung@geocities.com */ #ifndef COMPORT_H #define COMPORT_H #include #include #include //// //// This class encapsulate a RS232 port object. It opens/closes //// the ComPort and write byte values to it. //// //// ---Simple and Direct! //////////////////////////////////////// class ComPort { //////////////////////////////////////// private: ofstream com; // com port output stream string comName; public: //--------------------- ComPort(int portID) : comName("/dev/ttyS") { openCom(portID); } ~ComPort() { closeCom(); } //--------------------- private: /// /// Close the com port /// //--------------------- void closeCom(void) { //--------------------- cout << " closing com port ... " << comName << endl; com.close(); } /// /// Open the com port in C++ style! /// //--------------------- bool openCom(int& portID) { //--------------------- comName += 0x30 + portID ; // offseting in ASCII, (48+0) --> "0" cout << " Establishing communication channel: " << comName ; com.open( comName.c_str(), ios::out|ios::binary|ios::nocreate); if (!com) { cout << " ...(fail)" << endl; return false; } cout << " ...success " << endl; return true; } public: /// /// Write a byte to ComPort /// //--------------------- bool writeCom(char myChar) { //--------------------- #ifdef DEBUG cout << " ComPort::writeCom() - Writing to Comport: " << myChar <<" - as int("<< (myChar+48) << ")" << endl; #endif if (com) { com << myChar; com.flush(); return true; } else return false; } /// /// Same as writePort(), using operator<< instead! /// -- so we can do com1 << a << b; /// /// You can crash the computer here!, cast _any_ class object /// to char and write to com port /// template //--------------------- ComPort& operator<< (const Type& arg) { //--------------------- if (com) { #ifdef DEBUG cout << " ComPort::operator<<() Writing to Comport: " << arg <<" - as int("<< (arg+48) << ")" << endl; #endif com << static_cast(arg); com.flush(); } return *this ; } }; #endif