ACE下串口编程

ACE下串口编程

首先看下相关的类,在ACE下使用非常简单,后面是ACE自带的一个列子。

/**
 * @class ACE_TTY_IO
 *
 * @brief Class definitions for platform specific TTY features.
 *
 * This class represents an example interface for a specific
 * device (a serial line). It extends the capability of the
 * underlying DEV_IO class by adding a control method that takes
 * a special structure (Serial_Params) as argument to allow a
 * comfortable user interface (away from that annoying termios
 * structure, which is very specific to UNIX).
 */

什么是tty?

tty是终端设备的统称,tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西被键盘与显示器取代,所以现在叫终端比较合适。ACE中的这个ACE_TTY_IO类不长,这里全部拿来看下。


class ACE_Export ACE_TTY_IO : public ACE_DEV_IO
{
public:
  enum Control_Mode
  {
    SETPARAMS,              ///< Set control parameters.
    GETPARAMS               ///< Get control parameters.
  };


  struct ACE_Export Serial_Params
  {
    Serial_Params (void);


    /** Specifies the baudrate at which the communnication port operates. */
    int baudrate;
    /** Specifies the minimum number of bytes in input buffer before XON char
        is sent. Negative value indicates that default value should
        be used (Win32). */
    int xonlim;
    /** Specifies the maximum number of bytes in input buffer before XOFF char
        is sent. Negative value indicates that default value should
        be used (Win32). */
    int xofflim;
    /** Specifies the minimum number of characters for non-canonical
        read (POSIX). */
    unsigned int readmincharacters;
    /** Specifies the time to wait before returning from read. Negative value
        means infinite timeout. */
    int readtimeoutmsec;
    /** Enable/disable parity checking. */
    bool parityenb;
    /** Specifies the parity mode. POSIX supports "even" and "odd" parity.
        Additionally Win32 supports "mark" and "space" parity modes. */
    const char *paritymode;
    /** Enable & set CTS mode. Note that RTS & CTS are enabled/disabled
        together on some systems (RTS/CTS is enabled if either
        <code>ctsenb</code> or <code>rtsenb</code> is set). */
    bool ctsenb;
    /** Enable & set RTS mode. Note that RTS & CTS are enabled/disabled
        together on some systems (RTS/CTS is enabled if either
        <code>ctsenb</code> or <code>rtsenb</code> is set).
        - 0 = Disable RTS.
        - 1 = Enable RTS.
        - 2 = Enable RTS flow-control handshaking (Win32).
        - 3 = Specifies that RTS line will be high if bytes are available
              for transmission. After transmission RTS will be low (Win32). */
    int rtsenb;
    /** Enable/disable software flow control on input. */
    bool xinenb;
    /** Enable/disable software flow control on output. */
    bool xoutenb;
    /** Specifies if device is a modem (POSIX). If not set modem status
        lines are ignored. */
    bool modem;
    /** Enable/disable receiver (POSIX). */
    bool rcvenb;
    /** Controls whether DSR is disabled or enabled (Win32). */
    bool dsrenb;
    /** Controls whether DTR is disabled or enabled. */
    bool dtrdisable;
    /** Data bits. Valid values 5, 6, 7 and 8 data bits.
        Additionally Win32 supports 4 data bits. */
    unsigned char databits;
    /** Stop bits. Valid values are 1 and 2. */
    unsigned char stopbits;
  };


  /** Interface for reading/writing serial device parameters. */
  int control (Control_Mode cmd, Serial_Params *arg) const;


#if defined (ACE_NEEDS_DEV_IO_CONVERSION)
  /** This is necessary to pass ACE_TTY_IO as parameter to DEV_Connector. */
  operator ACE_DEV_IO &();
#endif /* ACE_NEEDS_DEV_IO_CONVERSION */
};


ACE的读Demo,所在目录ACE_wrappers\examples\IPC_SAP\DEV_SAP


#include "ace/DEV_Addr.h"
#include "ace/DEV_Connector.h"
#include "ace/TTY_IO.h"

int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  if (argc < 2)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("usage: %s device-filename\n"),
                       argv[0]),
                      1);


  ACE_TTY_IO read_dev;
  ACE_DEV_Connector con;


  if (con.connect (read_dev,
                   ACE_DEV_Addr (argv[1])) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       argv[1]),
                      1);


  ACE_TTY_IO::Serial_Params myparams;
  myparams.baudrate = 19200;
  myparams.xonlim = 0;
  myparams.xofflim = 0;
  myparams.readmincharacters = 0;
  myparams.readtimeoutmsec = 10*1000; // 10 seconds
  myparams.paritymode = "EVEN";
  myparams.ctsenb = false;
  myparams.rtsenb = 0;
  myparams.xinenb = false;
  myparams.xoutenb = false;
  myparams.modem = false;
  myparams.rcvenb = true;
  myparams.dsrenb = false;
  myparams.dtrdisable = false;
  myparams.databits = 8;
  myparams.stopbits = 1;


  if (read_dev.control (ACE_TTY_IO::SETPARAMS,
                        &myparams) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p control\n"),
                       argv[1]),
                      1);


  // Read till character 'q'.
  for (char readback = 'x'; readback != 'q'; )
    {
      ssize_t bytes_read =
        read_dev.recv ((void *) &readback, 1);


      if (bytes_read == 1)
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("read: %c\n"),
                      readback));
      else if (bytes_read == 0)
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("timeout!\n")));
      else if (bytes_read == -1)
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%p  recv\n"),
                             argv[1]), 1);
    }


  return 0;
}



你可能感兴趣的:(ACE下串口编程)