|
@@ -385,6 +385,8 @@ static int _modbus_rtu_connect(modbus_t *ctx)
|
|
if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
|
|
if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
|
|
fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
|
|
fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
|
|
(int)GetLastError());
|
|
(int)GetLastError());
|
|
|
|
+ CloseHandle(ctx_rtu->w_ser.fd)
|
|
|
|
+ ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -487,6 +489,8 @@ static int _modbus_rtu_connect(modbus_t *ctx)
|
|
if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
|
|
if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
|
|
fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
|
|
fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
|
|
(int)GetLastError());
|
|
(int)GetLastError());
|
|
|
|
+ CloseHandle(ctx_rtu->w_ser.fd)
|
|
|
|
+ ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
#else
|
|
#else
|
|
@@ -558,6 +562,8 @@ static int _modbus_rtu_connect(modbus_t *ctx)
|
|
/* Set the baud rate */
|
|
/* Set the baud rate */
|
|
if ((cfsetispeed(&tios, speed) < 0) ||
|
|
if ((cfsetispeed(&tios, speed) < 0) ||
|
|
(cfsetospeed(&tios, speed) < 0)) {
|
|
(cfsetospeed(&tios, speed) < 0)) {
|
|
|
|
+ close(ctx->s);
|
|
|
|
+ ctx->s = -1;
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -730,6 +736,8 @@ static int _modbus_rtu_connect(modbus_t *ctx)
|
|
tios.c_cc[VTIME] = 0;
|
|
tios.c_cc[VTIME] = 0;
|
|
|
|
|
|
if (tcsetattr(ctx->s, TCSANOW, &tios) < 0) {
|
|
if (tcsetattr(ctx->s, TCSANOW, &tios) < 0) {
|
|
|
|
+ close(ctx->s);
|
|
|
|
+ ctx->s = -1;
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|