Rev 1597 | Rev 1642 | Go to most recent revision | Details | Compare with Previous | 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 | } |
||
| 35 | |||
| 36 | void SerialClient::Connect(std::string address, unsigned int baud) |
||
| 37 | { |
||
| 38 | boost::asio::serial_port_base::baud_rate baud_option(baud); |
||
| 39 | |||
| 40 | this->serial_port_.open(address); |
||
| 41 | |||
| 42 | if (!this->serial_port_.is_open()) |
||
| 43 | { |
||
| 44 | throw std::runtime_error("Error while opening " + address); |
||
| 45 | } |
||
| 46 | |||
| 47 | this->serial_port_.set_option(baud_option); |
||
| 48 | |||
| 49 | this->Read(); |
||
| 50 | } |
||
| 51 | |||
| 1599 | runge | 52 | void SerialClient::Stop() |
| 1592 | runge | 53 | { |
| 1595 | runge | 54 | if (this->serial_port_.is_open()) |
| 55 | { |
||
| 56 | this->serial_port_.cancel(); |
||
| 57 | this->serial_port_.close(); |
||
| 58 | } |
||
| 1592 | runge | 59 | } |
| 60 | |||
| 1596 | runge | 61 | void SerialClient::Send(type::Byteset data) |
| 1592 | runge | 62 | { |
| 1597 | runge | 63 | try |
| 1593 | runge | 64 | { |
| 1597 | runge | 65 | if (this->serial_port_.is_open()) |
| 66 | { |
||
| 67 | this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize())); |
||
| 68 | } |
||
| 1593 | runge | 69 | } |
| 1597 | runge | 70 | catch (std::exception& e) |
| 1593 | runge | 71 | { |
| 72 | this->Disconnect(); |
||
| 1597 | runge | 73 | //throw std::runtime_error(e.what()); |
| 1593 | runge | 74 | } |
| 1592 | runge | 75 | } |
| 76 | |||
| 77 | void SerialClient::Read() |
||
| 78 | { |
||
| 79 | Client::Read(); |
||
| 80 | |||
| 1596 | runge | 81 | this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()), |
| 1592 | runge | 82 | boost::bind(&SerialClient::ReadHandler, |
| 83 | this, |
||
| 84 | boost::asio::placeholders::error, |
||
| 85 | boost::asio::placeholders::bytes_transferred)); |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | }; // namespace net |
||
| 90 | }; // namespace atom |