- First create an boost::asio::io_service
- Create the boost::asio::serial_port using the io_service
- Open the port.
- Set the properties of the port.
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));
}
thanks a lot. Very usefull!
AntwortenLöschenVery usefull. Thanks a lot!
AntwortenLöschenThe stray curly brace at the start of the header file tells me this isn't actually a working example?
AntwortenLöschenYes you're right and I found another typo. I fixed it and now it should be ok.
Löschen