Verbindung zuverlässiger

This commit is contained in:
Tristan Krause 2019-03-26 13:28:29 +01:00
parent c108303ba3
commit 93f22136c7
4 changed files with 98 additions and 15 deletions

View file

@ -8,24 +8,52 @@ B15F::B15F()
void B15F::init(void)
{
std::cout << PRE << "Stelle Verbindung mit Adapter her... ";
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());
usart = open(SERIAL_DEVICE.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
struct termios options;
tcgetattr(usart, &options);
options.c_cflag = baudrate | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cc[VTIME]=30;
tcsetattr(usart, TCSANOW, &options);
tcflush(usart, TCIFLUSH);
if(!testConnection())
std::cout << "OK" << std::endl;
// Verbindungstest muss dreimal erfolgreich sein
std::cout << PRE << "Teste Verbindung... ";
for(uint8_t i = 0; i < 3; i++)
{
throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
if(!testConnection())
{
throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
}
}
std::cout << "OK" << std::endl;
writeByte(0xFF);
writeByte(0x01);
writeByte(0x02);
std::cout << readByte() << std::endl;
std::cout << readByte() << std::endl;
}
bool B15F::testConnection(void)
{
// erzeuge zufälliges Byte
srand (time(NULL));
uint8_t dummy = rand() % 256;
writeByte(0); // echo / dummy command for testing
writeByte(dummy);
@ -37,17 +65,65 @@ bool B15F::testConnection(void)
void B15F::writeByte(uint8_t b)
{
usart.put(b);
if(write(usart, &b, 1) != 1)
throw DriverException("Fehler beim Senden.");
}
void B15F::writeInt(uint16_t v)
{
// static_cast<char*>(static_cast<void*>(&x));
writeByte((v >> 8) & 0xFF);
writeByte((v >> 0) & 0xFF);
}
void B15F::writeLong(uint32_t v)
{
writeByte((v >> 24) & 0xFF);
writeByte((v >> 16) & 0xFF);
writeByte((v >> 8) & 0xFF);
writeByte((v >> 0) & 0xFF);
}
uint8_t B15F::readByte()
{
char b;
while(1)
auto start = std::chrono::steady_clock::now();
auto end = start;
uint16_t elapsed = 0;
while(elapsed < timeout)
{
if (usart.get(b))
int n = read(usart, &b, 1);
if (n > 0)
return static_cast<uint8_t>(b);
/*else if(n < -1)
{
std::string msg = "Fehler bei der seriellen Verbindung. (Code: ";
msg += std::to_string(n);
msg += ")";
throw DriverException(msg);
}*/
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
if(elapsed >= timeout)
throw DriverException("Verbindung unterbrochen. (timeout)");
}
uint16_t B15F::readInt()
{
return readByte() << 8 | readByte();
}
uint32_t B15F::readLong()
{
return readByte() << 24 | readByte() << 16 | readByte() << 8 | readByte();
}
void B15F::sleep(uint16_t ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
B15F& B15F::getInstance(void)

View file

@ -6,17 +6,14 @@
#include <string>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <cstdint>
/*#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>*/
#include <fcntl.h>
#include <termios.h>
#include "driverexception.h"
class B15F
{
private:
@ -24,17 +21,27 @@ private:
public:
void init(void);
bool testConnection(void);
void writeByte(uint8_t);
uint8_t readByte(void);
inline void writeByte(uint8_t);
inline void writeInt(uint16_t);
inline void writeLong(uint32_t);
inline uint8_t readByte(void);
inline uint16_t readInt(void);
inline uint32_t readLong(void);
void sleep(uint16_t);
static B15F& getInstance(void);
private:
std::fstream usart;
int usart = -1;
uint32_t baudrate = 38400;
uint16_t timeout = 1000;
static B15F* instance;
// CONSTANTS
const std::string PRE = "[B15F] ";
const std::string SERIAL_DEVICE = "/dev/ttyUSB0";
constexpr static uint8_t MSG_OK = 0xFF;
constexpr static uint8_t MSG_FAIL = 0xFE;

Binary file not shown.

BIN
main

Binary file not shown.