auto reconnect

This commit is contained in:
Tristan Krause 2019-03-27 09:18:21 +01:00
parent 8cbd8a1a12
commit a3e9e1ce27
4 changed files with 113 additions and 29 deletions

View file

@ -48,16 +48,41 @@ void B15F::init()
while(1) while(1)
{ {
for(uint16_t i = 0; i < 1024; ) digitaleAusgabe0(0xFF);
digitaleAusgabe0(0x00);
//analogeEingabe(0);
/*for(uint16_t i = 0; i < 1024; )
{ {
i = analogeEingabe(0); i = analogeEingabe(0);
analogeAusgabe0(i); analogeAusgabe0(i);
delay(0); delay(0);
} }*/
} }
} }
void B15F::reconnect()
{
std::cout << PRE << "Verbindung unterbrochen, stelle Verbindung neu her: " << std::flush;
uint8_t tries = RECONNECT_TRIES;
while(tries--)
{
delay(RECONNECT_TIMEOUT);
discard();
if(testConnection())
{
std::cout << "OK" << std::endl << std::flush;
return;
}
}
throw DriverException("Verbindung kann nicht repariert werden");
}
void B15F::discard(void) void B15F::discard(void)
{ {
for(uint8_t i = 0; i < 8; i++) for(uint8_t i = 0; i < 8; i++)
@ -99,57 +124,113 @@ bool B15F::testIntConv()
bool B15F::digitaleAusgabe0(uint8_t port) bool B15F::digitaleAusgabe0(uint8_t port)
{ {
writeByte(RQ_BA0); try
writeByte(port); {
writeByte(RQ_BA0);
uint8_t aw = readByte(); writeByte(port);
return aw == MSG_OK;
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return digitaleAusgabe0(port);
}
} }
bool B15F::digitaleAusgabe1(uint8_t port) bool B15F::digitaleAusgabe1(uint8_t port)
{ {
writeByte(RQ_BA1); try
writeByte(port); {
writeByte(RQ_BA1);
uint8_t aw = readByte(); writeByte(port);
return aw == MSG_OK;
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return digitaleAusgabe1(port);
}
} }
uint8_t B15F::digitaleEingabe0() uint8_t B15F::digitaleEingabe0()
{ {
writeByte(RQ_BE0); try
return readByte(); {
writeByte(RQ_BE0);
return readByte();
}
catch(DriverException& de)
{
reconnect();
return digitaleEingabe0();
}
} }
uint8_t B15F::digitaleEingabe1() uint8_t B15F::digitaleEingabe1()
{ {
writeByte(RQ_BE1); try
return readByte(); {
writeByte(RQ_BE1);
return readByte();
}
catch(DriverException& de)
{
reconnect();
return digitaleEingabe1();
}
} }
bool B15F::analogeAusgabe0(uint16_t value) bool B15F::analogeAusgabe0(uint16_t value)
{ {
writeByte(RQ_AA0); try
writeInt(value); {
writeByte(RQ_AA0);
uint8_t aw = readByte(); writeInt(value);
return aw == MSG_OK;
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return analogeAusgabe0(value);
}
} }
bool B15F::analogeAusgabe1(uint16_t value) bool B15F::analogeAusgabe1(uint16_t value)
{ {
writeByte(RQ_AA1); try
writeInt(value); {
writeByte(RQ_AA1);
uint8_t aw = readByte(); writeInt(value);
return aw == MSG_OK;
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return analogeAusgabe1(value);
}
} }
uint16_t B15F::analogeEingabe(uint8_t channel) uint16_t B15F::analogeEingabe(uint8_t channel)
{ {
writeByte(RQ_ADC); try
writeByte(channel); {
return readInt(); writeByte(RQ_ADC);
writeByte(channel);
return readInt();
}
catch(DriverException& de)
{
reconnect();
return analogeEingabe(channel);
}
} }

View file

@ -21,6 +21,7 @@ private:
public: public:
// Grundfunktionen // Grundfunktionen
void init(void); void init(void);
void reconnect(void);
void discard(void); void discard(void);
bool testConnection(void); bool testConnection(void);
bool testIntConv(void); bool testIntConv(void);
@ -57,6 +58,8 @@ private:
const std::string SERIAL_DEVICE = "/dev/ttyUSB0"; const std::string SERIAL_DEVICE = "/dev/ttyUSB0";
constexpr static uint8_t MSG_OK = 0xFF; constexpr static uint8_t MSG_OK = 0xFF;
constexpr static uint8_t MSG_FAIL = 0xFE; constexpr static uint8_t MSG_FAIL = 0xFE;
constexpr static uint16_t RECONNECT_TIMEOUT = 32; // ms
constexpr static uint8_t RECONNECT_TRIES = 3;
// REQUESTS // REQUESTS
constexpr static uint8_t RQ_DISC = 0; constexpr static uint8_t RQ_DISC = 0;

Binary file not shown.

BIN
main

Binary file not shown.