mt4cpp - Serial Port

The cpp files with presented examples: serial1.cpp, serial2.cpp, serial3.cpp. The serial port with timeout reads and/or write data until timeout.

Quick start

#include <mt4cpp/SerialPort.hpp>

using namespace mt4cpp;

Read from port

Default template parameter use boost::chrono steady timer. To create and open the port the port name and timeout is required. The method read_n reads given number of bytes.
SerialPort s("COM34", boost::chrono::milliseconds(50) );
std::vector in = s.read_n(3); //read 3 bytes or timeout
To read while predicate is true use read_if method as presented below.
//return true means read more, return false means not read more
bool predicate(const std::vector& buffer) {
	return buffer.empty() || buffer.back() != '\n';
}
SerialPort s("/dev/ttyUSB0", boost::chrono::milliseconds(50) );
std::vector in = s.read_if(predicate); //read until predicate or timeout

Write to port

Write buffer to port use timeout as well.
SerialPort s("COM34", boost::chrono::milliseconds(50) );
std::string out("output");
s.write( std::vector(out.begin(), out.end() ) );

Use BasicSerialPort

The template class BasicSerialPort could be used to generate type using other timers, e.g. std::chrono::steady_clock available in C++11 compilers.
typedef BasicSerialPort Serial;

return to the main page