BlackUART.h
Go to the documentation of this file.
1  /*
2 
3  ####################################################################################
4  # BlackLib Library controls Beaglebone Black's inputs and outputs. #
5  # Copyright (C) 2013-2015 by Yigit YUCE #
6  ####################################################################################
7  # #
8  # This file is part of BlackLib library. #
9  # #
10  # BlackLib library is free software: you can redistribute it and/or modify #
11  # it under the terms of the GNU Lesser General Public License as published by #
12  # the Free Software Foundation, either version 3 of the License, or #
13  # (at your option) any later version. #
14  # #
15  # BlackLib library is distributed in the hope that it will be useful, #
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of #
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
18  # GNU Lesser General Public License for more details. #
19  # #
20  # You should have received a copy of the GNU Lesser General Public License #
21  # along with this program. If not, see <http://www.gnu.org/licenses/>. #
22  # #
23  # For any comment or suggestion please contact the creator of BlackLib Library #
24  # at ygtyce@gmail.com #
25  # #
26  ####################################################################################
27 
28  */
29 
30 
31 
32 #ifndef BLACKUART_H_
33 #define BLACKUART_H_
34 
35 #include "../BlackCore.h"
36 
37 #include <string>
38 #include <fstream>
39 #include <cstdint>
40 #include <unistd.h>
41 
42 #include <fcntl.h>
43 #include <termios.h>
44 
45 
46 
47 
48 namespace BlackLib
49 {
50 
54  enum uartName { UART1 = 1,
55  UART2 = 2,
56  UART4 = 4,
57  UART5 = 5
58  };
59 
60 
64  enum baudRate { Baud0 = 0,
65  Baud50 = 1,
66  Baud75 = 2,
67  Baud110 = 3,
68  Baud134 = 4,
69  Baud150 = 5,
70  Baud200 = 6,
71  Baud300 = 7,
72  Baud600 = 8,
73  Baud1200 = 9,
74  Baud1800 = 10,
75  Baud2400 = 11,
76  Baud4800 = 12,
77  Baud9600 = 13,
78  Baud19200 = 14,
79  Baud38400 = 15
80  };
81 
82 
86  enum parity { ParityNo = 0,
87  ParityOdd = 1,
90  };
91 
92 
96  enum characterSize { Char5 = 5,
97  Char6 = 6,
98  Char7 = 7,
99  Char8 = 8,
101  };
102 
103 
107  enum stopBits { StopOne = 1,
108  StopTwo = 2,
110  };
111 
112 
119  };
120 
121 
122 
123 
124  // ######################################### BLACKUARTPROPERTIES DECLARATION STARTS ######################################### //
125 
136  {
148  {
149  uartBaudIn = Baud9600;
150  uartBaudOut = Baud9600;
151  uartParity = ParityDefault;
152  uartStopBits = StopDefault;
153  uartCharSize = CharDefault;
154  }
155 
164  BlackUartProperties(baudRate S_baudIn, baudRate S_baudOut, parity S_parity, stopBits S_stopBits, characterSize S_charSize)
165  {
166  uartBaudIn = S_baudIn;
167  uartBaudOut = S_baudOut;
168  uartParity = S_parity;
169  uartStopBits = S_stopBits;
170  uartCharSize = S_charSize;
171  }
172 
179  {
180  uartBaudIn = S_properties->uartBaudIn;
181  uartBaudOut = S_properties->uartBaudOut;
182  uartParity = S_properties->uartParity;
183  uartStopBits = S_properties->uartStopBits;
184  uartCharSize = S_properties->uartCharSize;
185  }
186 
187 
193  {
194  uartBaudIn = equal.uartBaudIn;
195  uartBaudOut = equal.uartBaudOut;
196  uartCharSize = equal.uartCharSize;
197  uartParity = equal.uartParity;
198  uartStopBits = equal.uartStopBits;
199 
200  return *this;
201  }
202  };
203  // ########################################## BLACKUARTPROPERTIES DECLARATION ENDS ########################################### //
204 
205 
206 
207 
208 
209 
210 
211  // ########################################### BLACKUART DECLARATION STARTS ############################################ //
212 
326  class BlackUART : virtual private BlackCore
327  {
328 
329  private:
336  std::string dtUartFilename;
337  std::string uartPortPath;
339  uint32_t readBufferSize;
340  int uartFD;
341  bool isOpenFlag;
351  bool loadDeviceTree();
352 
353 
354  public:
358  enum flags { dtErr = 1,
359  openErr = 2,
360  closeErr = 3,
362  flushErr = 5,
363  readErr = 6,
364  writeErr = 7,
366  parityErr = 9,
367  stopBitsErr = 10,
369  };
370 
408  BlackUART(uartName uart, baudRate uartBaud, parity uartParity, stopBits uartStopBits, characterSize uartCharSize);
409 
439  BlackUART(uartName uart, BlackUartProperties uartProperties);
440 
461  BlackUART(uartName uart);
462 
467  virtual ~BlackUART();
468 
495  bool open(uint openMode);
496 
517  bool close();
518 
540  bool flush(direction whichDirection);
541 
579  std::string read();
580 
619  bool read(char *readBuffer, size_t size);
620 
655  bool write(std::string writeBuffer);
656 
692  bool write(char *writeBuffer, size_t size);
693 
733  bool transfer(char *writeBuffer, char *readBuffer, size_t size, uint32_t wait_us);
734 
773  std::string transfer(std::string writeBuffer, uint32_t wait_us);
774 
803  void setReadBufferSize(uint32_t newBufferSize);
804 
847  bool setBaudRate(baudRate newBaud, direction whichDirection, uartApplyMode applyMode = ApplyNow );
848 
890  bool setParity(parity newParity, uartApplyMode applyMode = ApplyNow );
891 
933  bool setStopBits(stopBits newStopBits, uartApplyMode applyMode = ApplyNow );
934 
976  bool setCharacterSize(characterSize newCharacterSize, uartApplyMode applyMode = ApplyNow );
977 
1036  bool setProperties(BlackUartProperties &props, uartApplyMode applyMode = ApplyNow );
1037 
1051 
1077  std::string getPortName();
1078 
1088  uint32_t getReadBufferSize();
1089 
1103  baudRate getBaudRate(direction whichDirection);
1104 
1114  parity getParity();
1115 
1126 
1137 
1166  bool isOpen();
1167 
1196  bool isClose();
1197 
1229  bool fail();
1230 
1303  bool fail(BlackUART::flags f);
1304 
1305 
1339  BlackUART& operator<<(std::string &writeFromThis);
1340 
1377  BlackUART& operator>>(std::string &readToThis);
1378  };
1379 
1380  // ########################################### BLACKUART DECLARATION ENDS ############################################## //
1381 
1382 
1383 } /* namespace BlackLib */
1384 
1385 #endif /* BLACKUART_H_ */
bool setParity(parity newParity, uartApplyMode applyMode=ApplyNow)
Changes parity of uart.
Definition: BlackUART.cpp:524
bool open(uint openMode)
Opens TTY file of uart.
Definition: BlackUART.cpp:118
Definition: BlackUART.h:108
Definition: BlackUART.h:367
Definition: BlackUART.h:74
std::string getPortName()
Exports uart's port path.
Definition: BlackUART.cpp:370
stopBits
Definition: BlackUART.h:107
Interacts with end user, to use UART.
Definition: BlackUART.h:326
Definition: BlackUART.h:75
Definition: BlackUART.h:69
BlackUART(uartName uart, baudRate uartBaud, parity uartParity, stopBits uartStopBits, characterSize uartCharSize)
Constructor of BlackUART class.
Definition: BlackUART.cpp:38
Definition: BlackUART.h:107
BlackUartProperties currentUartProperties
is used to hold the current properties of uart
Definition: BlackUART.h:331
Holds properties of UART.
Definition: BlackUART.h:135
Definition: BlackUART.h:362
Definition: BlackUART.h:366
bool setProperties(BlackUartProperties &props, uartApplyMode applyMode=ApplyNow)
Changes properties of uart.
Definition: BlackUART.cpp:782
baudRate
Definition: BlackUART.h:64
bool isCurrentEqDefault
is used to hold the properties of uart is equal to default properties
Definition: BlackUART.h:342
Holds BlackUART errors.
Definition: BlackErr.h:679
Definition: BlackUART.h:368
Definition: BlackUART.h:79
baudRate uartBaudOut
is used to hold the baud rate of UART TX
Definition: BlackUART.h:138
errorUART * uartErrors
is used to hold the errors of BlackUART class
Definition: BlackUART.h:334
bool flush(direction whichDirection)
Flushes uart line.
Definition: BlackUART.cpp:181
bool isClose()
Checks uart's tty file's close state.
Definition: BlackUART.cpp:914
Definition: BlackUART.h:55
Definition: BlackUART.h:88
Definition: BlackUART.h:77
Definition: BlackUART.h:100
bool setBaudRate(baudRate newBaud, direction whichDirection, uartApplyMode applyMode=ApplyNow)
Changes baud rate of uart.
Definition: BlackUART.cpp:416
bool close()
Closes TTY file of uart.
Definition: BlackUART.cpp:164
bool write(std::string writeBuffer)
Writes values to uart line.
Definition: BlackUART.cpp:274
Definition: BlackUART.h:117
Definition: BlackUART.h:54
Definition: BlackUART.h:361
bool setCharacterSize(characterSize newCharacterSize, uartApplyMode applyMode=ApplyNow)
Changes character size of uart.
Definition: BlackUART.cpp:670
Definition: BlackUART.h:57
uartApplyMode
Definition: BlackUART.h:116
Definition: BlackUART.h:363
characterSize
Definition: BlackUART.h:96
Definition: BlackUART.h:360
Definition: BlackUART.h:99
int uartFD
is used to hold the uart's tty file's file descriptor
Definition: BlackUART.h:340
Definition: BlackUART.h:118
openMode
Definition: BlackDef.h:72
Definition: BlackUART.h:76
bool fail()
Is used for general debugging.
Definition: BlackUART.cpp:922
Definition: BlackUART.h:359
Definition: BlackUART.h:70
bool loadDeviceTree()
Loads UART overlay to device tree.
Definition: BlackUART.cpp:96
Definition: BlackUART.h:365
uint32_t readBufferSize
is used to hold the size of temporary buffer
Definition: BlackUART.h:339
BlackUartProperties getProperties()
Exports properties of uart.
Definition: BlackUART.cpp:737
flags
Definition: BlackUART.h:358
Definition: BlackUART.h:65
Definition: BlackUART.h:78
std::string uartPortPath
is used to hold the uart's tty port path
Definition: BlackUART.h:337
Definition: BlackUART.h:96
characterSize uartCharSize
is used to hold the character size of UART
Definition: BlackUART.h:141
BlackUartProperties(baudRate S_baudIn, baudRate S_baudOut, parity S_parity, stopBits S_stopBits, characterSize S_charSize)
Overloaded constructor of BlackUartProperties struct.
Definition: BlackUART.h:164
parity
Definition: BlackUART.h:86
BlackUartProperties & operator=(BlackUartProperties equal)
Overloaded assign operator of BlackUartProperties struct.
Definition: BlackUART.h:192
Definition: BlackUART.h:67
BlackUART & operator<<(std::string &writeFromThis)
Writes values to uart line with "<<" operator.
Definition: BlackUART.cpp:958
Definition: BlackUART.h:66
baudRate uartBaudIn
is used to hold the baud rate of UART RX
Definition: BlackUART.h:137
Definition: BlackUART.h:71
Definition: BlackUART.h:64
direction
Definition: BlackDef.h:44
Definition: BlackUART.h:109
virtual ~BlackUART()
Destructor of BlackUART class.
Definition: BlackUART.cpp:88
Definition: BlackUART.h:87
Definition: BlackUART.h:97
std::string dtUartFilename
is used to hold the uart's device tree overlay name
Definition: BlackUART.h:336
BlackUartProperties()
Default constructor of BlackUartProperties struct.
Definition: BlackUART.h:147
BlackUartProperties constructorProperties
is used to hold the user specified properties of uart
Definition: BlackUART.h:332
Definition: BlackUART.h:73
BlackUartProperties(BlackUartProperties *S_properties)
Overloaded constructor of BlackUartProperties struct.
Definition: BlackUART.h:178
bool setStopBits(stopBits newStopBits, uartApplyMode applyMode=ApplyNow)
Changes stop bits size of uart.
Definition: BlackUART.cpp:605
Definition: BlackUART.h:68
parity uartParity
is used to hold the parity type of UART
Definition: BlackUART.h:139
Definition: BlackUART.h:116
characterSize getCharacterSize()
Exports character size value of uart.
Definition: BlackUART.cpp:642
std::string read()
Reads values from uart line.
Definition: BlackUART.cpp:239
BlackUartProperties defaultUartProperties
is used to hold the default properties of uart
Definition: BlackUART.h:330
BlackUART & operator>>(std::string &readToThis)
Reads values from uart line with ">>" operator.
Definition: BlackUART.cpp:972
stopBits uartStopBits
is used to hold the stop bits size of UART
Definition: BlackUART.h:140
Definition: BlackUART.h:89
baudRate getBaudRate(direction whichDirection)
Exports baud rate value of uart.
Definition: BlackUART.cpp:377
uartName
Definition: BlackUART.h:54
stopBits getStopBits()
Exports stop bits size value of uart.
Definition: BlackUART.cpp:571
Definition: BlackADC.cpp:36
bool isOpenFlag
is used to hold the uart's tty file's state
Definition: BlackUART.h:341
Definition: BlackUART.h:56
bool transfer(char *writeBuffer, char *readBuffer, size_t size, uint32_t wait_us)
Writes and reads values sequentially to/from uart line.
Definition: BlackUART.cpp:291
Definition: BlackUART.h:358
parity getParity()
Exports parity value of uart.
Definition: BlackUART.cpp:482
Definition: BlackUART.h:364
Definition: BlackUART.h:98
Definition: BlackUART.h:86
bool isOpen()
Checks uart's tty file's open state.
Definition: BlackUART.cpp:909
void setReadBufferSize(uint32_t newBufferSize)
Changes internal temporary buffers' sizes.
Definition: BlackUART.cpp:363
uint32_t getReadBufferSize()
Exports internal temporary buffers' size.
Definition: BlackUART.cpp:358
Definition: BlackUART.h:72
Base class of the other classes.
Definition: BlackCore.h:80