Merge pull request #18 from john30/master; first version supporting get, set, and cyc again.
This commit is contained in:
+90
-14
@@ -24,6 +24,9 @@
|
||||
#include <queue>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "result.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -64,7 +67,7 @@ public:
|
||||
* @param deviceName to determine device type.
|
||||
* @param noDeviceCheck en-/disable device check.
|
||||
*/
|
||||
virtual void openDevice(const string deviceName, const bool noDeviceCheck) = 0;
|
||||
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck) = 0;
|
||||
|
||||
/**
|
||||
* @brief virtual close function for closing opened file descriptor
|
||||
@@ -89,9 +92,10 @@ public:
|
||||
* @brief recvBytes read bytes from opened file descriptor.
|
||||
* @param timeout time for new input data [usec].
|
||||
* @param maxCount max size of receive buffer.
|
||||
* @param buffer optional direct buffer to write to (instead of queuing the data).
|
||||
* @return number of read bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t recvBytes(const long timeout, size_t maxCount);
|
||||
ssize_t recvBytes(const long timeout, size_t maxCount, unsigned char* buffer=NULL);
|
||||
|
||||
/**
|
||||
* @brief fetch first byte from receive buffer.
|
||||
@@ -147,7 +151,7 @@ public:
|
||||
* @param deviceName to determine device type.
|
||||
* @param noDeviceCheck en-/disable device check.
|
||||
*/
|
||||
void openDevice(const string deviceName, const bool noDeviceCheck);
|
||||
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck);
|
||||
|
||||
/**
|
||||
* @brief close function for closing opened file descriptor
|
||||
@@ -177,7 +181,7 @@ public:
|
||||
* @param deviceName to determine device type.
|
||||
* @param noDeviceCheck en-/disable device check.
|
||||
*/
|
||||
void openDevice(const string deviceName, const bool noDeviceCheck);
|
||||
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck);
|
||||
|
||||
/**
|
||||
* @brief close opened file descriptor
|
||||
@@ -199,18 +203,25 @@ public:
|
||||
* @brief constructs a new instance and determine device type.
|
||||
* @param deviceName to determine device type.
|
||||
* @param noDeviceCheck en-/disable device check.
|
||||
* @param logRaw whether logging of raw data is enabled.
|
||||
* @param logRawFunc a function to call for logging raw data, or NULL.
|
||||
* @param dumpRaw whether dumping of raw data to a file is enabled.
|
||||
* @param dumpRawFile the name of the file to dump raw data to.
|
||||
* @param dumpRawMaxSize the maximum size of @a m_dumpFile.
|
||||
*/
|
||||
Port(const string deviceName, const bool noDeviceCheck);
|
||||
Port(const string deviceName, const bool noDeviceCheck,
|
||||
const bool logRaw, void (*logRawFunc)(const unsigned char byte, bool received),
|
||||
const bool dumpRaw, const char* dumpRawFile, const long dumpRawMaxSize);
|
||||
|
||||
/**
|
||||
* @brief destructor.
|
||||
*/
|
||||
~Port() { delete m_device; }
|
||||
~Port() { delete m_device; m_dumpRawStream.close(); }
|
||||
|
||||
/**
|
||||
* @brief open device
|
||||
*/
|
||||
void open() { m_device->openDevice(m_deviceName, m_noDeviceCheck); }
|
||||
result_t open() { return m_device->openDevice(m_deviceName, m_noDeviceCheck); }
|
||||
|
||||
/**
|
||||
* @brief close device
|
||||
@@ -230,22 +241,33 @@ public:
|
||||
* @return number of written bytes or -1 if an error has occured.
|
||||
*/
|
||||
ssize_t send(const unsigned char* buffer, size_t nbytes = MAX_WRITE_SIZE)
|
||||
{ return m_device->sendBytes(buffer, nbytes); }
|
||||
{
|
||||
ssize_t ret = m_device->sendBytes(buffer, nbytes);
|
||||
if (ret>0 && m_logRaw == true && m_logRawFunc != NULL)
|
||||
(*m_logRawFunc)(buffer[0], false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief recv read bytes from opened file descriptor.
|
||||
* @param timeout max time out for new input data.
|
||||
* @param timeout max time out for new input data [usec], or 0 for infinite.
|
||||
* @param maxCount max size of receive buffer.
|
||||
* @return number of read bytes or -1 if an error has occured.
|
||||
* @param buffer optional direct buffer to write to (instead of queuing the data).
|
||||
* @return number of read bytes (never 0) or a negative result_t code.
|
||||
*/
|
||||
ssize_t recv(const long timeout, size_t maxCount = MAX_READ_SIZE)
|
||||
{ return m_device->recvBytes(timeout, maxCount); }
|
||||
ssize_t recv(const long timeout, size_t maxCount = MAX_READ_SIZE, unsigned char* buffer=NULL)
|
||||
{
|
||||
ssize_t ret = m_device->recvBytes(timeout, maxCount, buffer);
|
||||
if (buffer && ret>0 && m_logRaw == true && m_logRawFunc != NULL)
|
||||
(*m_logRawFunc)(buffer[0], true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief fetch first byte from receive buffer.
|
||||
* @return first byte (raw)
|
||||
*/
|
||||
unsigned char byte() { return m_device->getByte(); }
|
||||
unsigned char byte();
|
||||
|
||||
/**
|
||||
* @brief get current size (bytes) of the receive buffer.
|
||||
@@ -253,9 +275,45 @@ public:
|
||||
*/
|
||||
ssize_t size() const { return m_device->sizeRecvBuffer(); }
|
||||
|
||||
/**
|
||||
* @brief Get whether logging of raw data is enabled.
|
||||
* @return whether logging of raw data is enabled.
|
||||
*/
|
||||
bool getLogRaw() { return m_logRaw; }
|
||||
|
||||
/**
|
||||
* @brief Enable or disable logging of raw data.
|
||||
* @param logRawData true to enable logging of raw data, false to disable it.
|
||||
*/
|
||||
void setLogRaw(bool logRaw=true) { m_logRaw = logRaw; }
|
||||
|
||||
/**
|
||||
* @brief Get whether dumping of raw data to a file is enabled.
|
||||
* @return whether dumping of raw data to a file is enabled.
|
||||
*/
|
||||
bool getDumpRaw() { return m_dumpRaw; }
|
||||
|
||||
/**
|
||||
* @brief Enable or disable dumping of raw data to a file.
|
||||
* @param dumpRaw true to enable dumping of raw data to a file, false to disable it.
|
||||
*/
|
||||
void setDumpRaw(bool dumpRaw=true);
|
||||
|
||||
/**
|
||||
* @brief Set the name of the file to dump raw data to.
|
||||
* @param dumpFile the name of the file to dump raw data to.
|
||||
*/
|
||||
void setDumpRawFile(const string& dumpFile);
|
||||
|
||||
/**
|
||||
* @brief Set the maximum size of a file to dump raw data to.
|
||||
* @param maxSize the maximum size of a file to dump raw data to.
|
||||
*/
|
||||
void setDumpRawMaxSize(const long maxSize) { m_dumpRawMaxSize = maxSize; }
|
||||
|
||||
private:
|
||||
/** the device name */
|
||||
string m_deviceName;
|
||||
const string m_deviceName;
|
||||
|
||||
/** the device instance */
|
||||
Device* m_device;
|
||||
@@ -263,6 +321,24 @@ private:
|
||||
/** true if device check is disabled */
|
||||
bool m_noDeviceCheck;
|
||||
|
||||
/** whether logging of raw data is enabled. */
|
||||
bool m_logRaw;
|
||||
|
||||
/** a function to call for logging raw data, or NULL. */
|
||||
void (*m_logRawFunc)(const unsigned char byte, bool received);
|
||||
|
||||
/** whether dumping of raw data to a file is enabled. */
|
||||
bool m_dumpRaw;
|
||||
|
||||
/** the name of the file to dump raw data to. */
|
||||
string m_dumpRawFile;
|
||||
|
||||
/** the maximum size of @a m_dumpFile. */
|
||||
long m_dumpRawMaxSize;
|
||||
|
||||
/** the @a ofstream for dumping raw data to. */
|
||||
ofstream m_dumpRawStream;
|
||||
|
||||
/**
|
||||
* @brief internal setter for device type.
|
||||
* @param type of device
|
||||
|
||||
Reference in New Issue
Block a user