B15F
Board 15 Famulus Edition
usart.cpp
1 #include <stdexcept>
2 #include "usart.h"
3 
5 {
6  closeDevice();
7 }
8 
9 void USART::openDevice(std::string device)
10 {
11  // Benutze blockierenden Modus
12  file_desc = open(device.c_str(), O_RDWR | O_NOCTTY);// | O_NDELAY
13  if (file_desc <= 0)
14  throw USARTException("Fehler beim Öffnen des Gerätes");
15 
16  struct termios options;
17  int code = tcgetattr(file_desc, &options);
18  if (code)
19  throw USARTException("Fehler beim Lesen der Geräteparameter");
20 
21  options.c_cflag = CS8 | CLOCAL | CREAD;
22  options.c_iflag = IGNPAR;
23  options.c_oflag = 0;
24  options.c_lflag = 0;
25  options.c_cc[VMIN] = 0;
26  options.c_cc[VTIME] = timeout;
27  code = cfsetspeed(&options, baudrate);
28  if (code)
29  throw USARTException("Fehler beim Setzen der Baudrate");
30 
31  code = tcsetattr(file_desc, TCSANOW, &options);
32  if (code)
33  throw USARTException("Fehler beim Setzen der Geräteparameter");
34 
35  code = fcntl(file_desc, F_SETFL, 0); // blockierender Modus
36  if (code)
37  throw USARTException("Fehler beim Aktivieren des blockierenden Modus'");
38 
41 }
42 
44 {
45  if (file_desc > 0)
46  {
47  int code = close(file_desc);
48  if (code)
49  throw USARTException("Fehler beim Schließen des Gerätes");
50  file_desc = -1;
51  }
52 }
53 
55 {
56  int code = tcflush(file_desc, TCIFLUSH);
57  if (code)
58  throw USARTException("Fehler beim Leeren des Eingangspuffers");
59 }
60 
62 {
63  int code = tcflush(file_desc, TCOFLUSH);
64  if (code)
65  throw USARTException("Fehler beim Leeren des Ausgangspuffers");
66 }
67 
69 {
70  int code = tcdrain(file_desc);
71  if (code)
72  throw USARTException("Fehler beim Versenden des Ausgangspuffers");
73 }
74 
75 void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
76 {
77  int bytes_avail, code;
78  do
79  {
80  code = ioctl(file_desc, FIONREAD, &bytes_avail);
81  if(code)
82  throw USARTException(
83  std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
84  ", " + strerror(code) + " (code " + std::to_string(code) + ")");
85  }
86  while(bytes_avail < len);
87 
88  code = read(file_desc, buffer + offset, len);
89  if (code != len)
90  throw USARTException(
91  std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
92  ", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")");
93 }
94 
95 void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
96 {
97  int code = write(file_desc, buffer + offset, len);
98  if (code != len)
99  throw USARTException(
100  std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__) +
101  ", " + strerror(code) + " (code " + std::to_string(code) + " / " + std::to_string(len) + ")");
102 }
103 
105 {
106  return baudrate;
107 }
108 
110 {
111  return timeout;
112 }
113 
114 void USART::setBaudrate(uint32_t baudrate)
115 {
116  this->baudrate = baudrate;
117 }
118 
119 void USART::setTimeout(uint8_t timeout)
120 {
121  this->timeout = timeout;
122 }
USART::getBaudrate
uint32_t getBaudrate(void)
Definition: usart.cpp:104
USARTException
Definition: usartexception.h:9
USART::closeDevice
void closeDevice(void)
Definition: usart.cpp:43
USART::transmit
void transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
Definition: usart.cpp:95
USART::receive
void receive(uint8_t *buffer, uint16_t offset, uint8_t len)
Definition: usart.cpp:75
USART::clearInputBuffer
void clearInputBuffer(void)
Definition: usart.cpp:54
USART::getTimeout
uint8_t getTimeout(void)
Definition: usart.cpp:109
USART::clearOutputBuffer
void clearOutputBuffer(void)
Definition: usart.cpp:61
USART::setBaudrate
void setBaudrate(uint32_t baudrate)
Definition: usart.cpp:114
USART::~USART
virtual ~USART(void)
Definition: usart.cpp:4
USART::openDevice
void openDevice(std::string device)
Definition: usart.cpp:9
USART::setTimeout
void setTimeout(uint8_t timeout)
Definition: usart.cpp:119
USART::flushOutputBuffer
void flushOutputBuffer(void)
Definition: usart.cpp:68