aboutsummaryrefslogtreecommitdiff
path: root/src/serialboost.cpp
blob: 053d59e8f7b2ce8d83eac76423454bf3d0e8e595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    Copyright (C) 2014 BARRATERO Laurent                                   

    AeroUp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Aeroup is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
*/

/*
 *       Filename:  serialboost.cpp
 *
 *    Description:  
 *
 *        Version:  0.3.2
 *        Created:  20/12/2013 00:06:15
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BARATTERO Laurent, laurentba<at>larueluberlu.net
 *   Organization:  La rue Luberlu
 */
#include <serialboost.hpp>

using namespace std;
//using namespace boost;


/*
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: SerialBoost
 * Description:  Constructor
 */
SerialBoost::SerialBoost ( std::string port_name ):
                port(port_io)
{
  try
  {
  port.open( port_name );
  }
  catch(boost::system::system_error& e)
  {
        cout<<"Serial Error ("<<e.what()<< ")" << endl;
        throw;
  }

  typedef boost::asio::serial_port_base asio_serial;
  port.set_option(asio_serial::baud_rate(19200));
  port.set_option(asio_serial::flow_control(asio_serial::flow_control::none));
  port.set_option(asio_serial::parity( asio_serial::parity::none));
  port.set_option(asio_serial::stop_bits( asio_serial::stop_bits::one));
  port.set_option(asio_serial::character_size(8));

}		/* -----  end of Constructor  ----- */


/*
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: ~SerialBoost
 * Description:  Destructor
 */
SerialBoost::~SerialBoost (  )
{
  port.close();

}		/* -----  end of Destructor  ----- */


/**
 * SEND A CHAR METHODS... 
 **/

/*              
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendChar
 * Description:  Write on serial port
 *               char version
 */
  void
SerialBoost::sendChar(char c)
{
    port.write_some(boost::asio::buffer(&c, 1));
}

/*
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendChar
 * Description:  Write on serial port
 *               unsigned Char version
 */
  void
SerialBoost::sendChar(unsigned char c)
{
    port.write_some(boost::asio::buffer(&c, 1));

}

/**
 * SEND A STRING METHODS...
 **/

/*
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendString
 * Description:  Write on serial port
 *               (char* + nb) version
 */
  void
SerialBoost::sendString(char* s, int nb_char)
{
  port.write_some(boost::asio::buffer(s, nb_char));
}

/*
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendString 
 * Description:  Write on serial port
 *               (unsigned char* + nb) version
 */
  void
SerialBoost::sendString(unsigned char* s, int nb_char)
{
  port.write_some(boost::asio::buffer(s, nb_char));
}
  
/*               
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendString
 * Description:  Write on serial port
 *               (const char* + nb) version
 */
  void
SerialBoost::sendString(const char* s, int nb_char)
{
  port.write_some(boost::asio::buffer(s, nb_char));
}

/*               
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: sendString
 * Description:  Write on serial port
 *               String version
 */
  void 
SerialBoost::sendString(string s)
{
  port.write_some(boost::asio::buffer(s.c_str(),s.size()));
}

/** 
 * READ METHODS... 
 **/

/*               
 *       Class:  SerialBoost
 *      Method:  SerialBoost ::readLine
 * Description:  Read on serial port
 *               Readline version
 */
  string
SerialBoost::readLine()
{
  char c;
  string result;
    for(;;)
    {
      boost::asio::read(port, boost::asio::buffer(&c,1));
      switch(c)
      {
        case '\r':
          break;
        case '\n':
          return result;
        default:
          result+=c;
       }
    }
}

/*               
 *       Class:  SerialBoost
 *      Method:  SerialBoost :: getString
 * Description:  Read on serial port
 *               (char* + nb-read) version
 */
void SerialBoost::getString(char* s, int nb_char)
{
  boost::asio::read(port, boost::asio::buffer(s, nb_char));
}