<commit_before>/*
 * ASIOSerialPort.cpp
 *
 *  Created on: Nov 8, 2012
 *      Author: <NAME>
 */

#include "ASIOSerialPort.h"
#include <iostream>
#include <common/logger/logger.h>

ASIOSerialPort::ASIOSerialPort(std::string port_name, size_t baud)
    : port(ioservice)
{
    try
    {
        port.open(port_name);
    } catch(...){}

	if( !port.is_open() ) {
        std::stringstream msg;
        msg << "Failed to open serial port: " << port_name;
        Logger::Log(LogLevel::Error, msg.str());
        //exit(1);
	}

    if(isConnected())
    {
        try {
            port.set_option(boost::asio::serial_port_base::baud_rate(baud));
            port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
            port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
        } catch(...) {
            std::stringstream msg;
            msg << "Failed to set all options on port: " << port_name;
            Logger::Log(LogLevel::Error, msg.str());
            //exit(1);
        }
    }


    _packetHasBeenDefined = false;
    _hasEncounteredStartByte = false;
	_line = "";
	_eventRequests = 0;
}

void ASIOSerialPort::startEvents() {
    _eventRequests++;
    if(!_eventsEnabled)
    {
        _eventsEnabled = true;
        eventThread = boost::thread(boost::bind(&ASIOSerialPort::eventThreadRun, this));
    }
}

void ASIOSerialPort::stopEvents() {
    _eventRequests--;
    if(_eventRequests == 0)
    {
        _eventsEnabled = false;
        eventThread.join();
    }

}

void ASIOSerialPort::eventThreadRun() {
    while(isConnected() && _eventsEnabled)
    {
            char in = read();
            onNewByte(in);
            if(in == '\n' || in == '\r')
            {
                onNewLine(_line);
                _line = "";
            } else {
                _line += in;
            }
    }
}

void ASIOSerialPort::close() {
    if(_eventsEnabled)
    {
        _eventsEnabled = false;
        eventThread.join();
    }
    if(isConnected())
    {
        port.close();
    }
}

bool ASIOSerialPort::isConnected() {
	return port.is_open();
}

void ASIOSerialPort::write(std::string s) {
    if(isConnected()) boost::asio::write(port, boost::asio::buffer(s.c_str(),s.size()));
}

void ASIOSerialPort::write(char *msg, int length) {
    if(isConnected()) boost::asio::write(port, boost::asio::buffer(msg, length));
}

std::string ASIOSerialPort::readln() {
    if(!isConnected()) return "";

	char c;
	std::string line;


	while(true) {
		try {
            boost::asio::read(port, boost::asio::buffer(&c,1));
		} catch(boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
			std::cerr << "Error reading stream. Device may have been unplugged." << std::endl;
			std::cout << "\t" << err.what() << std::endl;
			return line;
		}
		switch(c) {
		case '\r':
            _eventsEnabled = true;
			return line;
			break;
		case '\n':
			return line;
			break;
		default:
			line += c;
			break;
		}
		if(_packetHasBeenDefined)
		{
		    if(c==_packetStartByte)
		    {
		        _packet = "";
		        _hasEncounteredStartByte = true;
		    }
		    if(_hasEncounteredStartByte)
		    {
		        _packet += c;
		    }
		    if(c==_packetEndByte)
		    {
                onNewPacket(_packet);
                _hasEncounteredStartByte = false;
		    }
		}
	}
	return "";
}

char ASIOSerialPort::read() {
    if(!isConnected()) return -1;

    char in;
    try {
        boost::asio::read(port, boost::asio::buffer(&in, 1));
    } catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
        std::cerr << "Error reading stream. Device may have been unplugged." << std::endl;
        std::cout << "\t" << err.what() << std::endl;
        return 0;
    }
    return in;
}

