#include "serial_read.hpp"

Boost_serial::Boost_serial()
{
    ;
}

Boost_serial::~Boost_serial()
{
    if (sp)
    {
        delete sp;
    }
}

void Boost_serial::init(const string port_name)
{
    sp = new serial_port(service);

    sp->open(port_name);

    if (sp->is_open())
    {
        sp->set_option(serial_port::baud_rate(115200));
        sp->set_option(serial_port::flow_control(serial_port::flow_control::none));
        sp->set_option(serial_port::parity(serial_port::parity::none));
        sp->set_option(serial_port::stop_bits(serial_port::stop_bits::one));
        sp->set_option(serial_port::character_size(8));
        cout << "打开串口成功！" << endl;
    }
    else
    {
        cout << "打开串口失败！" << endl;
    }
}

int Boost_serial::serial_read(char buf[], int size)
{
    int num = read(*sp, buffer(buf, size));
    if (num <= 0)
    {
        cout << "没有读到数据！" << endl;
    }
    return num;
}

