Subversion Repositories HomeAutomation

Rev

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

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