Add getters for number of corrupt and total packets received over serial

This commit is contained in:
jacobperron 2016-02-12 17:22:15 -08:00
parent d379c46012
commit 8a3209d6a1
2 changed files with 35 additions and 26 deletions

View file

@ -100,6 +100,8 @@ namespace create {
inline bool connected() const { return port.is_open(); }; inline bool connected() const { return port.is_open(); };
bool send(const uint8_t* bytes, const uint32_t numBytes); bool send(const uint8_t* bytes, const uint32_t numBytes);
bool sendOpcode(const Opcode& code); bool sendOpcode(const Opcode& code);
uint64_t getNumCorruptPackets();
uint64_t getNumTotalPackets();
}; };
} // namespace create } // namespace create

View file

@ -4,31 +4,31 @@
#include "create/types.h" #include "create/types.h"
namespace create { namespace create {
Serial::Serial(boost::shared_ptr<Data> d, const uint8_t& header) : Serial::Serial(boost::shared_ptr<Data> d, const uint8_t& header) :
data(d), data(d),
headerByte(header), headerByte(header),
port(io), port(io),
readState(READ_HEADER), readState(READ_HEADER),
isReading(false), isReading(false),
dataReady(false), dataReady(false),
corruptPackets(0), corruptPackets(0),
totalPackets(0) { totalPackets(0) {
//std::cout << "# Serial Created" << std::endl; //std::cout << "# Serial Created" << std::endl;
} }
Serial::~Serial() { Serial::~Serial() {
disconnect(); disconnect();
//std::cout << "# Serial Destroyed" << std::endl; //std::cout << "# Serial Destroyed" << std::endl;
} }
bool Serial::connect(const std::string& portName, const int& baud, boost::function<void()> cb) { bool Serial::connect(const std::string& portName, const int& baud, boost::function<void()> cb) {
//std::cout << "## Serial connect start" << std::endl; //std::cout << "## Serial connect start" << std::endl;
using namespace boost::asio; using namespace boost::asio;
port.open(portName); port.open(portName);
port.set_option(serial_port::baud_rate(baud)); port.set_option(serial_port::baud_rate(baud));
port.set_option(serial_port::flow_control(serial_port::flow_control::none)); port.set_option(serial_port::flow_control(serial_port::flow_control::none));
if (port.is_open()) { if (port.is_open()) {
callback = cb; callback = cb;
bool startReadSuccess = startReading(); bool startReadSuccess = startReading();
@ -40,7 +40,7 @@ namespace create {
//std::cout << "## Serial connect failed" << std::endl; //std::cout << "## Serial connect failed" << std::endl;
return false; return false;
} }
void Serial::disconnect() { void Serial::disconnect() {
if (isReading) { if (isReading) {
stopReading(); stopReading();
@ -56,7 +56,7 @@ namespace create {
//std::cout << "## Serial disconnect done" << std::endl; //std::cout << "## Serial disconnect done" << std::endl;
} }
} }
bool Serial::startReading() { bool Serial::startReading() {
if (!connected()) return false; if (!connected()) return false;
@ -85,10 +85,10 @@ namespace create {
sendOpcode(OC_START); sendOpcode(OC_START);
// Start streaming data // Start streaming data
send(streamReq, 2 + numPackets); send(streamReq, 2 + numPackets);
expectedNumBytes = data->getTotalDataBytes() + numPackets; expectedNumBytes = data->getTotalDataBytes() + numPackets;
//TODO: handle boost exceptions //TODO: handle boost exceptions
io.reset(); io.reset();
@ -98,7 +98,7 @@ namespace create {
boost::asio::buffer(&byteRead, 1), boost::asio::buffer(&byteRead, 1),
boost::bind(&Serial::onData, this, _1, _2)); boost::bind(&Serial::onData, this, _1, _2));
ioThread = boost::thread(boost::bind(&boost::asio::io_service::run, &io)); ioThread = boost::thread(boost::bind(&boost::asio::io_service::run, &io));
// Wait for first complete read to finish // Wait for first complete read to finish
boost::unique_lock<boost::mutex> lock(dataReadyMut); boost::unique_lock<boost::mutex> lock(dataReadyMut);
@ -112,12 +112,12 @@ namespace create {
io.stop(); io.stop();
ioThread.join(); ioThread.join();
return false; return false;
} }
attempts++; attempts++;
//std::cout << "Requesting data from Create. Attempt " << attempts << std::endl; //std::cout << "Requesting data from Create. Attempt " << attempts << std::endl;
// Request data again // Request data again
sendOpcode(OC_START); sendOpcode(OC_START);
send(streamReq, 2 + numPackets); send(streamReq, 2 + numPackets);
} }
} }
//std::cout << "#### Data is ready." << std::endl; //std::cout << "#### Data is ready." << std::endl;
@ -125,7 +125,7 @@ namespace create {
//std::cout << "### Serial start reading DONE" << std::endl; //std::cout << "### Serial start reading DONE" << std::endl;
return true; return true;
} }
void Serial::stopReading() { void Serial::stopReading() {
if (isReading) { if (isReading) {
//std::cout << "### Start stopReading" << std::endl; //std::cout << "### Start stopReading" << std::endl;
@ -146,7 +146,7 @@ namespace create {
CERR("[create::Serial] ", "serial error - " << e.message()); CERR("[create::Serial] ", "serial error - " << e.message());
return; return;
} }
// Should have read exactly one byte // Should have read exactly one byte
if (size == 1) { if (size == 1) {
numBytesRead++; numBytesRead++;
@ -158,16 +158,16 @@ namespace create {
byteSum = byteRead; byteSum = byteRead;
} }
break; break;
case READ_NBYTES: case READ_NBYTES:
if (byteRead == expectedNumBytes) { if (byteRead == expectedNumBytes) {
readState = READ_PACKET_ID; readState = READ_PACKET_ID;
numBytesRead = 0; numBytesRead = 0;
} }
else else
readState = READ_HEADER; readState = READ_HEADER;
break; break;
case READ_PACKET_ID: case READ_PACKET_ID:
packetID = byteRead; packetID = byteRead;
if (data->isValidPacketID(packetID)) { if (data->isValidPacketID(packetID)) {
@ -181,7 +181,7 @@ namespace create {
readState = READ_HEADER; readState = READ_HEADER;
} }
break; break;
case READ_PACKET_BYTES: case READ_PACKET_BYTES:
numDataBytesRead++; numDataBytesRead++;
if (expectedNumDataBytes == 2 && numDataBytesRead == 1) { if (expectedNumDataBytes == 2 && numDataBytesRead == 1) {
@ -200,7 +200,7 @@ namespace create {
readState = READ_PACKET_ID; readState = READ_PACKET_ID;
} }
break; break;
case READ_CHECKSUM: case READ_CHECKSUM:
if ((byteSum & 0xFF) == 0) { if ((byteSum & 0xFF) == 0) {
// Validate all packets // Validate all packets
@ -218,7 +218,7 @@ namespace create {
} }
// Callback to notify data is ready // Callback to notify data is ready
if (callback) if (callback)
callback(); callback();
} }
else { else {
// Corrupt data // Corrupt data
@ -236,10 +236,10 @@ namespace create {
boost::asio::buffer(&byteRead, 1), boost::asio::buffer(&byteRead, 1),
boost::bind(&Serial::onData, this, _1, _2)); boost::bind(&Serial::onData, this, _1, _2));
} }
bool Serial::send(const uint8_t* bytes, unsigned int numBytes) { bool Serial::send(const uint8_t* bytes, unsigned int numBytes) {
if (!connected()) { if (!connected()) {
CERR("[create::Serial] ", "send failed, not connected."); CERR("[create::Serial] ", "send failed, not connected.");
return false; return false;
} }
// TODO: catch boost exceptions // TODO: catch boost exceptions
@ -252,4 +252,11 @@ namespace create {
return send(&oc, 1); return send(&oc, 1);
} }
uint64_t Serial::getNumCorruptPackets() {
return corruptPackets;
}
uint64_t Serial::getNumTotalPackets() {
return totalPackets;
}
} // namespace create } // namespace create