// Copyright (C) 1999 Open Source Telecom Corporation.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception to the GNU General Public License, permission is
// granted for additional uses of the text contained in its release
// of APE.
//
// The exception is that, if you link the APE library with other files
// to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the APE library code into it.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the
// name APE. If you copy code from other releases into a copy of
// APE, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for APE, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
#ifndef __APE_SERIAL_H__
#define __APE_SERIAL_H__
#ifndef __APE_FILE_H__
#include <APE/file.h>
#endif
#include <termios.h>
#define MAX_SERIAL_TIMEOUT ((timeout_t)10000)
typedef enum
{
SERIAL_FLOW_NONE,
SERIAL_FLOW_HARD,
SERIAL_FLOW_SOFT,
SERIAL_FLOW_BOTH
};
typedef enum
{
SERIAL_FORMAT_7E1,
SERIAL_FORMAT_7O1,
SERIAL_FORMAT_7N1,
SERIAL_FORMAT_7S2,
SERIAL_FORMAT_8E1,
SERIAL_FORMAT_8O1,
SERIAL_FORMAT_8N1,
SERIAL_FORMAT_8S2
};
/**
* A portable APE class for access to generic serial devices. The win32
* version has not yet been written, but the Posix version uses termios
* services.
*
* @author David Sugar <dyfet@ostel.com>
* @short Generic serial device access.
*/
class Serial
{
private:
int _fd;
struct termios _restore;
struct termios _current;
char _lockname[65];
char _nl;
public:
/**
* Open a named serial device under a specified set of attributes.
* Attributes include the serial device speed (baud rate), the
* data format (SERIAL_FORMAT_7E1, SERIAL_FORMAT_7O1, SERIAL_FORMAT_7N1,
* SERIAL_FORMAT_7S2, SERIAL_FORMAT_8E1, SERIAL_FORMAT_8O1,
* SERIAL_FORMAT_8N1, and SERIAL_FORMAT_8S2), as well as the kind of
* flow control (SERIAL_FLOW_NONE, SERIAL_FLOW_HARD, SERIAL_FLOW_SOFT and
* SERIAL_FLOW_BOTH) to use. Additional data formats would be easy
* enough to add if so desired.
*
* @param fname path name of logical serial device.
* @speed absolute speed in bits per second.
* @format serial data format to use.
* @flow serial device flow control method to use.
*/
Serial(const char *fname, long speed, int format, int flow);
/**
* Release the serial device and restore it to it's previous
* attributes.
*/
virtual ~Serial();
/**
* Change the flow control mode in effect for the serial device.
* This allows post constructor changes.
*
* @param flow serial device flow control method to use.
*/
void setFlowControl(int flow);
/**
* Change the serial port baud rate (speed in bps) for the serial
* device.
*
* @param speed absolute speed in bits per second.
*/
void setSpeed(long speed);
/**
* Change the serial port data format in use for the serial device.
*
* @param format serial data format to use.
*/
void setFormat(int format);
/**
* Wait up to a specified timeout for a single character to appear
* at the serial device, and return that character.
*
* @return character read or -1 if timed out.
* @param timer timeout specified in milliseconds.
*/
int Inkey(timeout_t timer = 0);
/**
* Read a specified number of bytes from the serial device but
* wait no longer than a specified timeout for data to become
* available. The Posix termios only supports timeout resolution
* within 1/10th of a second, hence, specifying a timeout with
* an accuracy less than 100 milliseconds is useless.
*
* @return number of bytes actually read.
* @param buf pointer to store data from the device.
* @param len number of bytes to read.
* @param timer timeout specified in milliseconds.
*/
int Read(void *buf, size_t len, timeout_t timer = 0);
/**
* Read a specified number of bytes from the serial device
* but wait no longer than a specified timeout, and also stop
* if the newline character has been read.
*
* @return number of bytes actually read.
* @param buf pointer to store data from the device.
* @param len number of bytes to read.
* @param timer timeout specified in milliseconds.
*/
int Readline(char *buf, size_t max, timeout_t timer = 0);
/**
* Specify the newline character code to use with ReadLine.
*
* @param nl newline character code (default 13).
*/
inline void setNewline(char nl)
{_nl = nl;};
/**
* Write a block of data to the serial device.
*
* @return number of bytes written on success, -1 on error.
* @param buf pointer to data to write.
* @param len number of bytes to write.
*/
inline virtual int Write(void *buf, size_t len)
{return write(_fd, (char *)buf, len);};
friend inline int write(Serial &s, void *buf, size_t len)
{return write(s._fd, (char *)buf, len);};
/**
* Write a null terminated C string directly to the serial
* device.
*
* @return number of bytes written on success, -1 on error.
* @param buf pointer to null terminated string to write.
*/
inline virtual int Write(char *buf)
{return write(_fd, buf, strlen(buf));};
friend inline int write(Serial &s, char *buf)
{return write(s._fd, buf, strlen(buf));};
};
inline int inkey(Serial &s, timeout_t timer = 0)
{return s.Inkey(timer);};
inline int read(Serial &s, void *buf, size_t len, timeout_t timer = 0)
{return s.Read(buf, len, timer);};
inline int readline(Serial &s, char *buf, size_t max, timeout_t timer = 0)
{return s.Readline(buf, max, timer);};
#endif
Documentation generated by dyfet@centauri.sys on Sun Jul 25 18:45:46 EDT 1999