char* ASIOSerialPort::read(int numBytes) {
    if(!isConnected()) return (char*)"";
    char* bytes = new char[numBytes];
    for(int i = 0; i < numBytes; i++)
    {
        bytes[i] = read();
    }
    return bytes;<commit_msg>Fixes size/length bug in ASIOSerialPort Changes ASIOSerialPort printouts to the new Logger<commit_after>/*
 * ASIOSerialPort.cpp
 *
 *  Created on: Nov 8, 2012
 *      Author: <NAME>
 */

#include "ASIOSerialPort.h"
#include <iostream>
#include <common/logger/logger.h>

ASIOSerialPort::ASIOSerialPort(std::string port_name, size_t baud)
    : port(ioservice)
{
    try
    {
        port.open(port_name);
    } catch(...){}

	if( !port.is_open() ) {
        std::stringstream msg;
        msg << "Failed to open serial port: " << port_name;
        Logger::Log(LogLevel::Error, msg.str());
        //exit(1);
	}

    if(isConnected())
    {
        try {
            port.set_option(boost::asio::serial_port_base::baud_rate(baud));
            port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
            port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
        } catch(...) {
            std::stringstream msg;
            msg << "Failed to set all options on port: " << port_name;
            Logger::Log(LogLevel::Error, msg.str());
            //exit(1);
        }
    }


    _packetHasBeenDefined = false;
    _hasEncounteredStartByte = false;
	_line = "";
	_eventRequests = 0;
}

void ASIOSerialPort::startEvents() {
    _eventRequests++;
    if(!_eventsEnabled)
    {
        _eventsEnabled = true;
        eventThread = boost::thread(boost::bind(&ASIOSerialPort::eventThreadRun, this));
    }
}

void ASIOSerialPort::stopEvents() {
    _eventRequests--;
    if(_eventRequests == 0)
    {
        _eventsEnabled = false;
        eventThread.join();
    }

}

void ASIOSerialPort::eventThreadRun() {
    while(isConnected() && _eventsEnabled)
    {
            char in = read();
            onNewByte(in);
            if(in == '\n' || in == '\r')
            {
                onNewLine(_line);
                _line = "";
            } else {
                _line += in;
            }
    }
}

void ASIOSerialPort::close() {
    if(_eventsEnabled)
    {
        _eventsEnabled = false;
        eventThread.join();
    }
    if(isConnected())
    {
        port.close();
    }
}

bool ASIOSerialPort::isConnected() {
	return port.is_open();
}

void ASIOSerialPort::write(std::string s) {
    if(isConnected()) boost::asio::write(port, boost::asio::buffer(s.c_str(),s.length()));
}

void ASIOSerialPort::write(char *msg, int length) {
    if(isConnected()) boost::asio::write(port, boost::asio::buffer(msg, length));
}

std::string ASIOSerialPort::readln() {
    if(!isConnected()) return "";

	char c;
	std::string line;


    while(true) {
        try {
            boost::asio::read(port, boost::asio::buffer(&c,1));
		} catch(boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
            Logger::Log(LogLevel::Error, "Error reading serial port. Device may have been uplugged.");
            Logger::Log(LogLevel::Error, err.what());
			return line;
		}
		switch(c) {
		case '\r':
            _eventsEnabled = true;
			return line;
			break;
		case '\n':
			return line;
			break;
		default:
			line += c;
			break;
		}
		if(_packetHasBeenDefined)
		{
		    if(c==_packetStartByte)
		    {
		        _packet = "";
		        _hasEncounteredStartByte = true;
		    }
		    if(_hasEncounteredStartByte)
		    {
		        _packet += c;
		    }
		    if(c==_packetEndByte)
		    {
                onNewPacket(_packet);
                _hasEncounteredStartByte = false;
		    }
		}
	}
	return "";
}

char ASIOSerialPort::read() {
    if(!isConnected()) return -1;

    char in;
    try {
        boost::asio::read(port, boost::asio::buffer(&in, 1));
    } catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
        Logger::Log(LogLevel::Error, "Error reading serial port. Device may have been uplugged.");
        Logger::Log(LogLevel::Error, err.what());
        return 0;
    }
    return in;
}

char* ASIOSerialPort::read(int numBytes) {
    if(!isConnected()) return (char*)"";
    char* bytes = new char[numBytes];
    for(int i = 0; i < numBytes; i++)
    {
        bytes[i] = read();
    }
    return bytes;
{'fraction_non_alphanumeric': 0.1568650699549443, 'fraction_numerical': 0.003319895660422101, 'mean_word_length': 2.5893617021276594, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478030', 'n_tokens_mistral': 2961, 'n_tokens_neox': 2802, 'n_words': 480}