Subversion Repositories HomeAutomation

Rev

Rev 1963 | 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. #include "common/log.h"
  26.  
  27. namespace atom {
  28. namespace net {
  29.  
  30. static const std::string log_module_ = "net::serialclient";
  31.  
  32. SerialClient::SerialClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id), serial_port_(io_service)
  33. {
  34.   LOG_DEBUG_ENTER;
  35.   LOG_DEBUG_EXIT;
  36. }
  37.  
  38. SerialClient::~SerialClient()
  39. {
  40.   LOG_DEBUG_ENTER;
  41.   LOG_DEBUG_EXIT;
  42. }
  43.  
  44. void SerialClient::Connect(std::string address, unsigned int baud)
  45. {
  46.   LOG_DEBUG_ENTER;
  47.  
  48.   boost::asio::serial_port_base::baud_rate baud_option(baud);
  49.  
  50.   this->serial_port_.open(address);
  51.  
  52.   if (!this->serial_port_.is_open())
  53.   {
  54.     throw std::runtime_error("Error while opening " + address);
  55.   }
  56.  
  57.   this->serial_port_.set_option(baud_option);
  58.  
  59.   this->Read();
  60.  
  61.   LOG_DEBUG_EXIT;
  62. }
  63.  
  64. void SerialClient::Stop()
  65. {
  66.   LOG_DEBUG_ENTER;
  67.  
  68.   if (this->serial_port_.is_open())
  69.   {
  70.     this->serial_port_.cancel();
  71.     this->serial_port_.close();
  72.   }
  73.  
  74.   LOG_DEBUG_EXIT;
  75. }
  76.  
  77. void SerialClient::Send(common::Byteset data)
  78. {
  79.   LOG_DEBUG_ENTER;
  80.  
  81.   try
  82.   {
  83.     if (this->serial_port_.is_open())
  84.     {
  85.       this->serial_port_.write_some(boost::asio::buffer(data.data(), data.size()));
  86.     }
  87.   }
  88.   catch (std::exception& e)
  89.   {
  90.     this->Disconnect();
  91.       //throw std::runtime_error(e.what());
  92.   }
  93.  
  94.   LOG_DEBUG_EXIT;
  95. }
  96.  
  97. void SerialClient::Read()
  98. {
  99.   LOG_DEBUG_ENTER;
  100.  
  101.   Client::Read();
  102.  
  103.   log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity());
  104.  
  105.   this->serial_port_.async_read_some(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()),
  106.                                      boost::bind(&SerialClient::ReadHandler,
  107.                                                  this,
  108.                                                  boost::asio::placeholders::error,
  109.                                                  boost::asio::placeholders::bytes_transferred));
  110.  
  111.   LOG_DEBUG_EXIT;
  112. }
  113.  
  114.  
  115. }; // namespace net
  116. }; // namespace atom
  117.