Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1959 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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) : Client(io_service, id, server_id), serial_port_(io_service)
  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.  
  52. void SerialClient::Stop()
  53. {
  54.     if (this->serial_port_.is_open())
  55.     {
  56.         this->serial_port_.cancel();
  57.         this->serial_port_.close();
  58.     }
  59. }
  60.  
  61. void SerialClient::Send(common::Byteset data)
  62. {
  63.     try
  64.     {
  65.         if (this->serial_port_.is_open())
  66.         {
  67.             this->serial_port_.write_some(boost::asio::buffer(data.Get(), data.GetMaxSize()));
  68.         }
  69.     }
  70.     catch (std::exception& e)
  71.     {
  72.         this->Disconnect();
  73.         //throw std::runtime_error(e.what());
  74.     }
  75. }
  76.  
  77. void SerialClient::Read()
  78. {
  79.     Client::Read();
  80.    
  81.     this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
  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
  91.