From 98b8a2f2fac5df13a39d0ac6847539f7fbfebe8f Mon Sep 17 00:00:00 2001
From: Sergi Hernandez Juan <shernand@iri.upc.edu>
Date: Fri, 7 May 2021 16:16:37 +0200
Subject: [PATCH] The communication device is closed and deleted (if necessary)
 when the config_bus function is called.

---
 src/dynamixelserver_serial.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/dynamixelserver_serial.cpp b/src/dynamixelserver_serial.cpp
index d0199df..03d0676 100644
--- a/src/dynamixelserver_serial.cpp
+++ b/src/dynamixelserver_serial.cpp
@@ -41,8 +41,12 @@ void CDynamixelServerSerial::config_bus(const std::string &device, int baudrate)
 {
   TRS232_config serial_config;
 
-  if(this->comm_dev==NULL)
+  if(this->comm_dev!=NULL)
   {
+    delete this->comm_dev;
+    this->comm_dev=NULL;
+  }
+  try{
     serial_config.baud=baudrate;
     serial_config.num_bits=8;
     serial_config.parity=none;
@@ -52,6 +56,13 @@ void CDynamixelServerSerial::config_bus(const std::string &device, int baudrate)
     this->comm_dev->config(&serial_config);
     this->bus_info.baud_rate=baudrate;
     this->bus_info.device=device;
+  }catch(CException &e){
+    if(this->comm_dev!=NULL)
+    {
+      delete this->comm_dev;
+      this->comm_dev=NULL;
+    }
+    throw e;
   }
 }
 
-- 
GitLab