Commit 8a7e2446a2730c176d5ae9061576231f55c52432
1 parent
90df0215
Change order of few functions in modbus RTU code
Showing
3 changed files
with
33 additions
and
32 deletions
doc/Makefile.am
| @@ -29,9 +29,9 @@ TXT3 = \ | @@ -29,9 +29,9 @@ TXT3 = \ | ||
| 29 | modbus_rtu_set_serial_mode.txt \ | 29 | modbus_rtu_set_serial_mode.txt \ |
| 30 | modbus_rtu_get_rts.txt \ | 30 | modbus_rtu_get_rts.txt \ |
| 31 | modbus_rtu_set_rts.txt \ | 31 | modbus_rtu_set_rts.txt \ |
| 32 | + modbus_rtu_set_custom_rts.txt \ | ||
| 32 | modbus_rtu_get_rts_delay.txt \ | 33 | modbus_rtu_get_rts_delay.txt \ |
| 33 | modbus_rtu_set_rts_delay.txt \ | 34 | modbus_rtu_set_rts_delay.txt \ |
| 34 | - modbus_rtu_set_custom_rts.txt \ | ||
| 35 | modbus_send_raw_request.txt \ | 35 | modbus_send_raw_request.txt \ |
| 36 | modbus_set_bits_from_bytes.txt \ | 36 | modbus_set_bits_from_bytes.txt \ |
| 37 | modbus_set_bits_from_byte.txt \ | 37 | modbus_set_bits_from_byte.txt \ |
src/modbus-rtu.c
| @@ -968,7 +968,7 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) | @@ -968,7 +968,7 @@ int modbus_rtu_get_serial_mode(modbus_t *ctx) | ||
| 968 | } | 968 | } |
| 969 | } | 969 | } |
| 970 | 970 | ||
| 971 | -int modbus_rtu_set_rts(modbus_t *ctx, int mode) | 971 | +int modbus_rtu_get_rts(modbus_t *ctx) |
| 972 | { | 972 | { |
| 973 | if (ctx == NULL) { | 973 | if (ctx == NULL) { |
| 974 | errno = EINVAL; | 974 | errno = EINVAL; |
| @@ -978,19 +978,7 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) | @@ -978,19 +978,7 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) | ||
| 978 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { | 978 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 979 | #if HAVE_DECL_TIOCM_RTS | 979 | #if HAVE_DECL_TIOCM_RTS |
| 980 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 980 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 981 | - | ||
| 982 | - if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP || | ||
| 983 | - mode == MODBUS_RTU_RTS_DOWN) { | ||
| 984 | - ctx_rtu->rts = mode; | ||
| 985 | - | ||
| 986 | - /* Set the RTS bit in order to not reserve the RS485 bus */ | ||
| 987 | - ctx_rtu->set_rts(ctx, ctx_rtu->rts != MODBUS_RTU_RTS_UP); | ||
| 988 | - | ||
| 989 | - return 0; | ||
| 990 | - } else { | ||
| 991 | - errno = EINVAL; | ||
| 992 | - return -1; | ||
| 993 | - } | 981 | + return ctx_rtu->rts; |
| 994 | #else | 982 | #else |
| 995 | if (ctx->debug) { | 983 | if (ctx->debug) { |
| 996 | fprintf(stderr, "This function isn't supported on your platform\n"); | 984 | fprintf(stderr, "This function isn't supported on your platform\n"); |
| @@ -998,13 +986,13 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) | @@ -998,13 +986,13 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) | ||
| 998 | errno = ENOTSUP; | 986 | errno = ENOTSUP; |
| 999 | return -1; | 987 | return -1; |
| 1000 | #endif | 988 | #endif |
| 989 | + } else { | ||
| 990 | + errno = EINVAL; | ||
| 991 | + return -1; | ||
| 1001 | } | 992 | } |
| 1002 | - /* Wrong backend or invalid mode specified */ | ||
| 1003 | - errno = EINVAL; | ||
| 1004 | - return -1; | ||
| 1005 | } | 993 | } |
| 1006 | 994 | ||
| 1007 | -int modbus_rtu_get_rts(modbus_t *ctx) | 995 | +int modbus_rtu_set_rts(modbus_t *ctx, int mode) |
| 1008 | { | 996 | { |
| 1009 | if (ctx == NULL) { | 997 | if (ctx == NULL) { |
| 1010 | errno = EINVAL; | 998 | errno = EINVAL; |
| @@ -1014,7 +1002,19 @@ int modbus_rtu_get_rts(modbus_t *ctx) | @@ -1014,7 +1002,19 @@ int modbus_rtu_get_rts(modbus_t *ctx) | ||
| 1014 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { | 1002 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1015 | #if HAVE_DECL_TIOCM_RTS | 1003 | #if HAVE_DECL_TIOCM_RTS |
| 1016 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 1004 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1017 | - return ctx_rtu->rts; | 1005 | + |
| 1006 | + if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP || | ||
| 1007 | + mode == MODBUS_RTU_RTS_DOWN) { | ||
| 1008 | + ctx_rtu->rts = mode; | ||
| 1009 | + | ||
| 1010 | + /* Set the RTS bit in order to not reserve the RS485 bus */ | ||
| 1011 | + ctx_rtu->set_rts(ctx, ctx_rtu->rts != MODBUS_RTU_RTS_UP); | ||
| 1012 | + | ||
| 1013 | + return 0; | ||
| 1014 | + } else { | ||
| 1015 | + errno = EINVAL; | ||
| 1016 | + return -1; | ||
| 1017 | + } | ||
| 1018 | #else | 1018 | #else |
| 1019 | if (ctx->debug) { | 1019 | if (ctx->debug) { |
| 1020 | fprintf(stderr, "This function isn't supported on your platform\n"); | 1020 | fprintf(stderr, "This function isn't supported on your platform\n"); |
| @@ -1022,24 +1022,23 @@ int modbus_rtu_get_rts(modbus_t *ctx) | @@ -1022,24 +1022,23 @@ int modbus_rtu_get_rts(modbus_t *ctx) | ||
| 1022 | errno = ENOTSUP; | 1022 | errno = ENOTSUP; |
| 1023 | return -1; | 1023 | return -1; |
| 1024 | #endif | 1024 | #endif |
| 1025 | - } else { | ||
| 1026 | - errno = EINVAL; | ||
| 1027 | - return -1; | ||
| 1028 | } | 1025 | } |
| 1026 | + /* Wrong backend or invalid mode specified */ | ||
| 1027 | + errno = EINVAL; | ||
| 1028 | + return -1; | ||
| 1029 | } | 1029 | } |
| 1030 | 1030 | ||
| 1031 | -int modbus_rtu_set_rts_delay(modbus_t *ctx, int us) | 1031 | +int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)) |
| 1032 | { | 1032 | { |
| 1033 | - if (ctx == NULL || us < 0) { | 1033 | + if (ctx == NULL) { |
| 1034 | errno = EINVAL; | 1034 | errno = EINVAL; |
| 1035 | return -1; | 1035 | return -1; |
| 1036 | } | 1036 | } |
| 1037 | 1037 | ||
| 1038 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { | 1038 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1039 | #if HAVE_DECL_TIOCM_RTS | 1039 | #if HAVE_DECL_TIOCM_RTS |
| 1040 | - modbus_rtu_t *ctx_rtu; | ||
| 1041 | - ctx_rtu = (modbus_rtu_t *)ctx->backend_data; | ||
| 1042 | - ctx_rtu->rts_delay = us; | 1040 | + modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1041 | + ctx_rtu->set_rts = set_rts; | ||
| 1043 | return 0; | 1042 | return 0; |
| 1044 | #else | 1043 | #else |
| 1045 | if (ctx->debug) { | 1044 | if (ctx->debug) { |
| @@ -1079,17 +1078,18 @@ int modbus_rtu_get_rts_delay(modbus_t *ctx) | @@ -1079,17 +1078,18 @@ int modbus_rtu_get_rts_delay(modbus_t *ctx) | ||
| 1079 | } | 1078 | } |
| 1080 | } | 1079 | } |
| 1081 | 1080 | ||
| 1082 | -int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)) | 1081 | +int modbus_rtu_set_rts_delay(modbus_t *ctx, int us) |
| 1083 | { | 1082 | { |
| 1084 | - if (ctx == NULL) { | 1083 | + if (ctx == NULL || us < 0) { |
| 1085 | errno = EINVAL; | 1084 | errno = EINVAL; |
| 1086 | return -1; | 1085 | return -1; |
| 1087 | } | 1086 | } |
| 1088 | 1087 | ||
| 1089 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { | 1088 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1090 | #if HAVE_DECL_TIOCM_RTS | 1089 | #if HAVE_DECL_TIOCM_RTS |
| 1091 | - modbus_rtu_t *ctx_rtu = ctx->backend_data; | ||
| 1092 | - ctx_rtu->set_rts = set_rts; | 1090 | + modbus_rtu_t *ctx_rtu; |
| 1091 | + ctx_rtu = (modbus_rtu_t *)ctx->backend_data; | ||
| 1092 | + ctx_rtu->rts_delay = us; | ||
| 1093 | return 0; | 1093 | return 0; |
| 1094 | #else | 1094 | #else |
| 1095 | if (ctx->debug) { | 1095 | if (ctx->debug) { |
src/modbus-rtu.h
| @@ -31,6 +31,7 @@ MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx); | @@ -31,6 +31,7 @@ MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx); | ||
| 31 | 31 | ||
| 32 | MODBUS_API int modbus_rtu_set_rts(modbus_t *ctx, int mode); | 32 | MODBUS_API int modbus_rtu_set_rts(modbus_t *ctx, int mode); |
| 33 | MODBUS_API int modbus_rtu_get_rts(modbus_t *ctx); | 33 | MODBUS_API int modbus_rtu_get_rts(modbus_t *ctx); |
| 34 | + | ||
| 34 | MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)); | 35 | MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)); |
| 35 | 36 | ||
| 36 | MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us); | 37 | MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us); |