Subversion Repositories HomeAutomation

Rev

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