diff --git a/iio.h b/iio.h index b835613..ec9830e 100644 --- a/iio.h +++ b/iio.h @@ -27,12 +27,6 @@ extern "C" { #endif #include -#ifdef EXTRA_H - #include - #include - #include - #include -#endif #ifdef _MSC_BUILD /* Come on Microsoft, time to get some C99... */ @@ -116,6 +110,10 @@ __api struct iio_context * iio_create_network_context(const char *host); * NOTE: After that function, the iio_context pointer shall be invalid. */ __api void iio_context_destroy(struct iio_context *ctx); +/** @brief Check if the given context is valid + * @param ctx A pointer to an iio_context structure + * @return if the context is not valid a negative error code is returned */ +__api int iio_context_valid(struct iio_context *ctx); /** @brief Get the version of the backend in use * @param ctx A pointer to an iio_context structure diff --git a/iio_sys_obj.m b/iio_sys_obj.m index da677d5..129d1c4 100644 --- a/iio_sys_obj.m +++ b/iio_sys_obj.m @@ -63,15 +63,18 @@ classdef iio_sys_obj < matlab.System & matlab.system.mixin.Propagates ... % Create network context obj.iio_ctx = calllib(obj.libname, 'iio_create_network_context', obj.ip_address); - % Get the number of devices - try - nb_devices = calllib(obj.libname, 'iio_context_get_devices_count', obj.iio_ctx); - catch + % Check if the network context is valid + ctx_valid = calllib(obj.libname, 'iio_context_valid', obj.iio_ctx); + if(ctx_valid < 0) obj.iio_ctx = {}; unloadlibrary(obj.libname); msgbox('Could not connect to the IIO server!', 'Error','error'); + return; end + % Get the number of devices + nb_devices = calllib(obj.libname, 'iio_context_get_devices_count', obj.iio_ctx); + % If no devices are present unload the library and exit if(nb_devices == 0) calllib(obj.libname, 'iio_context_destroy', obj.iio_ctx);