usartexception revised

This commit is contained in:
Tristan Krause 2019-06-04 10:00:41 +02:00
parent b4ac345875
commit ab82f2f90e
200 changed files with 14602 additions and 160 deletions

View file

@ -0,0 +1,52 @@
void USART::writeByte(uint8_t b)
{
int sent = write(file_desc, &b, 1);
if (sent != 1)
{
std::cout << "WARNUNG: Fehler beim Senden (" << sent << "): writeByte(), wiederhole..." << std::endl;
usleep(100000);
sent = write(file_desc, &b, 1);
if (sent != 1)
throw USARTException("Fehler beim Senden: writeByte()");
}
}
void USART::writeInt(uint16_t d)
{
int sent = write(file_desc, reinterpret_cast<char *>(&d), 2);
if (sent != 2)
throw USARTException("Fehler beim Senden: writeInt()");
}
void USART::writeU32(uint32_t w)
{
int sent = write(file_desc, reinterpret_cast<char *>(&w), 4);
if (sent != 4)
throw USARTException("Fehler beim Senden: writeU32()");
}
uint8_t USART::readByte(void)
{
char b;
auto start = std::chrono::steady_clock::now();
auto end = start;
uint16_t elapsed = 0;
while (elapsed < timeout * 100)
{
int code = read(file_desc, &b, 1);
if (code > 0)
return static_cast<uint8_t>(b);
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
throw TimeoutException("Verbindung unterbrochen.", timeout);
}
uint16_t USART::readInt(void)
{
return readByte() | readByte() << 8;
}

View file

@ -1,10 +1,12 @@
#include "usart.h"
USART::~USART() {
USART::~USART()
{
closeDevice();
}
void USART::openDevice(std::string device) {
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)
@ -33,8 +35,10 @@ void USART::openDevice(std::string device) {
clearInputBuffer();
}
void USART::closeDevice() {
if (file_desc > 0) {
void USART::closeDevice()
{
if (file_desc > 0)
{
int code = close(file_desc);
if (code)
throw USARTException("Fehler beim Schließen des Gerätes");
@ -42,95 +46,59 @@ void USART::closeDevice() {
}
}
void USART::clearInputBuffer() {
void USART::clearInputBuffer()
{
int code = tcflush(file_desc, TCIFLUSH);
if (code)
throw USARTException("Fehler beim Leeren des Eingangspuffers");
}
void USART::clearOutputBuffer() {
void USART::clearOutputBuffer()
{
int code = tcflush(file_desc, TCOFLUSH);
if (code)
throw USARTException("Fehler beim Leeren des Ausgangspuffers");
}
void USART::flushOutputBuffer() {
void USART::flushOutputBuffer()
{
int code = tcdrain(file_desc);
if (code)
throw USARTException("Fehler beim Versenden des Ausgangspuffers");
}
void USART::writeByte(uint8_t b) {
int sent = write(file_desc, &b, 1);
if (sent != 1) {
std::cout << "WARNUNG: Fehler beim Senden (" << sent << "): writeByte(), wiederhole..." << std::endl;
usleep(100000);
sent = write(file_desc, &b, 1);
if (sent != 1)
throw USARTException("Fehler beim Senden: writeByte()");
}
}
void USART::writeInt(uint16_t d) {
int sent = write(file_desc, reinterpret_cast<char *>(&d), 2);
if (sent != 2)
throw USARTException("Fehler beim Senden: writeInt()");
}
void USART::writeU32(uint32_t w) {
int sent = write(file_desc, reinterpret_cast<char *>(&w), 4);
if (sent != 4)
throw USARTException("Fehler beim Senden: writeU32()");
}
void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len) {
void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
{
int n = read(file_desc, buffer + offset, len);
if (n != len)
throw USARTException(
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
}
void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len) {
void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
{
int n = write(file_desc, buffer + offset, len);
if (n != len)
throw USARTException(
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
}
uint8_t USART::readByte(void) {
char b;
auto start = std::chrono::steady_clock::now();
auto end = start;
uint16_t elapsed = 0;
while (elapsed < timeout * 100) {
int code = read(file_desc, &b, 1);
if (code > 0)
return static_cast<uint8_t>(b);
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
throw TimeoutException("Verbindung unterbrochen.", timeout);
}
uint16_t USART::readInt(void) {
return readByte() | readByte() << 8;
}
uint32_t USART::getBaudrate() {
uint32_t USART::getBaudrate()
{
return baudrate;
}
uint8_t USART::getTimeout() {
uint8_t USART::getTimeout()
{
return timeout;
}
void USART::setBaudrate(uint32_t baudrate) {
void USART::setBaudrate(uint32_t baudrate)
{
this->baudrate = baudrate;
}
void USART::setTimeout(uint8_t timeout) {
void USART::setTimeout(uint8_t timeout)
{
this->timeout = timeout;
}

View file

@ -1,17 +1,12 @@
#ifndef USART_H
#define USART_H
#include <iostream>
#include <cstdint>
#include <chrono>
#include <unistd.h>
#include <cstring>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <cmath>
#include <sys/ioctl.h>
#include "usartexception.h"
#include "timeoutexception.h"
/*! C++ Wrapper class for termios usart library. */
@ -23,7 +18,15 @@ public:
* Methoden für die Verwaltung der Schnittstelle *
*************************************************/
~USART(void);
/**
* Standard-Konstruktor
*/
explicit USART() = default;
/**
* Destructor, ruft automatisch closeDevice() auf
*/
virtual ~USART(void);
/**
* Öffnet die USART Schnittstelle
@ -64,43 +67,11 @@ public:
* Methoden für die Datenübertragung *
*************************************/
/**
* Sendet ein Byte über die USART Schnittstelle
* \param b das zu sendende Byte
* \throws USARTException
*/
void writeByte(uint8_t b);
/**
* Sendet ein Integer über die USART Schnittstelle
* \param b das zu sendende Int
* \throws USARTException
*/
void writeInt(uint16_t d);
/**
* Sendet ein uint32_t über die USART Schnittstelle
* \param b das zu sendende uint32_t
* \throws USARTException
*/
void writeU32(uint32_t d);
/**
* Empfängt ein Byte über die USART Schnittstelle
* \throws USARTException
*/
uint8_t readByte(void);
/**
* Empfängt ein Integer über die USART Schnittstelle
* \throws USARTException
*/
uint16_t readInt(void);
/**
* Sends n bytes from the buffer over USART
* \param buffer target buffer
* \param offset in buffer (mostly 0)
* \param len count of bytes to send
* \throws USARTException
*/
void transmit(uint8_t *buffer, uint16_t offset, uint8_t len);
@ -109,6 +80,7 @@ public:
* Receives n bytes from USART and writes them into the buffer
* \param buffer target buffer
* \param offset in buffer (mostly 0)
* \param len count of bytes to receive
* \throws USARTException
*/
void receive(uint8_t *buffer, uint16_t offset, uint8_t len);
@ -135,33 +107,23 @@ public:
/**
* Setzt die Baudrate
* <b>Änderungen werden erst nach einem open() wirksam</b>
* <b>Änderungen werden erst nach openDevice() wirksam</b>
*/
void setBaudrate(uint32_t baudrate);
/**
* Setzt den Timeout (in Dezisekunden)
* <b>Änderungen werden erst nach einem open() wirksam</b>
* <b>Änderungen werden erst nach openDevice() wirksam</b>
*/
void setTimeout(uint8_t timeout);
/***************************************/
constexpr static uint8_t CRC7_POLY = 0x91;
constexpr static uint8_t MAX_BLOCK_SIZE = 64;
constexpr static uint8_t BLOCK_END = 0x80;
private:
int file_desc = -1; // Linux Dateideskriptor
uint32_t baudrate = 9600; // Standard-Baudrate, sollte mit setBaudrate() überschrieben werden!
int TEST = 0;
uint8_t timeout = 10; // in Dezisekunden
uint8_t block_buffer[MAX_BLOCK_SIZE + 3];
// debug statistics
uint32_t n_blocks_total = 0;
uint32_t n_blocks_failed = 0;
int file_desc = -1; //!< Linux Dateideskriptor
uint32_t baudrate = 9600; //!< Standard-Baudrate, sollte mit setBaudrate() überschrieben werden!
uint8_t timeout = 10; //!< in Dezisekunden
};
#endif // USART_H

View file

@ -4,32 +4,43 @@
#include <exception>
#include <string>
// SOURCE: https://stackoverflow.com/a/8152888
/*! Exception for USART problems, for instance buffer overflow. */
class USARTException: public std::exception
{
public:
/**
* Constructor
* @param message as c-string
*/
explicit USARTException(const char* message) : msg(message)
{
}
/**
* Constructor
* @param message as c++-string
*/
explicit USARTException(const std::string& message) : msg(message)
{
}
virtual ~USARTException() throw ()
{
}
/**
* Standard-destructor
*/
virtual ~USARTException() = default;
/**
* Get failure description
* @return error message as c-string
*/
virtual const char* what() const throw ()
{
return msg.c_str();
}
protected:
std::string msg;
std::string msg; //!< failure description
};
#endif // USARTEXCEPTION_H