diff --git a/control/src/.idea/workspace.xml b/control/src/.idea/workspace.xml index 0d20e09..54240d5 100644 --- a/control/src/.idea/workspace.xml +++ b/control/src/.idea/workspace.xml @@ -12,7 +12,11 @@ + + + + @@ -40,7 +44,7 @@ - + @@ -48,13 +52,25 @@ - - + + - - + + - + + + + + + + + + + + + + @@ -63,7 +79,7 @@ - + @@ -72,7 +88,7 @@ - + @@ -81,8 +97,17 @@ - - + + + + + + + + + + + @@ -110,10 +135,12 @@ @@ -197,18 +224,18 @@ 1559118962147 - + - - + @@ -220,7 +247,7 @@ - + @@ -235,13 +262,6 @@ - - - - - - - @@ -256,46 +276,73 @@ - - - - - - - - - - - - - - - + - + - - + + - + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/control/src/cli.cpp b/control/src/cli.cpp index 5f76d3c..6abc7a2 100644 --- a/control/src/cli.cpp +++ b/control/src/cli.cpp @@ -1,4 +1,4 @@ -#define B15F_CLI_DEBUG +//#define B15F_CLI_DEBUG #include #include // sudo apt-get install libncurses5-dev diff --git a/control/src/drv/b15f.cpp b/control/src/drv/b15f.cpp index b4a47a0..a000db6 100644 --- a/control/src/drv/b15f.cpp +++ b/control/src/drv/b15f.cpp @@ -143,7 +143,6 @@ std::vector B15F::getBoardInfo(void) uint8_t n; usart.receive(&n, 0, sizeof(n)); - std::cout << "len: " << (int) n << std::endl << std::flush; while (n--) { uint8_t len; @@ -153,8 +152,6 @@ std::vector B15F::getBoardInfo(void) str[len] = '\0'; usart.receive(reinterpret_cast(&str[0]), 0, len); - std::cout << &str[0] << std::endl; - info.push_back(std::string(str)); } diff --git a/control/src/drv/timeoutexception.h b/control/src/drv/timeoutexception.h index a0be40f..d59fe24 100644 --- a/control/src/drv/timeoutexception.h +++ b/control/src/drv/timeoutexception.h @@ -2,36 +2,45 @@ #define TIMEOUTEXCEPTION_H #include - -// SOURCE: https://stackoverflow.com/a/8152888 +#include /*! Exception for USART related timeouts. */ class TimeoutException: public std::exception { public: - explicit TimeoutException(const char* message, int timeout) : TimeoutException(std::string(message), timeout) + /** + * Constructor + * @param message as c-string + */ + explicit TimeoutException(const char* message) : msg(message) { } - explicit TimeoutException(const std::string& message, int timeout) : msg(message), m_timeout(timeout) - { - if(!msg.length()) - msg = "Timeout reached (" + std::to_string(m_timeout) + ")"; - } - - virtual ~TimeoutException() throw () + /** + * Constructor + * @param message as c++-string + */ + explicit TimeoutException(const std::string& message) : msg(message) { } + /** + * Standard-destructor + */ + virtual ~TimeoutException() = default; + + /** + * Get failure description + * @return error message as c-string + */ virtual const char* what() const throw () { return msg.c_str(); } protected: - std::string msg; - int m_timeout; + std::string msg; //!< failure description }; #endif // TIMEOUTEXCEPTION_H diff --git a/control/src/drv/usart.cpp b/control/src/drv/usart.cpp index 568cdd7..28cbdf8 100644 --- a/control/src/drv/usart.cpp +++ b/control/src/drv/usart.cpp @@ -75,15 +75,25 @@ void USART::flushOutputBuffer() void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len) { int bytes_avail, code; + auto start = std::chrono::steady_clock::now(); + auto end = std::chrono::steady_clock::now(); do { code = ioctl(file_desc, FIONREAD, &bytes_avail); - if(code) + if (code) throw USARTException( std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) + ", " + strerror(code) + " (code " + std::to_string(code) + ")"); + + end = std::chrono::steady_clock::now(); + long elapsed = + std::chrono::duration_cast(end - start).count() / 100; // in Dezisekunden + if (elapsed >= timeout) + throw TimeoutException( + std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) + + ", " + std::to_string(elapsed) + " / " + std::to_string(timeout) + " ds"); } - while(bytes_avail < len); + while (bytes_avail < len); code = read(file_desc, buffer + offset, len); if (code != len) diff --git a/control/src/drv/usart.cpp.orig b/control/src/drv/usart.cpp.orig deleted file mode 100644 index 7893c7c..0000000 --- a/control/src/drv/usart.cpp.orig +++ /dev/null @@ -1,121 +0,0 @@ -#include -#include "usart.h" - -USART::~USART() -{ - closeDevice(); -} - -void USART::openDevice(std::string device) -{ - // Benutze blockierenden Modus - file_desc = open(device.c_str(), O_RDWR | O_NOCTTY);// | O_NDELAY - if (file_desc <= 0) - throw USARTException("Fehler beim Öffnen des Gerätes"); - - struct termios options; - int code = tcgetattr(file_desc, &options); - if (code) - throw USARTException("Fehler beim Lesen der Geräteparameter"); - - options.c_cflag = CS8 | CLOCAL | CREAD; - options.c_iflag = IGNPAR; - options.c_oflag = 0; - options.c_lflag = 0; - options.c_cc[VMIN] = 0; - options.c_cc[VTIME] = timeout; - code = cfsetspeed(&options, baudrate); - if (code) - throw USARTException("Fehler beim Setzen der Baudrate"); - - code = tcsetattr(file_desc, TCSANOW, &options); - if (code) - throw USARTException("Fehler beim Setzen der Geräteparameter"); - - code = fcntl(file_desc, F_SETFL, 0); // blockierender Modus - if (code) - throw USARTException("Fehler beim Aktivieren des blockierenden Modus'"); - - clearOutputBuffer(); - clearInputBuffer(); -} - -void USART::closeDevice() -{ - if (file_desc > 0) - { - int code = close(file_desc); - if (code) - throw USARTException("Fehler beim Schließen des Gerätes"); - file_desc = -1; - } -} - -void USART::clearInputBuffer() -{ - int code = tcflush(file_desc, TCIFLUSH); - if (code) - throw USARTException("Fehler beim Leeren des Eingangspuffers"); -} - -void USART::clearOutputBuffer() -{ - int code = tcflush(file_desc, TCOFLUSH); - if (code) - throw USARTException("Fehler beim Leeren des Ausgangspuffers"); -} - -void USART::flushOutputBuffer() -{ - int code = tcdrain(file_desc); - if (code) - throw USARTException("Fehler beim Versenden des Ausgangspuffers"); -} - -void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len) -{ - int bytes_avail, code; - do - { - code = ioctl(file_desc, FIONREAD, &bytes_avail); - if(code) - throw USARTException( - std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) + - ", " + strerror(code) + " (code " + std::to_string(code) + ")"); - } while(bytes_avail < len); - - code = read(file_desc, buffer + offset, len); - if (code != len) - throw USARTException( - std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) + - ", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")"); -} - -void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len) -{ - int code = write(file_desc, buffer + offset, len); - if (code != len) - throw USARTException( - std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) + - ", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")"); -} - -uint32_t USART::getBaudrate() -{ - return baudrate; -} - -uint8_t USART::getTimeout() -{ - return timeout; -} - -void USART::setBaudrate(uint32_t baudrate) -{ - this->baudrate = baudrate; -} - -void USART::setTimeout(uint8_t timeout) -{ - this->timeout = timeout; -} diff --git a/control/src/drv/usart.h b/control/src/drv/usart.h index ac42f7f..752f945 100644 --- a/control/src/drv/usart.h +++ b/control/src/drv/usart.h @@ -2,13 +2,14 @@ #define USART_H #include +#include #include #include #include -#include #include #include #include "usartexception.h" +#include "timeoutexception.h" /*! C++ Wrapper class for termios usart library. */ @@ -125,7 +126,7 @@ private: int file_desc = -1; //!< Linux Dateideskriptor uint32_t baudrate = 9600; //!< Standard-Baudrate, sollte mit setBaudrate() überschrieben werden! - uint8_t timeout = 100; //!< in Dezisekunden + uint8_t timeout = 10; //!< in Dezisekunden }; #endif // USART_H