Mittwoch, 9. Oktober 2013

Serial Port and Boost

I have to admit that I was kind of surprised that there is really so less examples on the Internet about Serial Port programming using the Boost library. After trying and looking finally I understood how to use it and made a helper class to communicate with the Serial Port Device.

  1. First create an boost::asio::io_service
  2. Create the boost::asio::serial_port using the io_service
  3. Open the port.
  4. Set the properties of the port.
You have to be aware of the baudrate. Not every baudrate is supported by the boost library. If you need some special baudrates use the .native() method of the boost::asio:serial_port class. This gives you the native representation of the serial port ID. Use that to change the settings of the port with native C/C++ OS functions.

I didn't do here no error checking. So the proper way would be to use exceptions. Anyway, this is just for demonstrations. Here my header file.

#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <string>
#include <boost/asio.hpp>

/**
 * @class Serial
 * @brief Supports serial port communication.
*/
class Serial
{
public:
 Serial();
 ~Serial();
 
 /// Opens a connection to the serial port.
 /**
  * The parity, stop bits and flowcontrol are types of the boost::asio::serial_port_base.
  * @param portname The device name.
  * @param baudrate The Baudrate.
  * @param charactersize The Character size in bits.
  * @param parity The parity.
  * @param stopbits The stop bits.
  * @param flowcontrol The flowcontrol
 */
 void open(const std::string& portname, 
           int baudrate, 
           int charactersize, 
           boost::asio::serial_port_base::parity::type parity, 
           boost::asio::serial_port_base::stop_bits::type stopbits,
           boost::asio::serial_port_base::flow_control::type flowcontrol);
  
 /// Close the serial port.
 void close();
  
 /// Receive data from the serial port.
 /**
  * @param data The pointer to the data which will be filled.
  * @param length The number of data to read. (In Bytes)
  * @return The number of received data.
  */
 size_t receive(void* data, size_t length);
 
 /// Send data to the serial port.
 /**
  * @param data The pointer to the data which will be send.
  * @param length The number of data to send. (In Bytes)
  * @return The number of transfered data.
  */
 size_t send(void* data, size_t length);
private:
 boost::asio::io_service m_io;
 boost::asio::serial_port m_serialPort;
};


#endif // SERIALPORT_H

And here the source file:

#include "serial.h"


Serial::Serial() : m_serialPort(m_io) {
 m_io.run();
}
 
Serial::~Serial() {
 m_serialPort.close();

}

void Serial::open(const std::string& portname, 
                  int baudrate, 
                  int charactersize, 
                  boost::asio::serial_port_base::parity::type parity, 
                  boost::asio::serial_port_base::stop_bits::type stopbits,
                  boost::asio::serial_port_base::flow_control::type flowcontrol)
{
 m_serialPort.open(portname.c_str());
 m_serialPort.set_option(boost::asio::serial_port_base::baud_rate(baudrate));
 m_serialPort.set_option(boost::asio::serial_port_base::character_size(charactersize));
 m_serialPort.set_option(boost::asio::serial_port_base::parity(parity));
 m_serialPort.set_option(boost::asio::serial_port_base::stop_bits(stopbits));
 m_serialPort.set_option(boost::asio::serial_port_base::flow_control(flowcontrol));
}
void Serial::close()
{
 m_serialPort.close();
}
size_t Serial::receive(void* data, size_t length)
{
 return m_serialPort.read_some(boost::asio::buffer(data, length));
}
size_t Serial::send(void* data, size_t length)
{
 return m_serialPort.write_some(boost::asio::buffer(data, length));
}