Subversion Repositories HomeAutomation

Rev

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

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