SerialPort.cpp
Go to the documentation of this file.
1 #include <igvc/SerialPort.h>
2 #include <iostream>
3 
4 using namespace std;
5 using namespace boost::asio;
6 
7 SerialPort::SerialPort(string device, int baud)
8  : port(ioservice),
9  path(device)
10 {
11  try
12  {
13  port.open(device);
14  } catch(...){}
15 
16  if( !port.is_open() ) {
17  cerr << "Could not open serial port " << device << endl;
18  return;
19  }
20 
21  try
22  {
23  port.set_option(boost::asio::serial_port_base::baud_rate(baud));
24  port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
25  port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
26  } catch(...) {
27  cerr << "Could not set options on serial port " << device << endl;
28  }
29 }
30 
32 {
33  port.close();
34 }
35 
37 {
38  return port.is_open();
39 }
40 
41 void SerialPort::write(string msg)
42 {
43  if(port.is_open())
44  boost::asio::write(port, boost::asio::buffer(msg.c_str(),msg.length()));
45 }
46 
47 void SerialPort::write(char *buffer, int length)
48 {
49  boost::asio::write(port, boost::asio::buffer(buffer, length));
50 }
51 
52 void SerialPort::write(unsigned char *buffer, int length)
53 {
54  if(port.is_open())
55  boost::asio::write(port, boost::asio::buffer(buffer, length));
56 }
57 
59 {
60  if(!port.is_open()) return -1;
61 
62  char in;
63  try
64  {
65  boost::asio::read(port, buffer(&in, 1));
66  } catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
67  cerr << "Error reading serial port." << endl;
68  cerr << err.what() << endl;
69  return 0;
70  }
71  return in;
72 }
73 
74 char* SerialPort::read(int numBytes)
75 {
76  if(!port.is_open())
77  return (char*)"";
78  char* bytes = new char[numBytes];
79  for(int i = 0; i < numBytes; i++)
80  bytes[i] = read();
81  return bytes;
82 }
83 
85 {
86  string line = "";
87  while(true)
88  {
89  char in = read();
90  if(in == '\n')
91  return line;
92  if(in == '\r')
93  return line;
94  line = line + in;
95  }
96 }
97 
99 {
100  return path;
101 }
char read()
Reads a single byte from the serial port.
Definition: SerialPort.cpp:58
action_pathConstPtr path
boost::asio::serial_port port
Definition: SerialPort.h:56
std::string readln()
Reads bytes from the serial port until or is found.
Definition: SerialPort.cpp:84
SerialPort(std::string device, int baud)
The constructor takes in the path to the port (eg. "/dev/ttyUSB0") and a baud rate for the connection...
Definition: SerialPort.cpp:7
bool isOpen()
Returns true if the serial port is connected and open.
Definition: SerialPort.cpp:36
void write(std::string msg)
Writes the given string to the serial port.
std::string devicePath()
Returns the path to the device this port is connected to.
Definition: SerialPort.cpp:98
std::string path
Definition: SerialPort.h:57


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45