Subversion Repositories HomeAutomation

Rev

Rev 1963 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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