Subversion Repositories HomeAutomation

Rev

Rev 1963 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1963 Rev 1987
1
/*
1
/*
2
 *
2
 *
3
 *    Copyright (C) 2010  Mattias Runge
3
 *    Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *    This program is free software; you can redistribute it and/or modify
5
 *    This program is free software; you can redistribute it and/or modify
6
 *    it under the terms of the GNU General Public License as published by
6
 *    it under the terms of the GNU General Public License as published by
7
 *    the Free Software Foundation; either version 2 of the License, or
7
 *    the Free Software Foundation; either version 2 of the License, or
8
 *    (at your option) any later version.
8
 *    (at your option) any later version.
9
 *
9
 *
10
 *    This program is distributed in the hope that it will be useful,
10
 *    This program is distributed in the hope that it will be useful,
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *    GNU General Public License for more details.
13
 *    GNU General Public License for more details.
14
 *
14
 *
15
 *    You should have received a copy of the GNU General Public License along
15
 *    You should have received a copy of the GNU General Public License along
16
 *    with this program; if not, write to the Free Software Foundation, Inc.,
16
 *    with this program; if not, write to the Free Software Foundation, Inc.,
17
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include "SerialClient.h"
21
#include "SerialClient.h"
22
 
22
 
23
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast.hpp>
24
 
24
 
25
#include "common/log.h"
25
#include "common/log.h"
26
 
26
 
27
namespace atom {
27
namespace atom {
28
namespace net {
28
namespace net {
29
 
29
 
30
static const std::string log_module_ = "net::serialclient";
30
static const std::string log_module_ = "net::serialclient";
31
 
31
 
32
SerialClient::SerialClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id), serial_port_(io_service)
32
SerialClient::SerialClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id), serial_port_(io_service)
33
{
33
{
34
  LOG_DEBUG_ENTER;
34
  LOG_DEBUG_ENTER;
35
  LOG_DEBUG_EXIT;
35
  LOG_DEBUG_EXIT;
36
}
36
}
37
 
37
 
38
SerialClient::~SerialClient()
38
SerialClient::~SerialClient()
39
{
39
{
40
  LOG_DEBUG_ENTER;
40
  LOG_DEBUG_ENTER;
41
  LOG_DEBUG_EXIT;
41
  LOG_DEBUG_EXIT;
42
}
42
}
43
 
43
 
44
void SerialClient::Connect(std::string address, unsigned int baud)
44
void SerialClient::Connect(std::string address, unsigned int baud)
45
{
45
{
46
  LOG_DEBUG_ENTER;
46
  LOG_DEBUG_ENTER;
47
 
47
 
48
  boost::asio::serial_port_base::baud_rate baud_option(baud);
48
  boost::asio::serial_port_base::baud_rate baud_option(baud);
49
 
49
 
50
  this->serial_port_.open(address);
50
  this->serial_port_.open(address);
51
 
51
 
52
  if (!this->serial_port_.is_open())
52
  if (!this->serial_port_.is_open())
53
  {
53
  {
54
    throw std::runtime_error("Error while opening " + address);
54
    throw std::runtime_error("Error while opening " + address);
55
  }
55
  }
56
 
56
 
57
  this->serial_port_.set_option(baud_option);
57
  this->serial_port_.set_option(baud_option);
58
 
58
 
59
  this->Read();
59
  this->Read();
60
 
60
 
61
  LOG_DEBUG_EXIT;
61
  LOG_DEBUG_EXIT;
62
}
62
}
63
 
63
 
64
void SerialClient::Stop()
64
void SerialClient::Stop()
65
{
65
{
66
  LOG_DEBUG_ENTER;
66
  LOG_DEBUG_ENTER;
67
 
67
 
68
  if (this->serial_port_.is_open())
68
  if (this->serial_port_.is_open())
69
  {
69
  {
70
    this->serial_port_.cancel();
70
    this->serial_port_.cancel();
71
    this->serial_port_.close();
71
    this->serial_port_.close();
72
  }
72
  }
73
 
73
 
74
  LOG_DEBUG_EXIT;
74
  LOG_DEBUG_EXIT;
75
}
75
}
76
 
76
 
77
void SerialClient::Send(common::Byteset data)
77
void SerialClient::Send(common::Byteset data)
78
{
78
{
79
  LOG_DEBUG_ENTER;
79
  LOG_DEBUG_ENTER;
80
 
80
 
81
  try
81
  try
82
  {
82
  {
83
    if (this->serial_port_.is_open())
83
    if (this->serial_port_.is_open())
84
    {
84
    {
85
      this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
85
      this->serial_port_.write_some(boost::asio::buffer(data.data(), data.size()));
86
    }
86
    }
87
  }
87
  }
88
  catch (std::exception& e)
88
  catch (std::exception& e)
89
  {
89
  {
90
    this->Disconnect();
90
    this->Disconnect();
91
      //throw std::runtime_error(e.what());
91
      //throw std::runtime_error(e.what());
92
  }
92
  }
93
 
93
 
94
  LOG_DEBUG_EXIT;
94
  LOG_DEBUG_EXIT;
95
}
95
}
96
 
96
 
97
void SerialClient::Read()
97
void SerialClient::Read()
98
{
98
{
99
  LOG_DEBUG_ENTER;
99
  LOG_DEBUG_ENTER;
100
 
100
 
101
  Client::Read();
101
  Client::Read();
102
 
102
 
-
 
103
  log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity());
-
 
104
 
103
  this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
105
  this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()),
104
                                     boost::bind(&SerialClient::ReadHandler,
106
                                     boost::bind(&SerialClient::ReadHandler,
105
                                                 this,
107
                                                 this,
106
                                                 boost::asio::placeholders::error,
108
                                                 boost::asio::placeholders::error,
107
                                                 boost::asio::placeholders::bytes_transferred));
109
                                                 boost::asio::placeholders::bytes_transferred));
108
 
110
 
109
  LOG_DEBUG_EXIT;
111
  LOG_DEBUG_EXIT;
110
}
112
}
111
 
113
 
112
 
114
 
113
}; // namespace net
115
}; // namespace net
114
}; // namespace atom
116
}; // namespace atom
115
 
117