Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error when com greater than nine #131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/rhsplib/RHSPlib/arch/includes/RHSPlib_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ int RHSPlib_serial_write(RHSPlib_Serial_T *serial, const uint8_t *buffer, size_t
* */
void RHSPlib_serial_close(RHSPlib_Serial_T *serial);

/**
* @brief Get the last error code from the OS.
* This is errno on unix, and GetLastError on windows.
* */
int RHSPlib_getLastOsError(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 4 additions & 1 deletion packages/rhsplib/RHSPlib/arch/linux/RHSPlib_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

//#include "RHSPlib_compiler.h"
#include "RHSPlib_serial.h"
Expand Down Expand Up @@ -330,4 +331,6 @@ static int baudrateToBits(uint32_t baudrate) {
}
}


int RHSPlib_getLastOsError(void) {
return (int) GetLastError();
}
4 changes: 4 additions & 0 deletions packages/rhsplib/RHSPlib/arch/mac/RHSPlib_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

//#include "RHSPlib_compiler.h"
#include "RHSPlib_serial.h"
Expand Down Expand Up @@ -232,3 +233,6 @@ void RHSPlib_serial_close(RHSPlib_Serial_T *serial)
serial->fd = -1;
}

int RHSPlib_getLastOsError(void) {
return errno;
}
9 changes: 8 additions & 1 deletion packages/rhsplib/RHSPlib/arch/win/RHSPlib_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ int RHSPlib_serial_open(RHSPlib_Serial_T* serial, const char* serialPortName,
{
return RHSPLIB_SERIAL_ERROR;
}

char modifiedSerialPortName[20];
snprintf(modifiedSerialPortName, 20, "\\\\.\\%s", serialPortName);

/* Try to open specified COM-port */
serial->handle = CreateFile(serialPortName,
serial->handle = CreateFile(modifiedSerialPortName,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
Expand Down Expand Up @@ -240,3 +243,7 @@ void RHSPlib_serial_close(RHSPlib_Serial_T* serial)
}
}
}

int RHSPlib_getLastOsError(void) {
return (int) GetLastError();
}
17 changes: 11 additions & 6 deletions packages/rhsplib/src/RHSPlibWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#define CREATE_WORKER(NAME, ENV, RETURN, FUNCTION_BODY) \
auto NAME = new RHSPlibWorker<RETURN>(ENV, [= \
](int &_code, RETURN &_data, uint8_t &_nackCode) mutable FUNCTION_BODY)
](int &_code, RETURN &_data, uint8_t &_nackCode, int &_osErrorCode) mutable FUNCTION_BODY)

/**
* @brief Create a worker whose work function does not return a value (void).
Expand All @@ -33,7 +33,7 @@
*/
#define CREATE_VOID_WORKER(NAME, ENV, FUNCTION_BODY) \
auto NAME = new RHSPlibWorker<void>( \
ENV, [=](int &_code, uint8_t &_nackCode) mutable FUNCTION_BODY)
ENV, [=](int &_code, uint8_t &_nackCode, int &_osErrorCode) mutable FUNCTION_BODY)

/**
* @brief Set the callback function for a worker.
Expand Down Expand Up @@ -118,7 +118,7 @@ class RHSPlibWorker : public Napi::AsyncWorker, public RHSPlibWorkerBase {
*/
void Execute() override {
std::scoped_lock<std::mutex> lock{m_mutex};
workFunction(resultCode, returnData, nackCode);
workFunction(resultCode, returnData, nackCode, osErrorCode);
}

/**
Expand Down Expand Up @@ -147,12 +147,13 @@ class RHSPlibWorker : public Napi::AsyncWorker, public RHSPlibWorkerBase {
void OnError(const Napi::Error &e) override { deferred.Reject(e.Value()); }

private:
std::function<void(int &, TReturn &, uint8_t &)> workFunction;
std::function<void(int &, TReturn &, uint8_t &, int &)> workFunction;
std::function<Napi::Value(Napi::Env, int &, TReturn &)> callbackFunction;
Napi::Promise::Deferred deferred;
TReturn returnData;
int resultCode;
uint8_t nackCode;
int osErrorCode;
};

/**
Expand Down Expand Up @@ -194,7 +195,7 @@ class RHSPlibWorker<void> : public Napi::AsyncWorker, public RHSPlibWorkerBase {
*/
void Execute() override {
std::scoped_lock<std::mutex> lock{m_mutex};
workFunction(resultCode, nackCode);
workFunction(resultCode, nackCode, osErrorCode);
}

/**
Expand All @@ -210,6 +211,9 @@ class RHSPlibWorker<void> : public Napi::AsyncWorker, public RHSPlibWorkerBase {
if (resultCode == RHSPLIB_ERROR_NACK_RECEIVED) {
errorObj.Set("nackCode", nackCode);
}
if(resultCode != 0) {
errorObj.Set("osErrorCode", osErrorCode);
}
deferred.Reject(errorObj);
}
}
Expand All @@ -222,10 +226,11 @@ class RHSPlibWorker<void> : public Napi::AsyncWorker, public RHSPlibWorkerBase {
void OnError(const Napi::Error &e) override { deferred.Reject(e.Value()); }

private:
std::function<void(int &, uint8_t &)> workFunction;
std::function<void(int &, uint8_t &, int&)> workFunction;
Napi::Promise::Deferred deferred;
int resultCode;
uint8_t nackCode;
int osErrorCode;
};

#endif
4 changes: 4 additions & 0 deletions packages/rhsplib/src/serialWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Napi::Value Serial::open(const Napi::CallbackInfo &info) {
const char *serialPortName = serialPortNameStr.c_str();
_code = RHSPlib_serial_open(&this->serialPort, serialPortName, baudrate,
databits, parity, stopbits, flowControl);

if(_code != 0) {
_osErrorCode = RHSPlib_getLastOsError();
}
});

QUEUE_WORKER(worker);
Expand Down