Skip to content

Commit

Permalink
Update serial device flags
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed May 5, 2024
1 parent b6b937d commit 797ac15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions include/SerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SerialPort {
std::condition_variable cv;

void readLoop();
void configurePort(int baud_rate);

public:
SerialPort(const std::string& device, int baud_rate);
Expand Down
36 changes: 22 additions & 14 deletions src/SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@
#include <stdexcept>

SerialPort::SerialPort(const std::string& device, int baud_rate) {
Logger(Logger::Level::Info) << "SerialPort init start - device " << device << " - baudRate - " << baud_rate;
Logger(Logger::Level::Info) << "SerialPort init start - device " << device << " - baudRate - " << baud_rate << std::endl;
serial_fd = open(device.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (serial_fd < 0) {
Logger(Logger::Level::Error) << "Error opening serial port: " << strerror(errno) << std::endl;
throw std::runtime_error("Error opening serial port");
}

configurePort(baud_rate);
Logger(Logger::Level::Info) << "SerialPort init finish" << std::endl;
}

SerialPort::~SerialPort() {
close(serial_fd);
}


void SerialPort::configurePort(int baud_rate) {
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(serial_fd, &tty) != 0) {
Logger(Logger::Level::Error) << "Error from tcgetattr: " << strerror(errno) << std::endl;
throw std::runtime_error("Error from tcgetattr");
}

Expand All @@ -23,28 +35,24 @@ SerialPort::SerialPort(const std::string& device, int baud_rate) {

tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CRTSCTS;
tty.c_cflag |= CS8; // 8-bit chars
tty.c_cflag &= ~PARENB; // no parity bit
tty.c_cflag &= ~CSTOPB; // only need 1 stop bit
tty.c_cflag &= ~CRTSCTS; // no hardware flowcontrol

// tty.c_iflag |= (IXON | IXOFF | IXANY);
// setup for raw input
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;

tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
// special characters
tty.c_cc[VMIN] = 1; // minimum number of characters to read
tty.c_cc[VTIME] = 1; // timeout in deciseconds for noncanonical read

if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {
Logger(Logger::Level::Error) << "Error from tcsetattr: " << strerror(errno) << std::endl;
throw std::runtime_error("Error from tcsetattr");
}
Logger(Logger::Level::Info) << "SerialPort init finish";
}

SerialPort::~SerialPort() {
close(serial_fd);
}

void SerialPort::writeData(const std::string& data) {
Expand Down

0 comments on commit 797ac15

Please sign in to comment.