b15f/drv/b15f.cpp

136 lines
2.8 KiB
C++
Raw Normal View History

2019-03-26 08:57:01 +00:00
#include "b15f.h"
B15F* B15F::instance = nullptr;
B15F::B15F()
{
}
void B15F::init(void)
{
2019-03-26 12:28:29 +00:00
std::cout << PRE << "Stelle Verbindung mit Adapter her... ";
2019-03-26 10:35:20 +00:00
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?");
}
2019-03-26 12:28:29 +00:00
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);
std::cout << "OK" << std::endl;
2019-03-26 10:35:20 +00:00
2019-03-26 12:28:29 +00:00
// Verbindungstest muss dreimal erfolgreich sein
std::cout << PRE << "Teste Verbindung... ";
for(uint8_t i = 0; i < 3; i++)
2019-03-26 10:35:20 +00:00
{
2019-03-26 12:28:29 +00:00
if(!testConnection())
{
throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
}
2019-03-26 10:35:20 +00:00
}
2019-03-26 12:28:29 +00:00
std::cout << "OK" << std::endl;
writeByte(0xFF);
writeByte(0x01);
writeByte(0x02);
std::cout << readByte() << std::endl;
std::cout << readByte() << std::endl;
2019-03-26 08:57:01 +00:00
}
2019-03-26 10:35:20 +00:00
bool B15F::testConnection(void)
{
2019-03-26 12:28:29 +00:00
// erzeuge zufälliges Byte
2019-03-26 10:35:20 +00:00
srand (time(NULL));
uint8_t dummy = rand() % 256;
2019-03-26 12:28:29 +00:00
2019-03-26 10:35:20 +00:00
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)
{
2019-03-26 12:28:29 +00:00
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);
2019-03-26 10:35:20 +00:00
}
uint8_t B15F::readByte()
{
char b;
2019-03-26 12:28:29 +00:00
auto start = std::chrono::steady_clock::now();
auto end = start;
uint16_t elapsed = 0;
while(elapsed < timeout)
2019-03-26 10:35:20 +00:00
{
2019-03-26 12:28:29 +00:00
int n = read(usart, &b, 1);
if (n > 0)
2019-03-26 10:35:20 +00:00
return static_cast<uint8_t>(b);
2019-03-26 12:28:29 +00:00
/*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();
2019-03-26 10:35:20 +00:00
}
2019-03-26 12:28:29 +00:00
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));
2019-03-26 10:35:20 +00:00
}
2019-03-26 08:57:01 +00:00
B15F& B15F::getInstance(void)
{
if(!instance)
instance = new B15F();
return *instance;
}