diff --git a/src/jetson_gpio.cpp b/src/jetson_gpio.cpp index c38a367..c081382 100644 --- a/src/jetson_gpio.cpp +++ b/src/jetson_gpio.cpp @@ -15,6 +15,7 @@ #include #include +#include namespace jetson_gpio { @@ -65,6 +66,15 @@ bool JetsonGpio::export_gpio() int file_descriptor, buffer_length; char buffer[BUFFER_SIZE]; + // Check target GPIO is available (not opened) first. + // If it is unavailable, close the port and try to open it. + std::string target_gpio = std::string(SYSFS_GPIO_DIR) + std::string("/gpio") + + std::to_string(gpio_); + struct stat return_status; + if (stat(target_gpio.c_str(), &return_status) == 0) { + unexport_gpio(); + } + file_descriptor = open(SYSFS_GPIO_DIR "/export", O_WRONLY); if (file_descriptor < 0) { return false;