-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialPort.cpp
More file actions
164 lines (135 loc) · 3.78 KB
/
SerialPort.cpp
File metadata and controls
164 lines (135 loc) · 3.78 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "SerialPort.h"
#include <cstdlib>
SerialPort::SerialPort(void) : end_of_line_char_('\n')
{
}
SerialPort::~SerialPort(void)
{
stop();
}
char SerialPort::end_of_line_char() const
{
return this->end_of_line_char_;
}
void SerialPort::end_of_line_char(const char &c)
{
this->end_of_line_char_ = c;
}
bool SerialPort::start(const char *com_port_name, int baud_rate)
{
boost::system::error_code ec;
if (port_) {
std::cout << "error : port is already opened..." << std::endl;
return false;
}
port_ = serial_port_ptr(new boost::asio::serial_port(io_service_));
port_->open(com_port_name, ec);
if (ec) {
std::cout << "error : port_->open() failed...com_port_name="
<< com_port_name << ", e=" << ec.message().c_str() << std::endl;
return false;
}
// option settings...
port_->set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
port_->set_option(boost::asio::serial_port_base::character_size(8));
port_->set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
port_->set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
port_->set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service_));
async_read_some_();
return true;
}
void SerialPort::stop()
{
boost::mutex::scoped_lock look(mutex_);
if (port_) {
port_->cancel();
port_->close();
port_.reset();
}
io_service_.stop();
io_service_.reset();
}
int SerialPort::write_some(const std::string &buf)
{
return write_some(buf.c_str(), buf.size());
}
int SerialPort::write_some(const char *buf, const int &size)
{
boost::system::error_code ec;
if (!port_) return -1;
if (size == 0) return 0;
return port_->write_some(boost::asio::buffer(buf, size), ec);
}
void SerialPort::async_read_some_()
{
if (port_.get() == NULL || !port_->is_open()) return;
port_->async_read_some(
boost::asio::buffer(read_buf_raw_, SERIAL_PORT_READ_BUF_SIZE),
boost::bind(
&SerialPort::on_receive_,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void SerialPort::on_receive_(const boost::system::error_code& ec, size_t bytes_transferred)
{
boost::mutex::scoped_lock look(mutex_);
if (port_.get() == NULL || !port_->is_open()) return;
if (ec) {
async_read_some_();
return;
}
for (unsigned int i = 0; i < bytes_transferred; ++i) {
char c = read_buf_raw_[i];
if (c == end_of_line_char_) {
this->on_receive_(read_buf_str_);
read_buf_str_.clear();
}
else {
read_buf_str_ += c;
}
}
async_read_some_();
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
void SerialPort::on_receive_(const std::string &data)
{
//std::cout << "SerialPort::on_receive_() : " << data << std::endl;
this->angle_mutex_.lock();
//std::cout << data << std::endl;
std::vector<std::string> tokens = split(data, '_');
if(tokens.size() == 3)
{
//this->angle = atof(data.c_str());
//std::cout << "In Angle: " << this->angle << std::endl;;
//std::cout << tokens[0] << " " << tokens[1] << " " << tokens[2] << std::endl;
this->angle = atof(tokens[2].c_str());
this->x = atof(tokens[0].c_str());
this->y = atof(tokens[1].c_str());
}
this->angle_mutex_.unlock();
}
double SerialPort::get_angle()
{
return this->angle;
}
double SerialPort::get_x()
{
return this->x;
}
double SerialPort::get_y()
{
return this->y;
}