BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
015b5f8
Branches
Tags
master
Blinker
src
linux
Device.cpp
open device also for writing
Stefan Schuermans
commited
015b5f8
at 2011-12-11 21:38:16
Device.cpp
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <string> #include <strings.h> #include <termios.h> #include <unistd.h> #include "Device.h" #include "Io.h" #include "SerCfg.h" namespace Blinker { /** * @brief constructor * @param[in] path path to device */ Device::Device(const std::string &path) { // open device m_fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); } /// destructor Device::~Device() { // exit if not initialized if (m_fd == -1) return; // close device close(m_fd); } /** * @brief set serial port configuration * @param[in] serCfg serial port configuration * @return if configuration succeeded * * This function should only be used if device is a serial port. */ bool Device::setSerCfg(const SerCfg& serCfg) { struct termios tio; // exit if not initialized if (m_fd == -1) return false; // set up serial port configuration structure bzero(&tio, sizeof(tio)); tio.c_cflag = CLOCAL | HUPCL | CREAD; tio.c_iflag = IGNBRK | IGNPAR; tio.c_oflag = 0; tio.c_lflag = 0; tio.c_cc[VTIME] = 1; // 0.1 sec timeout tio.c_cc[VMIN] = 0; // return on single char read switch (serCfg.m_baud) { case 300: tio.c_cflag |= B300; break; case 600: tio.c_cflag |= B600; break; case 1200: tio.c_cflag |= B1200; break; case 2400: tio.c_cflag |= B2400; break; case 4800: tio.c_cflag |= B4800; break; case 9600: tio.c_cflag |= B9600; break; case 19200: tio.c_cflag |= B19200; break; case 38400: tio.c_cflag |= B38400; break; case 57600: tio.c_cflag |= B57600; break; case 115200: tio.c_cflag |= B115200; break; default: return false; // invalid setting } switch (serCfg.m_data) { case 5: tio.c_cflag |= CS5; break; case 6: tio.c_cflag |= CS6; break; case 7: tio.c_cflag |= CS7; break; case 8: tio.c_cflag |= CS8; break; default: return false; // invalid setting } switch (serCfg.m_parity) { case SerCfg::ParityNone: break; case SerCfg::ParityEven: tio.c_cflag |= PARENB; break; case SerCfg::ParityOdd: tio.c_cflag |= PARENB | PARODD; break; default: return false; // invalid setting } switch (serCfg.m_stop) { case 1: break; case 2: tio.c_cflag |= CSTOPB; break; default: return false; // invalid setting } // configure serial port return tcsetattr(m_fd, TCSANOW, &tio) != -1; } /** * @brief write data to device * @param[in] data data to write * @return if the data could be written */ bool Device::write(const std::string &data) { // exit if not initialized if (m_fd == -1) return false; // write data return ::write(m_fd, data.c_str(), data.size()) == (ssize_t)data.size(); } } // namespace Blinker