Flexiport  2.0.0
serialport.h
Go to the documentation of this file.
1 /* Flexiport
2  *
3  * Header file for the serial port class.
4  *
5  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
6  * RT-Synthesis Research Group
7  * Intelligent Systems Research Institute,
8  * National Institute of Advanced Industrial Science and Technology (AIST),
9  * Japan
10  * All rights reserved.
11  *
12  * This file is part of Flexiport.
13  *
14  * Flexiport is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation; either version 2.1 of the License,
17  * or (at your option) any later version.
18  *
19  * Flexiport is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with Flexiport. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #ifndef __SERIALPORT_H
30 #define __SERIALPORT_H
31 
32 #include <flexiport/port.h>
33 
34 #include <map>
35 #include <string>
36 #if defined (WIN32)
37  #include <Windows.h>
38 #endif
39 
44 namespace flexiport
45 {
46 
75 {
76  public:
77  SerialPort (std::map<std::string, std::string> options);
79 
81  void Open ();
83  void Close ();
85  ssize_t Read (void * const buffer, size_t count);
87  ssize_t ReadFull (void * const buffer, size_t count);
89  ssize_t BytesAvailable ();
95  ssize_t BytesAvailableWait ();
97  ssize_t Write (const void * const buffer, size_t count);
99  void Flush ();
101  void Drain ();
103  std::string GetStatus () const;
105  void SetTimeout (Timeout timeout);
107  void SetCanRead (bool canRead);
109  void SetCanWrite (bool canWrite);
111  bool IsOpen () const { return _open; }
112 
114  void SetBaudRate (unsigned int baud);
116  unsigned int GetBaudRate () const { return _baud; }
117 
118  private:
119 #if defined (WIN32)
120  HANDLE _fd; // Serial device handle
121 #else
122  int _fd; // Serial device file descriptor
123 #endif
124 
125  std::string _device;
126  unsigned int _baud;
127  unsigned int _dataBits;
128  unsigned int _stopBits;
129  typedef enum {PAR_NONE, PAR_EVEN, PAR_ODD} Parity;
130  Parity _parity;
131  bool _hwFlowCtrl;
132  bool _open;
133 
134  void CheckPort (bool read);
135  bool ProcessOption (const std::string &option, const std::string &value);
136 
137  bool IsDataAvailable ();
138 #if !defined (WIN32)
139  typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
140  WaitStatus WaitForDataOrTimeout ();
141  WaitStatus WaitForWritableOrTimeout ();
142 #endif
143  void SetPortSettings ();
144  void SetPortTimeout ();
145 };
146 
147 } // namespace flexiport
148 
151 #endif // __SERIALPORT_H
152 
flexiport::SerialPort::Write
ssize_t Write(const void *const buffer, size_t count)
Write data to the port.
FLEXIPORT_EXPORT
#define FLEXIPORT_EXPORT
Definition: flexiport.h:44
flexiport::SerialPort::Read
ssize_t Read(void *const buffer, size_t count)
Read from the port.
flexiport::SerialPort::GetStatus
std::string GetStatus() const
Get the status of the port (type, device, etc).
port.h
flexiport::SerialPort::Open
void Open()
Open the port.
flexiport::SerialPort::ReadFull
ssize_t ReadFull(void *const buffer, size_t count)
Read the requested quantity of data from the port.
flexiport::SerialPort::Flush
void Flush()
Flush the port's input and output buffers, discarding all data.
flexiport::SerialPort::IsOpen
bool IsOpen() const
Check if the port is open.
Definition: serialport.h:111
flexiport
Definition: flexiport.h:52
flexiport::SerialPort
Serial implementation of the Port class.
Definition: serialport.h:75
flexiport::SerialPort::Close
void Close()
Close the port.
flexiport::SerialPort::BytesAvailableWait
ssize_t BytesAvailableWait()
Get the number of bytes waiting after blocking for the timeout.
flexiport::SerialPort::Drain
void Drain()
Drain the port's input and output buffers.
flexiport::SerialPort::SetTimeout
void SetTimeout(Timeout timeout)
Set the timeout value in milliseconds.
flexiport::SerialPort::~SerialPort
~SerialPort()
flexiport::Port
Base Port class.
Definition: port.h:82
flexiport::SerialPort::SetCanWrite
void SetCanWrite(bool canWrite)
Set the write permissions of the port.
flexiport::SerialPort::SetBaudRate
void SetBaudRate(unsigned int baud)
Change the baud rate.
flexiport::SerialPort::SetCanRead
void SetCanRead(bool canRead)
Set the read permissions of the port.
flexiport::SerialPort::GetBaudRate
unsigned int GetBaudRate() const
Get the current baud rate.
Definition: serialport.h:116
flexiport::SerialPort::SerialPort
SerialPort(std::map< std::string, std::string > options)
flexiport::SerialPort::BytesAvailable
ssize_t BytesAvailable()
Get the number of bytes waiting to be read at the port. Returns immediatly.
flexiport::Timeout
An object used to represent timeouts.
Definition: timeout.h:64