b15f/control/src/drv/usart.cpp

133 lines
3.8 KiB
C++
Raw Normal View History

2019-06-06 11:47:13 +02:00
#include <stdexcept>
2019-03-29 11:12:31 +01:00
#include "usart.h"
2019-06-06 11:47:13 +02:00
USART::~USART()
{
2019-06-04 09:43:10 +02:00
closeDevice();
}
2019-06-06 11:47:13 +02:00
void USART::openDevice(std::string device)
{
2019-06-04 09:43:10 +02:00
// Benutze blockierenden Modus
2019-06-04 11:04:22 +02:00
file_desc = open(device.c_str(), O_RDWR | O_NOCTTY);// | O_NDELAY
2019-06-04 09:43:10 +02:00
if (file_desc <= 0)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Öffnen des Gerätes");
struct termios options;
int code = tcgetattr(file_desc, &options);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Lesen der Geräteparameter");
options.c_cflag = CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
2019-06-06 11:47:13 +02:00
options.c_cc[VMIN] = 0;
2019-05-23 15:08:54 +02:00
options.c_cc[VTIME] = timeout;
2019-03-29 11:12:31 +01:00
code = cfsetspeed(&options, baudrate);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Setzen der Baudrate");
code = tcsetattr(file_desc, TCSANOW, &options);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Setzen der Geräteparameter");
2019-06-04 11:04:22 +02:00
code = fcntl(file_desc, F_SETFL, 0); // blockierender Modus
if (code)
throw USARTException("Fehler beim Aktivieren des blockierenden Modus'");
2019-05-23 15:08:54 +02:00
clearOutputBuffer();
clearInputBuffer();
2019-03-29 11:12:31 +01:00
}
2019-05-23 15:08:54 +02:00
2019-06-06 11:47:13 +02:00
void USART::closeDevice()
{
if (file_desc > 0)
{
2019-06-04 09:43:10 +02:00
int code = close(file_desc);
if (code)
throw USARTException("Fehler beim Schließen des Gerätes");
file_desc = -1;
}
2019-03-29 11:12:31 +01:00
}
2019-05-23 15:08:54 +02:00
2019-06-06 11:47:13 +02:00
void USART::clearInputBuffer()
{
2019-05-23 15:08:54 +02:00
int code = tcflush(file_desc, TCIFLUSH);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Leeren des Eingangspuffers");
2019-03-29 11:12:31 +01:00
}
2019-05-23 15:08:54 +02:00
2019-06-06 11:47:13 +02:00
void USART::clearOutputBuffer()
{
2019-05-23 15:08:54 +02:00
int code = tcflush(file_desc, TCOFLUSH);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Leeren des Ausgangspuffers");
2019-03-29 11:12:31 +01:00
}
2019-05-23 15:08:54 +02:00
2019-06-06 11:47:13 +02:00
void USART::flushOutputBuffer()
{
2019-05-23 15:08:54 +02:00
int code = tcdrain(file_desc);
2019-06-04 09:43:10 +02:00
if (code)
2019-05-23 15:08:54 +02:00
throw USARTException("Fehler beim Versenden des Ausgangspuffers");
2019-04-05 15:59:05 +02:00
}
2019-03-29 11:12:31 +01:00
2019-06-06 11:47:13 +02:00
void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
{
int bytes_avail, code;
2019-06-06 12:00:52 +02:00
auto start = std::chrono::steady_clock::now();
auto end = std::chrono::steady_clock::now();
2019-06-06 11:47:13 +02:00
do
{
code = ioctl(file_desc, FIONREAD, &bytes_avail);
2019-06-06 12:00:52 +02:00
if (code)
2019-06-06 11:47:13 +02:00
throw USARTException(
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
", " + strerror(code) + " (code " + std::to_string(code) + ")");
2019-06-06 12:00:52 +02:00
end = std::chrono::steady_clock::now();
long elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 100; // in Dezisekunden
if (elapsed >= timeout)
throw TimeoutException(
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
", " + std::to_string(elapsed) + " / " + std::to_string(timeout) + " ds");
2019-06-06 11:47:13 +02:00
}
2019-06-06 12:00:52 +02:00
while (bytes_avail < len);
2019-06-06 11:47:13 +02:00
code = read(file_desc, buffer + offset, len);
2019-06-04 11:04:22 +02:00
if (code != len)
2019-06-04 09:43:10 +02:00
throw USARTException(
2019-06-06 11:47:13 +02:00
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")");
2019-04-01 08:47:55 +02:00
}
2019-06-06 11:47:13 +02:00
void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
{
2019-06-04 11:04:22 +02:00
int code = write(file_desc, buffer + offset, len);
if (code != len)
2019-06-04 09:43:10 +02:00
throw USARTException(
2019-06-06 11:47:13 +02:00
std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")");
2019-03-29 11:38:11 +01:00
}
2019-06-06 11:47:13 +02:00
uint32_t USART::getBaudrate()
{
2019-05-23 15:08:54 +02:00
return baudrate;
2019-03-29 11:12:31 +01:00
}
2019-06-06 11:47:13 +02:00
uint8_t USART::getTimeout()
{
2019-05-23 15:08:54 +02:00
return timeout;
2019-03-29 11:12:31 +01:00
}
2019-05-23 15:08:54 +02:00
2019-06-06 11:47:13 +02:00
void USART::setBaudrate(uint32_t baudrate)
{
2019-05-23 15:08:54 +02:00
this->baudrate = baudrate;
2019-03-29 11:12:31 +01:00
}
2019-06-06 11:47:13 +02:00
void USART::setTimeout(uint8_t timeout)
{
2019-05-23 15:08:54 +02:00
this->timeout = timeout;
2019-03-29 11:12:31 +01:00
}