SERIAL++ library: simple serial port access
serialport.cc
Go to the documentation of this file.
1 
37 #define TF_SERIALPORT_CC_VERSION \
38  "TF_SERIALPORT_CC V1.1"
39 #define TF_SERIALPORT_CC_CVSID \
40  "$Id$"
41 
42 #include <serialxx/serialport.h>
43 #include <serialxx/error.h>
44 #include <unistd.h>
45 #include <iostream>
46 
47 namespace serialport {
48 
50  SerialPort::SerialPort(const std::string& devname,
51  const int& flags)
52  {
53  Mfd=open(devname.c_str(), flags);
54  SERIALPORT_assert(Mfd>0, "ERROR on opening port");
55  Mdebug=false;
56  ::tcgetattr(Mfd, &Mdevoptions);
57  Musexon=false;
58  }
59 
60  /*----------------------------------------------------------------------*/
61 
64  {
65  // restore options
66  ::tcsetattr(Mfd, TCSANOW, &Mdevoptions);
67  SERIALPORT_assert(close(Mfd)!=-1, "ERROR on closing port");
68  }
69 
70  /*----------------------------------------------------------------------*/
71 
73  void SerialPort::write(const std::string& text) const
74  {
75  int n,len=text.size();
76  n=::write(Mfd, text.c_str(), len);
77  SERIALPORT_assert(n==len, "ERROR on writing to port");
78  }
79 
80  /*----------------------------------------------------------------------*/
81 
83  std::string SerialPort::read(const std::string& delim) const
84  {
85  std::string retval;
86  const int nbuf=255;
87  char buffer[nbuf];
88  bool hot=true;
89  this->sendxon();
90  while (hot)
91  {
92  int nbytes=::read(Mfd, buffer, nbuf);
93  if (Mdebug)
94  {
95  std::cout << "SerialPort::read: " << std::endl
96  << "read " << nbytes << " characters" << std::endl;
97  }
98  if (nbytes>0) { retval.append(buffer, nbytes); }
99  if (retval.find(delim)!=std::string::npos) { hot=false; }
100  }
101  this->sendxoff();
102  return(retval);
103  }
104 
105  /*----------------------------------------------------------------------*/
106 
108  int SerialPort::read(char* buffer, const int& bufsize) const
109  { return(::read(Mfd, buffer, bufsize)); }
110 
111  /*----------------------------------------------------------------------*/
112 
113  void SerialPort::sendxon() const
114  { if (Musexon) { ::tcflow(Mfd, TCION); } }
115 
116  /*----------------------------------------------------------------------*/
117 
118  void SerialPort::sendxoff() const
119  { if (Musexon) { ::tcflow(Mfd, TCIOFF); } }
120 
121  /*----------------------------------------------------------------------*/
122 
123  void SerialPort::clocal() const
124  {
125  struct termios options;
126  ::tcgetattr(Mfd, &options);
127  options.c_cflag |= CLOCAL;
128  ::tcsetattr(Mfd, TCSANOW, &options);
129  } // void SerialPort::clocal() const
130 
131  /*----------------------------------------------------------------------*/
132 
133  void SerialPort::mode7E1() const
134  {
135  struct termios options;
136  ::tcgetattr(Mfd, &options);
137  options.c_cflag |= PARENB;
138  options.c_cflag &= ~PARODD;
139  options.c_cflag &= ~CSTOPB;
140  options.c_cflag &= ~CSIZE;
141  options.c_cflag |= CS7;
142  ::tcsetattr(Mfd, TCSANOW, &options);
143  } // void SerialPort::mode7E1() const
144 
145  /*----------------------------------------------------------------------*/
146 
148  {
149  struct termios options;
150  ::tcgetattr(Mfd, &options);
151  options.c_iflag |= (IXON | IXOFF | IXANY);
152  ::tcsetattr(Mfd, TCSANOW, &options);
153  Musexon=true;
154  ::tcflow(Mfd, TCIOFF);
155  } // void SerialPort::flowxon() const
156 
157  /*----------------------------------------------------------------------*/
158 
159  void SerialPort::baud9600() const
160  {
161  struct termios options;
162  ::tcgetattr(Mfd, &options);
163  ::cfsetispeed(&options, B9600);
164  ::cfsetospeed(&options, B9600);
165  ::tcsetattr(Mfd, TCSANOW, &options);
166  } // void SerialPort::baud9600() const
167 
168  /*----------------------------------------------------------------------*/
169 
170  void SerialPort::makeraw() const
171  {
172  struct termios options;
173  ::tcgetattr(Mfd, &options);
174  ::cfmakeraw(&options);
175  ::tcsetattr(Mfd, TCSANOW, &options);
176  } // void SerialPort::makeraw() const
177 
178 } // namespace serialport
179 
180 /* ----- END OF serialport.cc ----- */
void clocal() const
set clocal
Definition: serialport.cc:123
void baud9600() const
select 9600 baud
Definition: serialport.cc:159
std::string read(const std::string &delim="\) const
read from port until delimiter
Definition: serialport.cc:83
#define SERIALPORT_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:144
code to access a serial port (prototypes)
bool Musexon
use xon/xoff flow control
Definition: serialport.h:96
error handling code for serial port access (prototypes)
Root namespace of library.
Definition: doxygen.txt:26
struct termios Mdevoptions
remember device status
Definition: serialport.h:94
void makeraw() const
issue cfmakeraw
Definition: serialport.cc:170
void write(const std::string &text) const
write string to port
Definition: serialport.cc:73
void mode7E1() const
set 7 data bits, even parity, 1 stop bit
Definition: serialport.cc:133
SerialPort(const std::string &devname, const int &flags=(O_RDWR|O_NOCTTY|O_NDELAY))
open port by name of device file
Definition: serialport.cc:50
void sendxoff() const
send xoff
Definition: serialport.cc:118
int Mfd
file descriptor to this port
Definition: serialport.h:90
~SerialPort()
close port
Definition: serialport.cc:63
bool Mdebug
debug flag
Definition: serialport.h:92
void flowxon()
set xon/xoff slow control
Definition: serialport.cc:147
void sendxon() const
send xon
Definition: serialport.cc:113