diff --git a/Makefile b/Makefile index 4eaeee4..d84f459 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,6 @@ CFLAGS = -std=c++14 -O3 LDFLAGS = OBJECTS = main.o drv/b15f.o - COMPILE = $(COMPILER_PATH) $(CFLAGS) B15F: $(OBJECTS) diff --git a/drv/.b15f.cpp.swp b/drv/.b15f.cpp.swp deleted file mode 100644 index f73c26c..0000000 Binary files a/drv/.b15f.cpp.swp and /dev/null differ diff --git a/drv/b15f.cpp b/drv/b15f.cpp index cadbe43..a6ace00 100644 --- a/drv/b15f.cpp +++ b/drv/b15f.cpp @@ -8,9 +8,48 @@ B15F::B15F() void B15F::init(void) { - std::cout << "moint: " << std::endl; + int code = system(std::string("stty 38400 -F " + SERIAL_DEVICE).c_str()); + if(code) + { + throw DriverException("Konnte serielle Verbindung nicht initialisieren. Ist der Adapter angeschlossen?"); + } + + usart.open(SERIAL_DEVICE.c_str()); + + if(!testConnection()) + { + throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?"); + } } +bool B15F::testConnection(void) +{ + srand (time(NULL)); + uint8_t dummy = rand() % 256; + writeByte(0); // echo / dummy command for testing + writeByte(dummy); + + uint8_t aw = readByte(); + uint8_t mirror = readByte(); + + return aw == MSG_OK && mirror == dummy; +} + +void B15F::writeByte(uint8_t b) +{ + usart.put(b); +} + +uint8_t B15F::readByte() +{ + char b; + while(1) + { + if (usart.get(b)) + return static_cast(b); + } +} + B15F& B15F::getInstance(void) { if(!instance) diff --git a/drv/b15f.h b/drv/b15f.h index 82df289..bd50299 100644 --- a/drv/b15f.h +++ b/drv/b15f.h @@ -2,6 +2,20 @@ #define B15F_h #include +#include +#include +#include +#include +#include + +/*#include +#include +#include +#include +#include */ + +#include "driverexception.h" + class B15F { @@ -9,10 +23,21 @@ private: B15F(void); // privater Konstruktor public: void init(void); + bool testConnection(void); + void writeByte(uint8_t); + uint8_t readByte(void); + static B15F& getInstance(void); private: + std::fstream usart; + static B15F* instance; + +// CONSTANTS + const std::string SERIAL_DEVICE = "/dev/ttyUSB0"; + constexpr static uint8_t MSG_OK = 0xFF; + constexpr static uint8_t MSG_FAIL = 0xFE; }; #endif // B15F_h diff --git a/drv/b15f.o b/drv/b15f.o index 66aeb6e..b886f7c 100644 Binary files a/drv/b15f.o and b/drv/b15f.o differ diff --git a/drv/driverexception.h b/drv/driverexception.h new file mode 100644 index 0000000..8352fd4 --- /dev/null +++ b/drv/driverexception.h @@ -0,0 +1,32 @@ +#ifndef DRIVEREXCEPTION_H +#define DRIVEREXCEPTION_H + +#include + +// SOURCE: https://stackoverflow.com/a/8152888 + +class DriverException: public std::exception +{ +public: + explicit DriverException(const char* message) : msg_(message) + { + } + + explicit DriverException(const std::string& message) : msg_(message) + { + } + + virtual ~DriverException() throw () + { + } + + virtual const char* what() const throw () + { + return msg_.c_str(); + } + +protected: + std::string msg_; +}; + +#endif diff --git a/main b/main index 2ab378b..e128c81 100755 Binary files a/main and b/main differ diff --git a/main.cpp b/main.cpp index 572f019..1a09eaf 100644 --- a/main.cpp +++ b/main.cpp @@ -5,5 +5,5 @@ int main() { B15F& drv = B15F::getInstance(); drv.init(); - std::cout << "heelol" << std::endl; + std::cout << "Schluss." << std::endl; } diff --git a/main.o b/main.o index edd7a31..1045739 100644 Binary files a/main.o and b/main.o differ