diff --git a/TimeoutSerial.cpp b/TimeoutSerial.cpp index 8b49dac..7337a81 100755 --- a/TimeoutSerial.cpp +++ b/TimeoutSerial.cpp @@ -134,6 +134,19 @@ void TimeoutSerial::read(char *data, size_t size) } } +int TimeoutSerial::readChar(int delay) +{ + vector result(1,'\0'); + + timeout = posix_time::seconds(delay); + try { + result = read(1); + return static_cast(result[0]); + } catch (boost::system::system_error& e) { + return -1; + } +} + std::vector TimeoutSerial::read(size_t size) { vector result(size,'\0');//Allocate a vector with the desired size diff --git a/TimeoutSerial.h b/TimeoutSerial.h index 0e715ea..52b7264 100755 --- a/TimeoutSerial.h +++ b/TimeoutSerial.h @@ -161,6 +161,7 @@ public: * \throws timeout_exception in case of timeout */ std::string readStringUntil(const std::string& delim="\n"); + int readChar(int delay); void setRTS(bool enabled); void setDTR(bool enabled); diff --git a/XModem.cpp b/XModem.cpp index 79eac40..0d7d6a8 100644 --- a/XModem.cpp +++ b/XModem.cpp @@ -21,6 +21,7 @@ #include #include "XModem.h" +#include "TimeoutSerial.h" #ifdef UTEST #include "CppUTestExt/MockSupport.h" #endif @@ -35,24 +36,17 @@ const int XModem::receiveDelay=7000; const int XModem::rcvRetryLimit = 10; - - -XModem::XModem(int (*recvChar)(int msDelay), - void (*sendData)(const char *data, int len)) +XModem::XModem(TimeoutSerial *serial) { - this->sendData = sendData; - this->recvChar = recvChar; - this->dataHandler = NULL; - + this->serial = serial; + this->dataHandler = NULL; } -XModem::XModem(int (*recvChar)(int msDelay), - void (*sendData)(const char *data, int len), + +XModem::XModem(TimeoutSerial *serial, bool (*dataHandler)(unsigned long number, char *buffer, int len)) { - this->sendData = sendData; - this->recvChar = recvChar; + this->serial = serial; this->dataHandler = dataHandler; - } bool XModem::dataAvail(int delay) diff --git a/XModem.h b/XModem.h index e6344ec..442aa42 100644 --- a/XModem.h +++ b/XModem.h @@ -41,6 +41,7 @@ class XModem { char buffer[133]; //repeated block flag bool repeatedBlock; + TimeoutSerial *serial; int (*recvChar)(int); void (*sendData)(const char *data, int len); @@ -56,6 +57,8 @@ class XModem { bool receiveFrames(transfer_t transfer); bool sendNack(void); void init(void); + void sendSerialData(const char *data, int len); + int recvSerialChar(int); bool transmitFrames(transfer_t); unsigned char generateChkSum(const char *buffer, int len); @@ -67,9 +70,8 @@ class XModem { static const unsigned char EOT; static const unsigned char CAN; - XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len)); - XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len), - bool (*dataHandler)(unsigned long, char*, int)); + XModem(TimeoutSerial *serial); + XModem(TimeoutSerial *serial,bool (*dataHandler)(unsigned long, char*, int)); bool receive(); bool transmit();