Skip to content
Snippets Groups Projects
Commit 2e923a62 authored by Giordon Holtsberg Stark's avatar Giordon Holtsberg Stark Committed by Elisabetta Pianori
Browse files

feat: baud enum

parent 8f96e4e7
Branches
Tags
6 merge requests!308Bring main and devel back in sync,!300playing with fixing divergence between devel and main,!287Updates for Julabo Chiller,!285Julabo Chiller Added with Fixed Pipeline Compatibility,!269Merge devel into main: largest change is code formatting checker and enforcement in CI,!224feat: baud enum
Showing
with 168 additions and 59 deletions
......@@ -13,7 +13,8 @@
#include "Si7021.h"
int main(int argc, char* argv[]) {
std::shared_ptr<TextSerialCom> TC(new TextSerialCom("/dev/ttyACM2", B9600));
std::shared_ptr<TextSerialCom> TC(
new TextSerialCom("/dev/ttyACM2", SerialCom::BaudRate::Baud9600));
TC->setTermination("\r\n");
TC->init();
std::shared_ptr<I2CCom> i2c(new I2CDevComuino(0x40, TC));
......
......@@ -14,7 +14,8 @@ def si7021_example(device, baud, address) :
## initialize the communication line
##
try :
serial = com.TextSerialCom(device, baud)
baud_rate = getattr(com.SerialCom.BaudRate, 'Baud{0:d}'.format(baud))
serial = com.TextSerialCom(device, baud_rate)
except :
logger.error(f"Unable to initialize serial communication with device at port \"{device}\"")
sys.exit(1)
......
......@@ -39,7 +39,7 @@ int main(int argc, char* argv[]) {
// inputs
float target_temp = 15;
std::string device = "";
speed_t baud = B9600;
SerialCom::BaudRate baud = SerialCom::BaudRate::Baud9600;
int c;
while (true) {
......@@ -61,13 +61,13 @@ int main(int argc, char* argv[]) {
case 'b': {
std::string baud_str = optarg;
if (baud_str == "2400")
baud = B2400;
baud = SerialCom::BaudRate::Baud2400;
else if (baud_str == "4800")
baud = B4800;
baud = SerialCom::BaudRate::Baud4800;
else if (baud_str == "9600")
baud = B9600;
baud = SerialCom::BaudRate::Baud9600;
else if (baud_str == "19200")
baud = B19200;
baud = SerialCom::BaudRate::Baud19200;
else {
std::cerr << "Invalid baud rate '" << optarg
<< "' supplied. Use either 2400, 4800, 9600, or "
......
......@@ -14,7 +14,8 @@ def chiller_example(device, baud, temperature) :
## setup the communication line
##
try :
serial = com.TextSerialCom(device, baud)
baud_rate = getattr(com.SerialCom.BaudRate, 'Baud{0:d}'.format(baud))
serial = com.TextSerialCom(device, baud_rate)
except :
logger.error(f"Unable to initialize serial communication with device at port \"{device}\"")
sys.exit(1)
......
......@@ -13,7 +13,7 @@
// https://cdn-learn.adafruit.com/assets/assets/000/035/931/original/Support_Documents_TechnicalDocs_Si7021-A20.pdf
int main(int argc, char* argv[]) {
TextSerialCom com("/dev/ttyACM2", B9600);
TextSerialCom com("/dev/ttyACM2", SerialCom::BaudRate::Baud9600);
com.setTermination("\r\n");
com.init();
......
......@@ -25,7 +25,8 @@ def devcomuino_example(device, baud) :
## initialize the communication line
##
try :
serial = com.TextSerialCom(device, baud)
baud_rate = getattr(com.SerialCom.BaudRate, 'Baud{0:d}'.format(baud))
serial = com.TextSerialCom(device, baud_rate)
except :
logger.error(f"Unable to initialize serial communication with device at port \"{device}\"")
sys.exit(1)
......
......@@ -132,7 +132,7 @@ int main(int argc, char* argv[]) {
logger(logDEBUG) << "Pin: " << (int)pin;
std::shared_ptr<TextSerialCom> com =
std::make_shared<TextSerialCom>(port, B9600);
std::make_shared<TextSerialCom>(port, SerialCom::BaudRate::Baud9600);
com->setTermination("\r\n");
com->setTimeout(5);
com->init();
......
......@@ -33,7 +33,7 @@ def ntc_example(device, pin) :
## initialize the communication line
##
try :
serial = com.TextSerialCom(device, 9600)
serial = com.TextSerialCom(device, com.SerialCom.BaudRate.Baud9600)
except :
logger.error(f"Unable to initialize serial communication with device at port \"{device}\"")
sys.exit(1)
......
......@@ -19,7 +19,8 @@ int main(int argc, char* argv[]) {
MPSSEChip::Endianness::MSBFirst);
std::shared_ptr<I2CCom> i2c = std::make_shared<I2CFTDICom>(ftdi, 0x44);
#else
std::shared_ptr<TextSerialCom> TC(new TextSerialCom("/dev/ttyACM0", B9600));
std::shared_ptr<TextSerialCom> TC(
new TextSerialCom("/dev/ttyACM0", SerialCom::BaudRate::Baud9600));
TC->setTermination("\r\n");
TC->init();
......
......@@ -27,7 +27,8 @@ def sht85_example(use_ftdi, device, baud) :
## initialize the communication line
##
try :
serial = com.TextSerialCom(device, baud)
baud_rate = getattr(com.SerialCom.BaudRate, 'Baud{0:d}'.format(baud))
serial = com.TextSerialCom(device, baud_rate)
except :
logger.error(f"Unable to initialize serial communication with the device at port \"{device}\"")
sys.exit(1)
......
......@@ -87,7 +87,7 @@ int main(int argc, char** argv) {
}
// open serial device
speed_t baud = B9600;
SerialCom::BaudRate baud = SerialCom::BaudRate::Baud9600;
std::shared_ptr<TextSerialCom> sc(
new TextSerialCom("/dev/ttyACM" + std::to_string(rport), baud));
sc->init();
......
#include "Logger.h"
#include <pybind11/pybind11.h>
#include "Logger.h"
namespace py = pybind11;
void register_com(py::module&);
......
......@@ -19,7 +19,7 @@
*
* ```
* std::shared_ptr<TextSerialCom> com =
* std::make_shared<TextSerialCom>("/dev/ttyUSB0",B9600);
* std::make_shared<TextSerialCom>("/dev/ttyUSB0",SerialCom::BaudRate::Baud9600);
* com->setTermination("\r");
* com->init();
*
......
......@@ -16,8 +16,9 @@
REGISTER_COM(GPIBSerialCom)
GPIBSerialCom::GPIBSerialCom(uint16_t gpib_addr, const std::string& port,
speed_t baud, bool parityBit, bool twoStopBits,
bool flowControl, CharSize charsize)
SerialCom::BaudRate baud, bool parityBit,
bool twoStopBits, bool flowControl,
SerialCom::CharSize charsize)
: TextSerialCom(port, baud, parityBit, twoStopBits, flowControl, charsize),
m_gpib_addr(gpib_addr) {}
......
......@@ -26,10 +26,12 @@ class GPIBSerialCom : public TextSerialCom {
* @param flowControl Enable hardware flow control
* @param charsize Size of a character
*/
GPIBSerialCom(uint16_t gpib_addr, const std::string& port,
speed_t baud = B19200, bool parityBit = false,
bool twoStopBits = false, bool flowControl = false,
CharSize charsize = CharSize::CharSize8);
GPIBSerialCom(
uint16_t gpib_addr, const std::string& port,
SerialCom::BaudRate baud = SerialCom::BaudRate::Baud19200,
bool parityBit = false, bool twoStopBits = false,
bool flowControl = false,
SerialCom::CharSize charsize = SerialCom::CharSize::CharSize8);
GPIBSerialCom();
~GPIBSerialCom();
......
......@@ -17,19 +17,46 @@
#include "ComRegistry.h"
REGISTER_COM(SerialCom)
const std::unordered_map<std::string, speed_t> SerialCom::mapBAUDRATE = {
{"B0", B0}, {"B50", B50}, {"B75", B75}, {"B110", B110},
{"B134", B134}, {"B150", B150}, {"B200", B200}, {"B300", B300},
{"B600", B600}, {"B1200", B1200}, {"B1800", B1800}, {"B2400", B2400},
{"B4800", B4800}, {"B9600", B9600}, {"B19200", B19200}, {"B38400", B38400}};
const std::unordered_map<std::string, SerialCom::BaudRate>
SerialCom::mapBAUDRATE = {{"B0", SerialCom::BaudRate::Baud0},
{"B50", SerialCom::BaudRate::Baud50},
{"B75", SerialCom::BaudRate::Baud75},
{"B110", SerialCom::BaudRate::Baud110},
{"B134", SerialCom::BaudRate::Baud134},
{"B150", SerialCom::BaudRate::Baud150},
{"B200", SerialCom::BaudRate::Baud200},
{"B300", SerialCom::BaudRate::Baud300},
{"B600", SerialCom::BaudRate::Baud600},
{"B1200", SerialCom::BaudRate::Baud1200},
{"B1800", SerialCom::BaudRate::Baud1800},
{"B2400", SerialCom::BaudRate::Baud2400},
{"B4800", SerialCom::BaudRate::Baud4800},
{"B9600", SerialCom::BaudRate::Baud9600},
{"B19200", SerialCom::BaudRate::Baud19200},
{"B38400", SerialCom::BaudRate::Baud38400},
{"B57600", SerialCom::BaudRate::Baud57600},
{"B115200", SerialCom::BaudRate::Baud115200},
{"B230400", SerialCom::BaudRate::Baud230400},
{"B460800", SerialCom::BaudRate::Baud460800},
{"B500000", SerialCom::BaudRate::Baud500000},
{"B576000", SerialCom::BaudRate::Baud576000},
{"B921600", SerialCom::BaudRate::Baud921600},
{"B1000000", SerialCom::BaudRate::Baud1000000},
{"B1152000", SerialCom::BaudRate::Baud1152000},
{"B1500000", SerialCom::BaudRate::Baud1500000},
{"B2000000", SerialCom::BaudRate::Baud2000000},
{"B2500000", SerialCom::BaudRate::Baud2500000},
{"B3000000", SerialCom::BaudRate::Baud3000000},
{"B3500000", SerialCom::BaudRate::Baud3500000},
{"B4000000", SerialCom::BaudRate::Baud4000000}};
const std::unordered_map<std::string, SerialCom::CharSize>
SerialCom::mapCHARSIZE = {{"CS5", CharSize5},
{"CS6", CharSize6},
{"CS7", CharSize7},
{"CS8", CharSize8}};
SerialCom::mapCHARSIZE = {{"CS5", SerialCom::CharSize::CharSize5},
{"CS6", SerialCom::CharSize::CharSize6},
{"CS7", SerialCom::CharSize::CharSize7},
{"CS8", SerialCom::CharSize::CharSize8}};
SerialCom::SerialCom(const std::string &port, speed_t baud, bool parityBit,
SerialCom::SerialCom(const std::string &port, BaudRate baud, bool parityBit,
bool twoStopBits, bool flowControl, CharSize charsize)
: m_port(port),
m_baudrate(baud),
......@@ -98,11 +125,12 @@ void SerialCom::config() {
logger(logDEBUG3) << __PRETTY_FUNCTION__;
logger(logDEBUG3) << "Configuring serial device " << m_port;
logger(logDEBUG3) << " Baud Rate: " << m_baudrate;
logger(logDEBUG3) << " Baud Rate: " << static_cast<speed_t>(m_baudrate);
logger(logDEBUG3) << " Enable parity bit: " << m_parityBit;
logger(logDEBUG3) << " Use two stop bits: " << m_twoStopBits;
logger(logDEBUG3) << " Enable hardware flow control: " << m_flowControl;
logger(logDEBUG3) << " Character size: " << m_charsize;
logger(logDEBUG3) << " Character size: "
<< static_cast<tcflag_t>(m_charsize);
ScopeLock lock(this);
if (tcgetattr(m_dev, &m_tty))
......@@ -111,8 +139,8 @@ void SerialCom::config() {
m_tty_old = m_tty;
cfsetospeed(&m_tty, m_baudrate);
cfsetispeed(&m_tty, m_baudrate);
cfsetospeed(&m_tty, static_cast<speed_t>(m_baudrate));
cfsetispeed(&m_tty, static_cast<speed_t>(m_baudrate));
if (m_parityBit)
m_tty.c_cflag &= PARENB;
......@@ -130,7 +158,7 @@ void SerialCom::config() {
m_tty.c_cflag &= ~CRTSCTS;
m_tty.c_cflag &= ~CSIZE;
m_tty.c_cflag |= m_charsize;
m_tty.c_cflag |= static_cast<tcflag_t>(m_charsize);
m_tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
cfmakeraw(&m_tty);
......
......@@ -24,8 +24,43 @@
*/
class SerialCom : public ICom {
public:
/** Available baud rates */
enum class BaudRate : speed_t {
Baud0 = B0,
Baud50 = B50,
Baud75 = B75,
Baud110 = B110,
Baud134 = B134,
Baud150 = B150,
Baud200 = B200,
Baud300 = B300,
Baud600 = B600,
Baud1200 = B1200,
Baud1800 = B1800,
Baud2400 = B2400,
Baud4800 = B4800,
Baud9600 = B9600,
Baud19200 = B19200,
Baud38400 = B38400,
Baud57600 = B57600,
Baud115200 = B115200,
Baud230400 = B230400,
Baud460800 = B460800,
Baud500000 = B500000,
Baud576000 = B576000,
Baud921600 = B921600,
Baud1000000 = B1000000,
Baud1152000 = B1152000,
Baud1500000 = B1500000,
Baud2000000 = B2000000,
Baud2500000 = B2500000,
Baud3000000 = B3000000,
Baud3500000 = B3500000,
Baud4000000 = B4000000
};
/** Available character sizes */
enum CharSize {
enum class CharSize : tcflag_t {
CharSize5 = CS5,
CharSize6 = CS6,
CharSize7 = CS7,
......@@ -41,7 +76,7 @@ class SerialCom : public ICom {
* @param flowControl Enable hardware flow control
* @param charsize Size of a character
*/
SerialCom(const std::string& port, speed_t baud = B19200,
SerialCom(const std::string& port, BaudRate baud = BaudRate::Baud19200,
bool parityBit = false, bool twoStopBits = false,
bool flowControl = false,
CharSize charsize = CharSize::CharSize8);
......@@ -187,7 +222,7 @@ class SerialCom : public ICom {
std::string m_port;
/// Baud rate to use
speed_t m_baudrate = B19200;
BaudRate m_baudrate = BaudRate::Baud19200;
/// Use parity bit
bool m_parityBit = false;
......@@ -228,8 +263,9 @@ class SerialCom : public ICom {
//! Temporary buffer for ::read()
char m_tmpbuf[MAX_READ];
//! Maps valid baud rate settings to `speed_t` type
static const std::unordered_map<std::string, speed_t> mapBAUDRATE;
//! Maps valid baud rate settings to `BaudRate` type
static const std::unordered_map<std::string, SerialCom::BaudRate>
mapBAUDRATE;
//! Maps valid char size settings to `CharSize` type
static const std::unordered_map<std::string, SerialCom::CharSize>
......
......@@ -15,9 +15,9 @@
#include "ComRegistry.h"
REGISTER_COM(TextSerialCom)
TextSerialCom::TextSerialCom(const std::string& port, speed_t baud,
TextSerialCom::TextSerialCom(const std::string& port, SerialCom::BaudRate baud,
bool parityBit, bool twoStopBits, bool flowControl,
CharSize charsize)
SerialCom::CharSize charsize)
: SerialCom(port, baud, parityBit, twoStopBits, flowControl, charsize) {}
TextSerialCom::TextSerialCom() : SerialCom() {}
......
......@@ -22,10 +22,12 @@ class TextSerialCom : public SerialCom {
* @param flowControl Enable hardware flow control
* @param charsize Size of a character
*/
TextSerialCom(const std::string& port, speed_t baud = B19200,
bool parityBit = false, bool twoStopBits = false,
bool flowControl = false,
CharSize charsize = CharSize::CharSize8);
TextSerialCom(
const std::string& port,
SerialCom::BaudRate baud = SerialCom::BaudRate::Baud19200,
bool parityBit = false, bool twoStopBits = false,
bool flowControl = false,
SerialCom::CharSize charsize = SerialCom::CharSize::CharSize8);
TextSerialCom();
~TextSerialCom() = default;
......
......@@ -149,6 +149,39 @@ void register_com(py::module &m) {
py::class_<SerialCom, PyCom<SerialCom>, ICom, std::shared_ptr<SerialCom>>
py_serialcom(m, "SerialCom");
// nb: enum defined first because it is used in py_serialcom init() default
py::enum_<SerialCom::BaudRate>(py_serialcom, "BaudRate")
.value("Baud0", SerialCom::BaudRate::Baud0)
.value("Baud50", SerialCom::BaudRate::Baud50)
.value("Baud75", SerialCom::BaudRate::Baud75)
.value("Baud110", SerialCom::BaudRate::Baud110)
.value("Baud134", SerialCom::BaudRate::Baud134)
.value("Baud150", SerialCom::BaudRate::Baud150)
.value("Baud200", SerialCom::BaudRate::Baud200)
.value("Baud300", SerialCom::BaudRate::Baud300)
.value("Baud600", SerialCom::BaudRate::Baud600)
.value("Baud1200", SerialCom::BaudRate::Baud1200)
.value("Baud1800", SerialCom::BaudRate::Baud1800)
.value("Baud2400", SerialCom::BaudRate::Baud2400)
.value("Baud4800", SerialCom::BaudRate::Baud4800)
.value("Baud9600", SerialCom::BaudRate::Baud9600)
.value("Baud19200", SerialCom::BaudRate::Baud19200)
.value("Baud38400", SerialCom::BaudRate::Baud38400)
.value("Baud57600", SerialCom::BaudRate::Baud57600)
.value("Baud115200", SerialCom::BaudRate::Baud115200)
.value("Baud230400", SerialCom::BaudRate::Baud230400)
.value("Baud460800", SerialCom::BaudRate::Baud460800)
.value("Baud500000", SerialCom::BaudRate::Baud500000)
.value("Baud576000", SerialCom::BaudRate::Baud576000)
.value("Baud921600", SerialCom::BaudRate::Baud921600)
.value("Baud1000000", SerialCom::BaudRate::Baud1000000)
.value("Baud1152000", SerialCom::BaudRate::Baud1152000)
.value("Baud1500000", SerialCom::BaudRate::Baud1500000)
.value("Baud2000000", SerialCom::BaudRate::Baud2000000)
.value("Baud2500000", SerialCom::BaudRate::Baud2500000)
.value("Baud3000000", SerialCom::BaudRate::Baud3000000)
.value("Baud3500000", SerialCom::BaudRate::Baud3500000)
.value("Baud4000000", SerialCom::BaudRate::Baud4000000);
py::enum_<SerialCom::CharSize>(py_serialcom, "CharSize")
.value("CharSize5", SerialCom::CharSize::CharSize5)
.value("CharSize6", SerialCom::CharSize::CharSize6)
......@@ -156,9 +189,9 @@ void register_com(py::module &m) {
.value("CharSize8", SerialCom::CharSize::CharSize8);
py_serialcom
.def(py::init<const std::string &, speed_t, bool, bool, bool,
SerialCom::CharSize>(),
py::arg("port"), py::arg("baud") = B19200,
.def(py::init<const std::string &, SerialCom::BaudRate, bool, bool,
bool, SerialCom::CharSize>(),
py::arg("port"), py::arg("baud") = SerialCom::BaudRate::Baud19200,
py::arg("parityBit") = false, py::arg("twoStopBits") = false,
py::arg("flowControl") = false,
py::arg("charsize") = SerialCom::CharSize::CharSize8)
......@@ -167,9 +200,9 @@ void register_com(py::module &m) {
py::class_<TextSerialCom, PyTextSerialCom, SerialCom,
std::shared_ptr<TextSerialCom>>(m, "TextSerialCom")
.def(py::init<const std::string &, speed_t, bool, bool, bool,
SerialCom::CharSize>(),
py::arg("port"), py::arg("baud") = B19200,
.def(py::init<const std::string &, SerialCom::BaudRate, bool, bool,
bool, SerialCom::CharSize>(),
py::arg("port"), py::arg("baud") = SerialCom::BaudRate::Baud19200,
py::arg("parityBit") = false, py::arg("twoStopBits") = false,
py::arg("flowControl") = false,
py::arg("charsize") = SerialCom::CharSize::CharSize8)
......@@ -178,9 +211,10 @@ void register_com(py::module &m) {
py::class_<GPIBSerialCom, PyGPIBSerialCom, TextSerialCom,
std::shared_ptr<GPIBSerialCom>>(m, "GPIBSerialCom")
.def(py::init<uint16_t, const std::string &, speed_t, bool, bool, bool,
SerialCom::CharSize>(),
py::arg("gpib_addr"), py::arg("port"), py::arg("baud") = B19200,
.def(py::init<uint16_t, const std::string &, SerialCom::BaudRate, bool,
bool, bool, SerialCom::CharSize>(),
py::arg("gpib_addr"), py::arg("port"),
py::arg("baud") = SerialCom::BaudRate::Baud19200,
py::arg("parityBit") = false, py::arg("twoStopBits") = false,
py::arg("flowControl") = false,
py::arg("charsize") = SerialCom::CharSize::CharSize8);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment