Skip to content

Commit 5bc006b

Browse files
authored
Merge pull request #5 from jimklimov/fix-mingw-build-only-rtu-usb
Fix mingw build only (rtu_usb specific)
2 parents 24b525d + dcbea25 commit 5bc006b

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

src/modbus-rtu-usb.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
418418
}
419419

420420
if (ctx->debug) {
421-
printf("Number of USB devices: %ld\n", devs_len);
421+
// Here and below: cast from ssize_t to a portable type big enough for expected
422+
// values
423+
printf("Number of USB devices: %lld\n", (long long int) devs_len);
422424
}
423425

424426
for (i = 0; i < devs_len; i++) {
@@ -433,16 +435,16 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
433435
if (r != LIBUSB_SUCCESS) {
434436
if (ctx->debug) {
435437
fprintf(stderr,
436-
"libusb_get_device_descriptor for device #%ld failed: %s\n",
437-
i,
438+
"libusb_get_device_descriptor for device #%lld failed: %s\n",
439+
(long long int) i,
438440
libusb_strerror(r));
439441
}
440442
continue;
441443
}
442444

443445
if (ctx->debug) {
444-
printf("Considering device #%ld (%04x:%04x)\n",
445-
i,
446+
printf("Considering device #%lld (%04x:%04x)\n",
447+
(long long int) i,
446448
dev_desc.idVendor,
447449
dev_desc.idProduct);
448450
}
@@ -461,7 +463,9 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
461463
r = libusb_get_port_numbers(d, ud.port_path, sizeof(ud.port_path));
462464
if (r < 1) {
463465
if (ctx->debug) {
464-
fprintf(stderr, "libusb_get_port_numbers for device #%ld failed\n", i);
466+
fprintf(stderr,
467+
"libusb_get_port_numbers for device #%lld failed\n",
468+
(long long int) i);
465469
}
466470
continue;
467471
}
@@ -477,8 +481,8 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
477481
if ((r = libusb_open(d, &dev_handle)) != LIBUSB_SUCCESS) {
478482
if (ctx->debug) {
479483
fprintf(stderr,
480-
"libusb_open for device #%ld failed: %s\n",
481-
i,
484+
"libusb_open for device #%lld failed: %s\n",
485+
(long long int) i,
482486
libusb_strerror(r));
483487
}
484488
continue;
@@ -540,18 +544,19 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
540544

541545
if (is_match) {
542546
if (ctx->debug) {
543-
printf("Found Device %ld (Path %s):\n", i, path_buffer);
547+
printf("Found Device %lld (Path %s):\n", (long long int) i, path_buffer);
544548
printf(" Vendor ID: 0x%04x\n", ud.vid);
545549
printf(" Product ID: 0x%04x\n", ud.pid);
546550
}
547551

548552
ctx_rtu_usb->device_handle = dev_handle;
549553
#if defined HAVE_LIBUSB_POLLFD && HAVE_LIBUSB_POLLFD
550554
if (usb_ctx) {
551-
const struct libusb_pollfd** pollfds = libusb_get_pollfds(usb_ctx);
555+
const struct libusb_pollfd **pollfds = libusb_get_pollfds(usb_ctx);
552556
if (pollfds) {
553-
unsigned j;
554-
for (j=0; pollfds[j] != NULL; j++) {}
557+
unsigned j;
558+
for (j = 0; pollfds[j] != NULL; j++) {
559+
}
555560
if (ctx->debug) {
556561
printf("Got a list of %u libusb file descriptors to poll\n", j);
557562
}
@@ -572,9 +577,10 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx)
572577
}
573578
#else
574579
if (ctx->debug) {
575-
printf("Can not get a list of libusb file descriptors to poll from this libusb version\n");
580+
printf("Can not get a list of libusb file descriptors to poll from this "
581+
"libusb version\n");
576582
}
577-
#endif /* HAVE_LIBUSB_POLLFD */
583+
#endif /* HAVE_LIBUSB_POLLFD */
578584
break;
579585
}
580586

src/modbus.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
388388
if (ctx->s < 0) {
389389
if (ctx->debug) {
390390
/* we may not have an FD with e.g. libusb usage */
391-
fprintf(stderr, "Using a backend without a file descriptor, will not select() on it.\n");
391+
fprintf(stderr, "Using a backend without a file descriptor, will not natively select() on it.\n");
392392
}
393393
} else {
394394
FD_SET(ctx->s, &rset);
@@ -444,12 +444,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
444444
errno = saved_errno;
445445
#endif
446446
}
447-
if (ctx->s >= 0) {
448-
return -1;
449-
}
450-
// else: We have at most tried some default FD's but not
451-
// the (lacking) one for the backend, so fall through for
452-
// its recv method anyway (e.g. query libusb directly).
447+
return -1;
453448
}
454449

455450
rc = ctx->backend->recv(ctx, msg + msg_length, length_to_read);

tests/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ noinst_PROGRAMS += \
1616
endif
1717

1818
common_ldflags = \
19-
$(top_builddir)/src/libmodbus.la \
20-
-lusb-1.0
19+
$(top_builddir)/src/libmodbus.la
2120

2221
if WITH_LIBUSB
22+
common_ldflags += \
23+
-lusb-1.0
24+
2325
apc_test_client_SOURCES = apc-test-client.c
2426
apc_test_client_LDADD = $(common_ldflags)
2527
endif

0 commit comments

Comments
 (0)