Subversion Repositories HomeAutomation

Rev

Rev 1959 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1959 Rev 1963
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
 
-
 
25
#include "common/log.h"
24
 
26
 
25
namespace atom {
27
namespace atom {
26
namespace net {
28
namespace net {
-
 
29
 
-
 
30
static const std::string log_module_ = "net::serialclient";
27
 
31
 
28
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)
29
{
33
{
-
 
34
  LOG_DEBUG_ENTER;
-
 
35
  LOG_DEBUG_EXIT;
30
}
36
}
31
 
37
 
32
SerialClient::~SerialClient()
38
SerialClient::~SerialClient()
33
{
39
{
-
 
40
  LOG_DEBUG_ENTER;
-
 
41
  LOG_DEBUG_EXIT;
34
}
42
}
35
 
43
 
36
void SerialClient::Connect(std::string address, unsigned int baud)
44
void SerialClient::Connect(std::string address, unsigned int baud)
37
{
45
{
-
 
46
  LOG_DEBUG_ENTER;
-
 
47
 
38
    boost::asio::serial_port_base::baud_rate baud_option(baud);
48
  boost::asio::serial_port_base::baud_rate baud_option(baud);
39
   
49
 
40
    this->serial_port_.open(address);
50
  this->serial_port_.open(address);
41
   
51
 
42
    if (!this->serial_port_.is_open())
52
  if (!this->serial_port_.is_open())
43
    {
53
  {
44
        throw std::runtime_error("Error while opening " + address);
54
    throw std::runtime_error("Error while opening " + address);
45
    }
55
  }
46
   
56
 
47
    this->serial_port_.set_option(baud_option);
57
  this->serial_port_.set_option(baud_option);
48
   
58
 
49
    this->Read();
59
  this->Read();
-
 
60
 
-
 
61
  LOG_DEBUG_EXIT;
50
}
62
}
51
 
63
 
52
void SerialClient::Stop()
64
void SerialClient::Stop()
53
{
65
{
-
 
66
  LOG_DEBUG_ENTER;
-
 
67
 
54
    if (this->serial_port_.is_open())
68
  if (this->serial_port_.is_open())
55
    {
69
  {
56
        this->serial_port_.cancel();
70
    this->serial_port_.cancel();
57
        this->serial_port_.close();
71
    this->serial_port_.close();
58
    }
72
  }
-
 
73
 
-
 
74
  LOG_DEBUG_EXIT;
59
}
75
}
60
 
76
 
61
void SerialClient::Send(common::Byteset data)
77
void SerialClient::Send(common::Byteset data)
62
{
78
{
-
 
79
  LOG_DEBUG_ENTER;
-
 
80
 
63
    try
81
  try
64
    {
82
  {
65
        if (this->serial_port_.is_open())
83
    if (this->serial_port_.is_open())
66
        {
84
    {
67
            this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
85
      this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
68
        }
86
    }
69
    }
87
  }
70
    catch (std::exception& e)
88
  catch (std::exception& e)
71
    {
89
  {
72
        this->Disconnect();
90
    this->Disconnect();
73
        //throw std::runtime_error(e.what());
91
      //throw std::runtime_error(e.what());
74
    }
92
  }
-
 
93
 
-
 
94
  LOG_DEBUG_EXIT;
75
}
95
}
76
 
96
 
77
void SerialClient::Read()
97
void SerialClient::Read()
78
{
98
{
-
 
99
  LOG_DEBUG_ENTER;
-
 
100
 
79
    Client::Read();
101
  Client::Read();
80
   
102
 
81
    this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
103
  this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
82
                                       boost::bind(&SerialClient::ReadHandler,
104
                                     boost::bind(&SerialClient::ReadHandler,
83
                                                   this,
105
                                                 this,
84
                                                   boost::asio::placeholders::error,
106
                                                 boost::asio::placeholders::error,
85
                                                   boost::asio::placeholders::bytes_transferred));
107
                                                 boost::asio::placeholders::bytes_transferred));
-
 
108
 
-
 
109
  LOG_DEBUG_EXIT;
86
}
110
}
87
 
111
 
88
 
112
 
89
}; // namespace net
113
}; // namespace net
90
}; // namespace atom
114
}; // namespace atom
91
 
115