Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
std_serial.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 14.06.2013 22:27:34 EDT
3 // File: std_uart.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __STD_UART_HEADER
12 #define __STD_UART_HEADER
13 
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <termios.h>
17 #include <string.h>
18 
19 #include "color.hpp"
20 
21 class serial_class {
22  typedef int16_t size_type;
23 public:
24  serial_class(): avail(false), c(0) {
25  }
27  close();
28  }
29  void set_tio() {
30  memset(&tio,0,sizeof(tio));
31  tio.c_iflag=0;
32  tio.c_oflag=0;
33  tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
34  tio.c_lflag=0;
35  tio.c_cc[VMIN]=1;
36  tio.c_cc[VTIME]=5;//50
37  }
38  bool init(std::string const & name, uint const & baud = 460800ul) {
39  tty_fd=::open(name.c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY);
40  if(tty_fd == -1) {
41  std::cout << RED << "couldn't open port: " << REDB << name << NONE << std::endl;
42  return false;
43  }
44  std::cout << GREEN << "port " << GREENB << name << GREEN << " is open" << NONE << std::endl;
45  set_tio();
46 
47  #define lazyswitch(X) else if(baud == X) {cfsetospeed(&tio, B##X);cfsetispeed(&tio, B##X);}
48 
49  if(0);
50  lazyswitch(460800)
51  lazyswitch(230400)
52  lazyswitch(115200)
53  lazyswitch(57600)
54  lazyswitch(38400)
55  lazyswitch(19200)
56  lazyswitch(9600)
57  lazyswitch(4800)
58  lazyswitch(2400)
59  lazyswitch(1800)
60  lazyswitch(1200)
61  lazyswitch(600)
62  lazyswitch(300)
63  lazyswitch(200)
64  lazyswitch(150)
65  lazyswitch(134)
66  lazyswitch(110)
67  lazyswitch(75)
68  lazyswitch(50)
69  lazyswitch(0)
70  else {
71  std::cout << RED << "baud rate " << REDB << baud << RED << " is not supported!" << NONE << std::endl;
72  return false;
73  }
74 
75  tcsetattr(tty_fd, TCSANOW, &tio);
76 
77  flush();
78 
79  return true;
80  }
81 
82  size_type available() {
83  if(!avail) {
84  avail = (::read(tty_fd,&c,1) > 0);
85  }
86  return avail;
87  }
88  size_type write(char const & data, uint8_t const & len = 1) {
89 
90  int sent = ::write(tty_fd, &data, len);
91  while(sent != 1) //resend on failure
92  sent = ::write(tty_fd, &data, len);
93 
94  //~ if(sent == 1)
95  //~ std::cout << GREENB;
96  //~ else
97  //~ std::cout << REDB;
98  //~ std::cout << int(data) << " " << NONE;
99  //~ std::cout.flush();
100 
101  return sent;
102  }
103  int wait() {
104  int counter = 0;
105  while(!available())
106  ++counter;
107  return counter;
108  }
109 
110  char read() {
111  if(available()) {
112  avail = false;
113  return c;
114  }
115  return 0;
116  }
117  void flush() {
118  for(uint i = 0; i < 10000; ++i) { //just heuristics...
119  if(available()) {
120  avail = false;
121  }
122  }
123  while(available()) {
124  avail = false;
125  }
126  }
127  void close() {
128  flush();
129  if(tty_fd != -1)
130  ::close(tty_fd);
131  std::cout << std::endl << GREEN << "close port" << NONE << std::endl;
132  }
133 private:
134  struct termios tio;
135  int tty_fd;
136  bool avail;
137  char c;
138 };
139 
140 extern serial_class Serial;
141 
142 #endif //__STD_UART_HEADER
#define lazyswitch(X)
~serial_class()
Definition: std_serial.hpp:26
#define GREEN
Definition: color.hpp:15
#define RED
Definition: color.hpp:13
#define GREENB
Definition: color.hpp:16
void set_tio()
Definition: std_serial.hpp:29
Definition: std_serial.hpp:21
serial_class()
Definition: std_serial.hpp:24
#define NONE
Definition: color.hpp:37
serial_class Serial
struct ustd::endl_class endl
size_type available()
Definition: std_serial.hpp:82
#define REDB
Definition: color.hpp:14
size_type write(char const &data, uint8_t const &len=1)
Definition: std_serial.hpp:88
void close()
Definition: std_serial.hpp:127
int wait()
Definition: std_serial.hpp:103
ustd::cout_class cout
void flush()
Definition: std_serial.hpp:117
char read()
Definition: std_serial.hpp:110
bool init(std::string const &name, uint const &baud=460800ul)
Definition: std_serial.hpp:38