Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNS: Serial Communication #120

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
27 changes: 14 additions & 13 deletions src/utils/io/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include <iostream>
#include <sstream>

#include <utils/system.hpp>

namespace hyped::utils::io {

SerialProtocol::SerialProtocol(const std::string serial, const BaudRate baud_rate,
utils::Logger &log)
SerialProtocol::SerialProtocol(const std::string serial, const BaudRate baud_rate)
kshxtij marked this conversation as resolved.
Show resolved Hide resolved
: serial_device_(serial),
baud_rate_(baud_rate),
log_(log)
log_(utils::Logger("SERIAL", utils::System::getSystem().config_.log_level))
{
configureTermios();
}
Expand Down Expand Up @@ -45,31 +46,31 @@ void SerialProtocol::configureTermios()
options.c_oflag = 0;
options.c_lflag = 0;
switch (baud_rate_) {
case BaudRate::kB_300:
case BaudRate::kB300:
cfsetispeed(&options, B300);
cfsetospeed(&options, B300);
break;
case BaudRate::kB_600:
case BaudRate::kB600:
cfsetispeed(&options, B600);
cfsetospeed(&options, B600);
break;
case BaudRate::kB_1200:
case BaudRate::kB1200:
cfsetispeed(&options, B1200);
cfsetospeed(&options, B1200);
break;
case BaudRate::kB_2400:
case BaudRate::kB2400:
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);
break;
case BaudRate::kB_4800:
case BaudRate::kB4800:
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
break;
case BaudRate::kB_9600:
case BaudRate::kB9600:
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
break;
case BaudRate::kB_19200:
case BaudRate::kB19200:
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
break;
Expand All @@ -90,14 +91,14 @@ void SerialProtocol::readData(std::vector<uint8_t> &data)
return;
}

size_t n = read(serial_, &readBuffer_[0], readBufferSize_B_);
const size_t bytesRead = read(serial_, &readBuffer_[0], readBufferSize_B_);
kshxtij marked this conversation as resolved.
Show resolved Hide resolved

if (n < 0) {
if (bytesRead < 0) {
log_.error("Error reading from serial device.");
return;
}

copy(readBuffer_.begin(), readBuffer_.begin() + n, back_inserter(data));
std::copy(readBuffer_.begin(), readBuffer_.begin() + bytesRead, back_inserter(data));
return;
}

Expand Down
29 changes: 3 additions & 26 deletions src/utils/io/serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,10 @@
#include <utils/logger.hpp>
namespace hyped::utils::io {

enum class SerialState {
kNoSerial = 0,
kSuccess = 1,
// There is not (enough) data to process
kNoData = 2,
// The object is being received but the buffer doesn't have all the data
kWaitingForData = 3,
// The size of the received payload doesn't match the expected size
kInvalidSize = 4,
// The object was received but it is not the same as one sent
kInvalidChecksum = 5
};

enum class BaudRate {
kB_300,
kB_600,
kB_1200,
kB_2400,
kB_4800,
kB_9600,
kB_14400,
kB_19200,
kB_28800
};
enum class BaudRate { kB300, kB600, kB1200, kB2400, kB4800, kB9600, kB14400, kB19200, kB28800 };
mifrandir marked this conversation as resolved.
Show resolved Hide resolved
class SerialProtocol {
public:
SerialProtocol(std::string serial, BaudRate baud_rate, Logger &log);
SerialProtocol(std::string serial, BaudRate baud_rate);
~SerialProtocol();

void configureTermios();
Expand All @@ -50,7 +27,7 @@ class SerialProtocol {
std::string serial_device_;
int serial_;
BaudRate baud_rate_;
utils::Logger &log_;
utils::Logger log_;

std::vector<char> readBuffer_;
unsigned char readBufferSize_B_;
Expand Down