Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/dervomsee/patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
debevv committed Apr 11, 2024
2 parents 50f2285 + cc32513 commit d7ecb2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 3 additions & 5 deletions examples/win32/comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ bool InitCommPort(HANDLE* hComm, int PortNumber) {
}

bool CloseCommPort(HANDLE* hComm) {
if (hComm != INVALID_HANDLE_VALUE)
CloseHandle(hComm);
if (*hComm != INVALID_HANDLE_VALUE)
CloseHandle(*hComm);
else
return false;

Expand All @@ -104,7 +104,6 @@ bool CloseCommPort(HANDLE* hComm) {
int32_t ReadCommPort(HANDLE hComm, uint8_t* buf, uint16_t count, int32_t byte_timeout_ms) {
int TotalBytesRead = 0;
bool Status = false;
bool TimedOut = false;
ULONGLONG StartTime = 0;
uint8_t b;
int tmpByteCount;
Expand All @@ -126,11 +125,10 @@ int32_t ReadCommPort(HANDLE hComm, uint8_t* buf, uint16_t count, int32_t byte_ti

// did we time out yet??
if (GetTickCount64() - StartTime > byte_timeout_ms) {
TimedOut = true;
break;
}

} while (TotalBytesRead < count);

return TotalBytesRead;
}
}
10 changes: 10 additions & 0 deletions nanomodbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ static void discard_1(nmbs_t* nmbs) {
}


#ifndef NMBS_SERVER_DISABLED
static void discard_n(nmbs_t* nmbs, uint16_t n) {
nmbs->msg.buf_idx += n;
}
#endif


static uint16_t get_2(nmbs_t* nmbs) {
Expand All @@ -76,6 +78,7 @@ static void put_2(nmbs_t* nmbs, uint16_t data) {
}


#ifndef NMBS_SERVER_DISABLED
static void set_1(nmbs_t* nmbs, uint8_t data, uint8_t index) {
nmbs->msg.buf[index] = data;
}
Expand All @@ -85,6 +88,7 @@ static void set_2(nmbs_t* nmbs, uint16_t data, uint8_t index) {
nmbs->msg.buf[index] = (uint8_t) ((data >> 8) & 0xFFU);
nmbs->msg.buf[index + 1] = (uint8_t) data;
}
#endif


static uint8_t* get_n(nmbs_t* nmbs, uint16_t n) {
Expand All @@ -94,6 +98,7 @@ static uint8_t* get_n(nmbs_t* nmbs, uint16_t n) {
}


#ifndef NMBS_SERVER_DISABLED
static void put_n(nmbs_t* nmbs, const uint8_t* data, uint8_t size) {
memcpy(&nmbs->msg.buf[nmbs->msg.buf_idx], data, size);
nmbs->msg.buf_idx += size;
Expand All @@ -108,15 +113,18 @@ static uint16_t* get_regs(nmbs_t* nmbs, uint16_t n) {
}
return msg_buf_ptr;
}
#endif


#ifndef NMBS_CLIENT_DISABLED
static void put_regs(nmbs_t* nmbs, const uint16_t* data, uint16_t n) {
uint16_t* msg_buf_ptr = (uint16_t*) (nmbs->msg.buf + nmbs->msg.buf_idx);
nmbs->msg.buf_idx += n * 2;
while (n--) {
msg_buf_ptr[n] = (data[n] << 8) | ((data[n] >> 8) & 0xFF);
}
}
#endif


static void swap_regs(uint16_t* data, uint16_t n) {
Expand Down Expand Up @@ -359,12 +367,14 @@ static void put_msg_header(nmbs_t* nmbs, uint16_t data_length) {
}


#ifndef NMBS_SERVER_DISABLED
static void set_msg_header_size(nmbs_t* nmbs, uint16_t data_length) {
if (nmbs->platform.transport == NMBS_TRANSPORT_TCP) {
data_length += 2;
set_2(nmbs, data_length, 4);
}
}
#endif


static nmbs_error send_msg(nmbs_t* nmbs) {
Expand Down

0 comments on commit d7ecb2c

Please sign in to comment.