| 1 |
#include "IndiSerialWrapper.hpp" |
| 2 |
|
| 3 |
using namespace GoToDriver; |
| 4 |
|
| 5 |
#define UNUSED(x) (void)(x) |
| 6 |
|
| 7 |
IndiSerialWrapper::IndiSerialWrapper() : |
| 8 |
mTtyFd(-1) |
| 9 |
{ |
| 10 |
|
| 11 |
} |
| 12 |
|
| 13 |
IndiSerialWrapper::~IndiSerialWrapper() |
| 14 |
{ |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
int IndiSerialWrapper::GetFD() |
| 19 |
{ |
| 20 |
return mTtyFd; |
| 21 |
} |
| 22 |
|
| 23 |
void IndiSerialWrapper::SetFD(int fd) |
| 24 |
{ |
| 25 |
mTtyFd = fd; |
| 26 |
} |
| 27 |
|
| 28 |
//Opens the serial device, the acutal implementation has to deal with the handles! |
| 29 |
bool IndiSerialWrapper::Open() |
| 30 |
{ |
| 31 |
//indi opens the serial interface for us, so assume this worked. |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
//Closes the serial device, the actual implementation has to deal with the handles! |
| 36 |
bool IndiSerialWrapper::Close() |
| 37 |
{ |
| 38 |
//indi closes the serial interface for us so assume this worked. |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
//Returns true if the serial port is open and ready to receive or transmit data. |
| 43 |
bool IndiSerialWrapper::IsOpen() |
| 44 |
{ |
| 45 |
return (mTtyFd > -1); |
| 46 |
} |
| 47 |
|
| 48 |
//Returns the number of bytes to read available in the serial receiver queue. |
| 49 |
size_t IndiSerialWrapper::BytesToRead() |
| 50 |
{ |
| 51 |
if(IsOpen()) |
| 52 |
{ |
| 53 |
size_t chars_available = 0; |
| 54 |
int result = -1; |
| 55 |
|
| 56 |
result = ioctl(mTtyFd, FIONREAD, &chars_available); |
| 57 |
|
| 58 |
//std::cerr << "Bytes Available: FD " << std::dec << mTtyFd << " data: " << chars_available << std::endl; |
| 59 |
|
| 60 |
if(result > -1) |
| 61 |
{ |
| 62 |
return chars_available; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
//Reads a byte from the serial device. Can safely cast to uint8_t unless -1 is returned, corresponding to "stream end reached". |
| 70 |
int16_t IndiSerialWrapper::ReadByte() |
| 71 |
{ |
| 72 |
if(IsOpen()) |
| 73 |
{ |
| 74 |
int bytesRead = 0; |
| 75 |
char dataByte = 0x00; |
| 76 |
int result = tty_read(mTtyFd, &dataByte, 1, 0, &bytesRead); |
| 77 |
|
| 78 |
if(result == TTY_OK) |
| 79 |
{ |
| 80 |
//std::cerr << "Read OK: FD " << std::dec << mTtyFd << " data: " << std::hex << dataByte << std::dec << std::endl; |
| 81 |
return dataByte; |
| 82 |
} |
| 83 |
/*else |
| 84 |
{ |
| 85 |
//TODO: log error; |
| 86 |
//LOGF_ERROR("BresserExosIIDriver::IndiSerialWrapper::ReadByte: error reading from serial device..."); |
| 87 |
std::cerr << "Error while reading!" << std::endl; |
| 88 |
}*/ |
| 89 |
} |
| 90 |
|
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
//writes the buffer to the serial interface. |
| 95 |
//this function should handle all the quirks of various serial interfaces. |
| 96 |
bool IndiSerialWrapper::Write(uint8_t* buffer, size_t offset, size_t length) |
| 97 |
{ |
| 98 |
UNUSED(offset); |
| 99 |
|
| 100 |
{ |
| 101 |
std::lock_guard<std::mutex> guard(mMutex); |
| 102 |
|
| 103 |
if(IsOpen() && buffer != nullptr && length > 0) |
| 104 |
{ |
| 105 |
int nbytes_written; |
| 106 |
int result = tty_write(mTtyFd, (char*)buffer, length, &nbytes_written); |
| 107 |
|
| 108 |
if(result != TTY_OK) |
| 109 |
{ |
| 110 |
std::cerr << "Error while writing!" << std::endl; |
| 111 |
//TODO log error: |
| 112 |
//LOGF_ERROR("BresserExosIIDriver::IndiSerialWrapper::Write: error writing to serial device..."); |
| 113 |
return false; |
| 114 |
} |
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
//flush the buffer. |
| 122 |
bool IndiSerialWrapper::Flush() |
| 123 |
{ |
| 124 |
if(IsOpen()) |
| 125 |
{ |
| 126 |
int result = tcflush(mTtyFd, TCIOFLUSH); |
| 127 |
|
| 128 |
if(result != TTY_OK) |
| 129 |
{ |
| 130 |
std::cerr << "Error while flushing!" << std::endl; |
| 131 |
return false; |
| 132 |
} |
| 133 |
return true; |
| 134 |
} |
| 135 |
return false; |
| 136 |
} |