Skip to content

Commit

Permalink
it builds, need to fix runner assignment in connect, so that runner.j…
Browse files Browse the repository at this point in the history
…oin() can happen in the descructor
  • Loading branch information
caseykelso committed Mar 26, 2017
1 parent 2ab361b commit 4723f74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
36 changes: 17 additions & 19 deletions app/serialport.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#include <cstdio>
#include <string>
#include <asio.hpp>
#include <cstddef>
#include <iostream>
#include <thread>
#include <functional>
#include "serialport.h"

SerialClass::SerialClass():
port(io){};
// quitFlag(false){};
port(io),quitFlag(false){};


SerialClass::~SerialClass()
{
//Stop the I/O services
io.stop();
//Wait for the thread to finish
// runner.join();
runner.join();
}

bool SerialClass::connect(const std::string& port_name, int baud)
Expand All @@ -23,14 +30,8 @@

if (port.is_open())
{
//Start io-service in a background thread.
//boost::bind binds the ioservice instance
//with the method call
/* runner = boost::thread(
boost::bind(
&boost::asio::io_service::run,
&io));
*/
//runner =
std::bind(static_cast<std::size_t (asio::io_service::*)()>(&asio::io_service::run), &io);
startReceive();
}

Expand All @@ -39,24 +40,21 @@

void SerialClass::startReceive()
{
#if 0
using namespace asio;
//Issue a async receive and give it a callback
//onData that should be called when "\r\n"
//is matched.
async_read_until(port, buffer,
"\r\n",
boost::bind(&SerialClass::onData,
this, _1,_2));
#endif
std::bind(&SerialClass::onData,
this, std::placeholders::_1,std::placeholders::_2));
}

void SerialClass::send(const std::string& text)
{
asio::write(port, asio::buffer(text));
}
#if 0
void onData(const system::error_code& e,
void SerialClass::onData(const asio::error_code& e,
std::size_t size)
{
if (!e)
Expand All @@ -74,8 +72,8 @@

startReceive();
};
#endif
// bool SerialClass::quit(){return quitFlag;}

bool SerialClass::quit(){return quitFlag;}



Expand Down
12 changes: 6 additions & 6 deletions app/serialport.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include <string>
#include <asio.hpp>

class SerialClass
{
public:
Expand All @@ -15,20 +12,23 @@ class SerialClass
void startReceive();
//Function for sending a data string
void send(const std::string& text);
bool quit();

private:
//The callback function that will be executed when data
//arrives.
// void onData(const asio::system::error_code& e,
// std::size_t size)
void onData(const asio::error_code& e, std::size_t size);


//Boost.Asio I/O service required for asynchronous
//operation
asio::io_service io;
//Serial port accessor class
asio::serial_port port;
//Background thread
// thread runner;
std::thread runner;
//Buffer in which to read the serial data
asio::streambuf buffer;
bool quitFlag;
};

0 comments on commit 4723f74

Please sign in to comment.