| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <mutex> |
| 24 |
#include <string> |
| 25 |
#include <termios.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "Logger/Logger.h" |
| 29 |
|
| 30 |
namespace Network |
| 31 |
{ |
| 32 |
class Serial |
| 33 |
{ |
| 34 |
public: |
| 35 |
Serial() = delete; |
| 36 |
Serial(const Serial&) = delete; |
| 37 |
Serial& operator=(const Serial&) = delete; |
| 38 |
|
| 39 |
inline Serial(Logger::Logger* logger, |
| 40 |
const std::string& tty, |
| 41 |
const unsigned int dataSpeed, |
| 42 |
const unsigned char dataBits, |
| 43 |
const char parity, |
| 44 |
const unsigned char stopBits, |
| 45 |
const bool hardwareFlowControl = false) |
| 46 |
: logger(logger), |
| 47 |
tty(tty), |
| 48 |
dataSpeed(dataSpeed), |
| 49 |
dataBits(dataBits), |
| 50 |
parity(parity), |
| 51 |
stopBits(stopBits), |
| 52 |
hardwareFlowControl(hardwareFlowControl), |
| 53 |
fileHandle(-1) |
| 54 |
{ |
| 55 |
Init(); |
| 56 |
} |
| 57 |
|
| 58 |
~Serial() |
| 59 |
{ |
| 60 |
Close(); |
| 61 |
} |
| 62 |
|
| 63 |
inline void ReInit() |
| 64 |
{ |
| 65 |
Close(); |
| 66 |
Init(); |
| 67 |
} |
| 68 |
|
| 69 |
inline bool IsConnected() const |
| 70 |
{ |
| 71 |
return fileHandle != -1; |
| 72 |
} |
| 73 |
|
| 74 |
inline void ClearBuffers() |
| 75 |
{ |
| 76 |
tcflush(fileHandle, TCIOFLUSH); |
| 77 |
} |
| 78 |
|
| 79 |
inline ssize_t Send(const std::string& data) |
| 80 |
{ |
| 81 |
return Send(reinterpret_cast<const unsigned char*>(data.c_str()), data.length()); |
| 82 |
} |
| 83 |
|
| 84 |
inline ssize_t Send(const unsigned char data) |
| 85 |
{ |
| 86 |
return Send(&data, 1); |
| 87 |
} |
| 88 |
|
| 89 |
inline ssize_t Send(const unsigned char* data, const size_t size) |
| 90 |
{ |
| 91 |
if (!IsConnected()) |
| 92 |
{ |
| 93 |
return 0; |
| 94 |
} |
| 95 |
std::lock_guard<std::mutex> Guard(fileHandleMutex); |
| 96 |
return write(fileHandle, data, size); |
| 97 |
} |
| 98 |
|
| 99 |
bool Receive(std::string& data, const size_t maxData = 1024, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000); |
| 100 |
ssize_t Receive(unsigned char* data, const size_t maxData, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000); |
| 101 |
bool ReceiveExact(std::string& data, const size_t length, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000); |
| 102 |
ssize_t ReceiveExact(unsigned char* data, const size_t length, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000); |
| 103 |
|
| 104 |
private: |
| 105 |
void Init(); |
| 106 |
void Close(); |
| 107 |
|
| 108 |
Logger::Logger* logger; |
| 109 |
const std::string tty; |
| 110 |
const unsigned int dataSpeed; |
| 111 |
const unsigned char dataBits; |
| 112 |
const char parity; |
| 113 |
const unsigned char stopBits; |
| 114 |
const bool hardwareFlowControl; |
| 115 |
int fileHandle; |
| 116 |
mutable std::mutex fileHandleMutex; |
| 117 |
}; |
| 118 |
} |