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

@ -12,9 +12,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d13a1bae-0fd7-48fc-b53c-d6dd1aaf95b9" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/usart.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usart.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/usart.h" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usart.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/usartexception.h" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usartexception.h" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/cmake-build-debug/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -26,7 +26,6 @@
<component name="ClangdSettings">
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug" />
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
@ -41,26 +40,47 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/usart.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="405">
<caret line="154" column="8" lean-forward="true" selection-start-line="154" selection-start-column="8" selection-end-line="154" selection-end-column="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="34" selection-start-line="7" selection-start-column="34" selection-end-line="7" selection-end-column="34" />
<state relative-caret-position="-1140">
<caret line="28" column="11" selection-start-line="28" selection-start-column="11" selection-end-line="28" selection-end-column="11" />
<folding>
<element signature="e#33#51#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-376">
<caret line="25" column="40" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="40" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/backup.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="780">
<caret line="52" selection-start-line="52" selection-end-line="52" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="13" column="27" selection-start-line="13" selection-start-column="13" selection-end-line="13" selection-end-column="27" />
<state relative-caret-position="58">
<caret line="4" column="17" selection-start-line="4" selection-start-column="17" selection-end-line="4" selection-end-column="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/b15f.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="267">
<caret line="232" column="43" selection-start-line="232" selection-start-column="39" selection-end-line="232" selection-end-column="43" />
</state>
</provider>
</entry>
@ -68,7 +88,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/b15f.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="3315">
<state relative-caret-position="192">
<caret line="221" column="29" selection-start-line="221" selection-start-column="29" selection-end-line="221" selection-end-column="29" />
</state>
</provider>
@ -96,8 +116,10 @@
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/drv/b15f.cpp" />
<option value="$PROJECT_DIR$/drv/usart.h" />
<option value="$PROJECT_DIR$/drv/usart.cpp" />
<option value="$PROJECT_DIR$/drv/backup.cpp" />
<option value="$PROJECT_DIR$/drv/usart.h" />
<option value="$PROJECT_DIR$/drv/usartexception.h" />
</list>
</option>
</component>
@ -151,12 +173,23 @@
</list>
</option>
</component>
<component name="RunManager">
<component name="RunManager" selected="Makefile.Makefile">
<configuration name="src" type="CMakeRunConfiguration" factoryName="Application" PASS_PARENT_ENVS_2="true" PROJECT_NAME="src" TARGET_NAME="src" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="src" RUN_TARGET_NAME="src">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="Makefile" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile" temporary="true">
<makefile filename="$PROJECT_DIR$/Makefile" target="" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Makefile.Makefile" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
@ -169,12 +202,12 @@
<option name="presentableId" value="Default" />
<updated>1559118962147</updated>
<workItem from="1559118964304" duration="1913000" />
<workItem from="1559633658436" duration="466000" />
<workItem from="1559633658436" duration="1568000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="2379000" />
<option name="totallyTimeSpent" value="3481000" />
</component>
<component name="ToolWindowManager">
<frame x="67" y="25" width="1853" height="1055" extended-state="6" />
@ -185,14 +218,14 @@
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32937366" />
<window_info anchor="bottom" id="Run" order="2" />
<window_info anchor="bottom" id="Run" order="2" weight="0.32937366" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Database Changes" order="7" />
<window_info anchor="bottom" id="Messages" order="8" weight="0.32937366" />
<window_info active="true" anchor="bottom" id="Terminal" order="9" visible="true" weight="0.23434125" />
<window_info active="true" anchor="bottom" id="Terminal" order="9" visible="true" weight="0.44168466" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="11" />
<window_info anchor="bottom" id="CMake" order="12" weight="0.33045357" />
@ -207,9 +240,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/drv/b15f.h">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
@ -219,29 +249,46 @@
</entry>
<entry file="file://$PROJECT_DIR$/drv/b15f.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="3315">
<state relative-caret-position="192">
<caret line="221" column="29" selection-start-line="221" selection-start-column="29" selection-end-line="221" selection-end-column="29" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<entry file="file://$PROJECT_DIR$/drv/b15f.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="13" column="27" selection-start-line="13" selection-start-column="13" selection-end-line="13" selection-end-column="27" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="405">
<caret line="154" column="8" lean-forward="true" selection-start-line="154" selection-start-column="8" selection-end-line="154" selection-end-column="17" />
<state relative-caret-position="267">
<caret line="232" column="43" selection-start-line="232" selection-start-column="39" selection-end-line="232" selection-end-column="43" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="34" selection-start-line="7" selection-start-column="34" selection-end-line="7" selection-end-column="34" />
<state relative-caret-position="-376">
<caret line="25" column="40" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="40" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/backup.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="780">
<caret line="52" selection-start-line="52" selection-end-line="52" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1140">
<caret line="28" column="11" selection-start-line="28" selection-start-column="11" selection-end-line="28" selection-end-column="11" />
<folding>
<element signature="e#33#51#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="58">
<caret line="4" column="17" selection-start-line="4" selection-start-column="17" selection-end-line="4" selection-end-column="17" />
</state>
</provider>
</entry>

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