Commit e97b72e3dcb50a4efc03213aa96daab26fa44a17
1 parent
71585404
Export the MODBUS_RTU_RS232/485 and rtu_set_serial_mode on all platforms
Showing
3 changed files
with
12 additions
and
6 deletions
doc/modbus_rtu_set_serial_mode.txt
| @@ -30,7 +30,7 @@ mode: | @@ -30,7 +30,7 @@ mode: | ||
| 30 | automation because it can be used effectively over long distances and in | 30 | automation because it can be used effectively over long distances and in |
| 31 | electrically noisy environments. | 31 | electrically noisy environments. |
| 32 | 32 | ||
| 33 | -This function is only available on Linux kernels 2.6.28 onwards. | 33 | +This function is only supported on Linux kernels 2.6.28 onwards. |
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | RETURN VALUE | 36 | RETURN VALUE |
| @@ -44,6 +44,9 @@ ERRORS | @@ -44,6 +44,9 @@ ERRORS | ||
| 44 | *EINVAL*:: | 44 | *EINVAL*:: |
| 45 | The current libmodbus backend is not RTU. | 45 | The current libmodbus backend is not RTU. |
| 46 | 46 | ||
| 47 | +*ENOTSUP*:: | ||
| 48 | +The function is not supported on your platform. | ||
| 49 | + | ||
| 47 | If the call to ioctl() fails, the error code of ioctl will be returned. | 50 | If the call to ioctl() fails, the error code of ioctl will be returned. |
| 48 | 51 | ||
| 49 | 52 |
src/modbus-rtu.c
| @@ -704,10 +704,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) | @@ -704,10 +704,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) | ||
| 704 | return 0; | 704 | return 0; |
| 705 | } | 705 | } |
| 706 | 706 | ||
| 707 | -#if defined(HAVE_DECL_TIOCSRS485) | ||
| 708 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | 707 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) |
| 709 | { | 708 | { |
| 710 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { | 709 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 710 | +#if defined(HAVE_DECL_TIOCSRS485) | ||
| 711 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 711 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 712 | struct serial_rs485 rs485conf; | 712 | struct serial_rs485 rs485conf; |
| 713 | memset(&rs485conf, 0x0, sizeof(struct serial_rs485)); | 713 | memset(&rs485conf, 0x0, sizeof(struct serial_rs485)); |
| @@ -728,6 +728,13 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | @@ -728,6 +728,13 @@ int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) | ||
| 728 | ctx_rtu->serial_mode = MODBUS_RTU_RS232; | 728 | ctx_rtu->serial_mode = MODBUS_RTU_RS232; |
| 729 | return 0; | 729 | return 0; |
| 730 | } | 730 | } |
| 731 | +#else | ||
| 732 | + if (ctx->debug) { | ||
| 733 | + fprintf(stderr, "This function isn't supported on your platform\n"); | ||
| 734 | + } | ||
| 735 | + errno = ENOTSUP; | ||
| 736 | + return -1; | ||
| 737 | +#endif | ||
| 731 | } | 738 | } |
| 732 | 739 | ||
| 733 | /* Wrong backend and invalid mode specified */ | 740 | /* Wrong backend and invalid mode specified */ |
| @@ -744,7 +751,6 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) { | @@ -744,7 +751,6 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) { | ||
| 744 | return -1; | 751 | return -1; |
| 745 | } | 752 | } |
| 746 | } | 753 | } |
| 747 | -#endif | ||
| 748 | 754 | ||
| 749 | void _modbus_rtu_close(modbus_t *ctx) | 755 | void _modbus_rtu_close(modbus_t *ctx) |
| 750 | { | 756 | { |
src/modbus-rtu.h
| @@ -28,13 +28,10 @@ | @@ -28,13 +28,10 @@ | ||
| 28 | modbus_t* modbus_new_rtu(const char *device, int baud, char parity, | 28 | modbus_t* modbus_new_rtu(const char *device, int baud, char parity, |
| 29 | int data_bit, int stop_bit); | 29 | int data_bit, int stop_bit); |
| 30 | 30 | ||
| 31 | -#if defined(linux) | ||
| 32 | -/* On Linux, we can tell the kernel for RS485 communication */ | ||
| 33 | #define MODBUS_RTU_RS232 0 | 31 | #define MODBUS_RTU_RS232 0 |
| 34 | #define MODBUS_RTU_RS485 1 | 32 | #define MODBUS_RTU_RS485 1 |
| 35 | 33 | ||
| 36 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode); | 34 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode); |
| 37 | int modbus_rtu_get_serial_mode(modbus_t *ctx); | 35 | int modbus_rtu_get_serial_mode(modbus_t *ctx); |
| 38 | -#endif | ||
| 39 | 36 | ||
| 40 | #endif /* _MODBUS_RTU_H_ */ | 37 | #endif /* _MODBUS_RTU_H_ */ |