echo befehl funktioniert
This commit is contained in:
parent
a68bb3aecc
commit
c108303ba3
1
Makefile
1
Makefile
|
@ -12,7 +12,6 @@ CFLAGS = -std=c++14 -O3
|
|||
LDFLAGS =
|
||||
OBJECTS = main.o drv/b15f.o
|
||||
|
||||
|
||||
COMPILE = $(COMPILER_PATH) $(CFLAGS)
|
||||
|
||||
B15F: $(OBJECTS)
|
||||
|
|
Binary file not shown.
41
drv/b15f.cpp
41
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<uint8_t>(b);
|
||||
}
|
||||
}
|
||||
|
||||
B15F& B15F::getInstance(void)
|
||||
{
|
||||
if(!instance)
|
||||
|
|
25
drv/b15f.h
25
drv/b15f.h
|
@ -2,6 +2,20 @@
|
|||
#define B15F_h
|
||||
|
||||
#include <iostream>
|
||||
#include <bits/stdc++.h>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
|
||||
/*#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>*/
|
||||
|
||||
#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
|
||||
|
|
BIN
drv/b15f.o
BIN
drv/b15f.o
Binary file not shown.
32
drv/driverexception.h
Normal file
32
drv/driverexception.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef DRIVEREXCEPTION_H
|
||||
#define DRIVEREXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
|
||||
// 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
|
2
main.cpp
2
main.cpp
|
@ -5,5 +5,5 @@ int main()
|
|||
{
|
||||
B15F& drv = B15F::getInstance();
|
||||
drv.init();
|
||||
std::cout << "heelol" << std::endl;
|
||||
std::cout << "Schluss." << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue