Subversion Repositories HomeAutomation

Rev

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

Rev 1642 Rev 1916
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
namespace atom {
25
namespace atom {
26
namespace net {
26
namespace net {
27
 
27
 
28
SerialClient::SerialClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : serial_port_(io_service), Client(io_service, id, server_id)
28
SerialClient::SerialClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : Client(io_service, id, server_id), serial_port_(io_service)
29
{
29
{
30
}
30
}
31
 
31
 
32
SerialClient::~SerialClient()
32
SerialClient::~SerialClient()
33
{
33
{
34
}
34
}
35
 
35
 
36
void SerialClient::Connect(std::string address, unsigned int baud)
36
void SerialClient::Connect(std::string address, unsigned int baud)
37
{
37
{
38
    boost::asio::serial_port_base::baud_rate baud_option(baud);
38
    boost::asio::serial_port_base::baud_rate baud_option(baud);
39
   
39
   
40
    this->serial_port_.open(address);
40
    this->serial_port_.open(address);
41
   
41
   
42
    if (!this->serial_port_.is_open())
42
    if (!this->serial_port_.is_open())
43
    {
43
    {
44
        throw std::runtime_error("Error while opening " + address);
44
        throw std::runtime_error("Error while opening " + address);
45
    }
45
    }
46
   
46
   
47
    this->serial_port_.set_option(baud_option);
47
    this->serial_port_.set_option(baud_option);
48
   
48
   
49
    this->Read();
49
    this->Read();
50
}
50
}
51
 
51
 
52
void SerialClient::Stop()
52
void SerialClient::Stop()
53
{
53
{
54
    if (this->serial_port_.is_open())
54
    if (this->serial_port_.is_open())
55
    {
55
    {
56
        this->serial_port_.cancel();
56
        this->serial_port_.cancel();
57
        this->serial_port_.close();
57
        this->serial_port_.close();
58
    }
58
    }
59
}
59
}
60
 
60
 
61
void SerialClient::Send(common::Byteset data)
61
void SerialClient::Send(common::Byteset data)
62
{
62
{
63
    try
63
    try
64
    {
64
    {
65
        if (this->serial_port_.is_open())
65
        if (this->serial_port_.is_open())
66
        {
66
        {
67
            this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
67
            this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
68
        }
68
        }
69
    }
69
    }
70
    catch (std::exception& e)
70
    catch (std::exception& e)
71
    {
71
    {
72
        this->Disconnect();
72
        this->Disconnect();
73
        //throw std::runtime_error(e.what());
73
        //throw std::runtime_error(e.what());
74
    }
74
    }
75
}
75
}
76
 
76
 
77
void SerialClient::Read()
77
void SerialClient::Read()
78
{
78
{
79
    Client::Read();
79
    Client::Read();
80
   
80
   
81
    this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
81
    this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
82
                                       boost::bind(&SerialClient::ReadHandler,
82
                                       boost::bind(&SerialClient::ReadHandler,
83
                                                   this,
83
                                                   this,
84
                                                   boost::asio::placeholders::error,
84
                                                   boost::asio::placeholders::error,
85
                                                   boost::asio::placeholders::bytes_transferred));
85
                                                   boost::asio::placeholders::bytes_transferred));
86
}
86
}
87
 
87
 
88
 
88
 
89
}; // namespace net
89
}; // namespace net
90
}; // namespace atom
90
}; // namespace atom
91
 
91