libtasks Documentation  1.6
term.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _TERM_H_
9 #define _TERM_H_
10 
11 #include <string>
12 #include <exception>
13 #include <ios>
14 #include <termios.h>
15 
16 #include <tasks/io_base.h>
17 #include <tasks/tasks_exception.h>
18 
19 namespace tasks {
20 namespace serial {
21 
22 enum class termmode_t { _8N1, _7S1, _7E1, _7O1 };
23 enum class charsize_t : tcflag_t { _5 = CS5, _6 = CS6, _7 = CS7, _8 = CS8 };
24 enum class parity_t { NONE, EVEN, ODD };
25 enum class stopbits_t { _1, _2 };
26 
35 class term : public io_base {
36  public:
38  term() {}
39 
45  term(int fd) : io_base(fd) {}
46 
56  void open(std::string port, speed_t baudrate = B9600, charsize_t charsize = charsize_t::_8,
57  parity_t parity = parity_t::NONE, stopbits_t stopbits = stopbits_t::_1);
58 
67  void open(std::string port, speed_t baudrate = B9600, termmode_t mode = termmode_t::_8N1);
68 
70  struct termios options();
72  void set_options(struct termios& opts);
74  speed_t baudrate() const;
76  void baudrate(speed_t br);
78  void close();
79 
87  std::streamsize write(const char* data, std::size_t len);
88 
96  std::streamsize read(char* data, std::size_t len);
97 };
98 
99 } // serial
100 } // tasks
101 
102 #endif // _TERM_H_
void open(std::string port, speed_t baudrate=B9600, charsize_t charsize=charsize_t::_8, parity_t parity=parity_t::NONE, stopbits_t stopbits=stopbits_t::_1)
Open a terminal device.
Definition: term.cpp:40
term()
Contruct an object.
Definition: term.h:38
std::streamsize write(const char *data, std::size_t len)
Write data to the terminal.
Definition: term.cpp:137
void close()
Close the terminal.
Definition: term.cpp:130
int fd() const
Definition: io_base.h:22
term(int fd)
Contruct an object by providing a terminal file descriptor.
Definition: term.h:45
Base class for socket and term.
Definition: io_base.h:16
std::streamsize read(char *data, std::size_t len)
Read data from the terminal.
Definition: term.cpp:144
struct termios options()
Access terminal options.
Definition: term.cpp:16
speed_t baudrate() const
Access the baudrate.
void set_options(struct termios &opts)
Set terminal options.
Definition: term.cpp:29
A class that allows to read and write from/to terminal devices.
Definition: term.h:35