-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialport.cc
102 lines (76 loc) · 1.85 KB
/
serialport.cc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
/* vim: set sw=4 sts=4 et : */
#include "serialport.hh"
#include "stringify.hh"
#include <string>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <iostream>
SerialPortOptions::SerialPortOptions() :
dev("/dev/null"),
speed(0)
{
}
SerialPortOptions & SerialPortOptions::with_dev(const std::string & d)
{
dev = d;
return *this;
}
SerialPortOptions & SerialPortOptions::with_speed(unsigned s)
{
speed = s;
return *this;
}
SerialPort::SerialPort(const SerialPortOptions & o) :
_opts(o)
{
open_port();
configure_port();
}
SerialPort::~SerialPort()
{
}
void SerialPort::open_port()
{
_fd = open(_opts.dev.c_str(), O_RDWR | O_NDELAY | O_NOCTTY);
if (_fd == -1){
std::cout << "Failed in opening serial port " << _opts.dev.c_str() << std::endl;
return;
}
tcflush(_fd, TCIOFLUSH);
int n(fcntl(_fd, F_GETFL, 0));
fcntl(_fd, F_SETFL, n & ~O_NDELAY);
tcgetattr(_fd, &_oldtio);
}
/* XXX Throw if stuff goes wrong */
void SerialPort::configure_port()
{
struct termios ptio;
tcgetattr(_fd, &ptio);
cfsetispeed(&ptio, _opts.speed);
cfsetospeed(&ptio, _opts.speed);
ptio.c_cflag |= CLOCAL | CREAD;
ptio.c_cflag &= ~(PARENB | CSIZE | CSTOPB);
ptio.c_cflag |= CS8;
ptio.c_cflag &= ~CRTSCTS;
ptio.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
ptio.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK
| INPCK | ISTRIP | IXON);
ptio.c_oflag &= ~OPOST;
ptio.c_cc[VTIME] = 0;
ptio.c_cc[VMIN] = 1;
tcsetattr(_fd, TCSANOW, &ptio);
int mcs(0);
ioctl(_fd, TIOCMGET, &mcs);
mcs |= TIOCM_RTS;
ioctl(_fd, TIOCMSET, &mcs);
tcgetattr(_fd, &ptio);
ptio.c_cflag &= ~CRTSCTS;
tcsetattr(_fd, TCSANOW, &ptio);
}
int SerialPort::get_fd() const
{
return _fd;
}