Share Library works but is unstable
조회 수: 9 (최근 30일)
이전 댓글 표시
I am trying to use the xbee C API libxbee with Matlab and while the functions work, my script is unstable and Matlab crashes at random times while running. I've compiled libxbee into a 64 bit dll and I am using Matlab 2015a 64 bit.
The script will run normally for about 2 minutes then the libxbee functions will return errors and Matlab will freeze and crash. Sometimes I can catch the error and break without crashing and sometimes the script never crashes. I assume this has something to do with how I am passing arguments to the libxbee functions but I can't figure it out.
libxbee info:
Source: https://github.com/attie/libxbee3
Matlab Script:
clear
if (~libisloaded('libxbee3'))
loadlibrary('libxbee3','xbee.h');
end
xbeeP = libpointer('xbeePtr');
[ret, ~, ~, ~] = calllib('libxbee3','xbee_setup_matlab', xbeeP, 'xbee6b', 'COM6', 57600)
while(1)
[ret, ~] = calllib('libxbee3','xbee_validate', xbeeP);
msg = ['Xbee: ', ret];
disp(msg)
pause(1)
end
The function xbee_setup uses variable input arguments with en ellipsis (...) which matlab doesn't support. So I wrote another function xbee_setup_matlab which calls xbee_setup with the correct number of arguments.
Also xbee_setup expects an uninitialized xbeePtrPtr which is why I chose to create the pointer myself rather than let Matlab handle it. Is this still not a good idea?
xbee_setup and xbee_setup_matlab
xbee_err xbee_setup(struct xbee **retXbee, const char *mode, ...) {
xbee_err ret;
va_list ap;
va_start(ap, mode);
ret = xbee_vsetup(retXbee, mode, ap);
va_end(ap);
return ret;
}
xbee_err xbee_setup_matlab(struct xbee **retXbee, const char *mode, const char *device, int baudrate) {
return xbee_setup(retXbee, mode, device, baudrate);
}
답변 (1개)
Philip Borghesani
2015년 7월 14일
You are going to have to debug this yourself. Some suggestions:
- Don't unload the library. Unloading a c or c++ library does almost nothing as far as cleaning it up or resetting it and many static variables do not get initialized correctly when a library is reloaded.
- Avoid using libpointers, in most cases a libstruct object can be correctly passed to a function expecting a pointer to a structure.
- Let MATLAB worry about creating pointers when needed. If it can't do the correct thing (make the correct pointer by adding any number of address operations to the input argument) with any input an error will be produced.
Without documentation and function definitions of the functions you are calling and stack traces from a crash we can't even guess what is going wrong. Simplify your code to minimum that is unstable and supply information about any functions being called.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!