1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include "serial.hpp"
#include <ios>
extern "C" {
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
}
serial::serial(const std::string& port, speed_t baud)
{
m_fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (m_fd < 0) {
throw std::ios_base::failure("Failed to open serial port " + port);
}
tcgetattr(m_fd, &m_settings);
// set input and output baudrate
cfsetispeed(&m_settings, baud);
cfsetospeed(&m_settings, baud);
// disable parity, 1 stop bit, clear data size
m_settings.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
// set data size to 8
m_settings.c_cflag |= CS8;
// disable hw flow control
m_settings.c_cflag &= ~CRTSCTS;
// enable receiver and ignore modem lines
m_settings.c_cflag |= CREAD | CLOCAL;
// disable XON / XOFF flow control
m_settings.c_iflag &= ~(IXON | IXOFF | IXANY);
// non canonical mode
m_settings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// no output processing
m_settings.c_oflag &= ~OPOST;
// input buffer of 8 bytes
m_settings.c_cc[VMIN] = 8;
// set indefinite timeout
m_settings.c_cc[VTIME] = 0;
if ((tcsetattr(m_fd, TCSANOW, &m_settings)) != 0)
throw std::ios_base::failure("Failed to set attributes");
// clean rx buffer
tcflush(m_fd, TCIFLUSH);
}
serial::~serial()
{
if (m_fd > 0)
close(m_fd);
}
int serial::write(const std::string& data) const
{
write(data.c_str(), data.length());
}
int serial::write(const char * const data, size_t len) const
{
return ::write(m_fd, data, len);
}
char serial::read() const
{
char ch;
if (::read(m_fd, &ch, 1) < 0)
throw std::ios_base::failure("Failed to read");
return ch;
}
std::string serial::read(size_t howmany) const
{
char data[howmany] = {};
if (::read(m_fd, data, howmany) < 0)
throw std::ios_base::failure("Failed to read");
return std::string(data);
}
|