Matlab loadlibrary, reading from SUB20

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
sanjosanjo
Posts: 4
Joined: Wed Jan 11, 2012 2:00 pm

Matlab loadlibrary, reading from SUB20

Post by sanjosanjo »

Hi.
I'm stuck with using the old "loadlibrary" method in Matlab because my version doesn't support the .NET interface. I've been able to send commands with no problem, both to the LCD and the SPI interface. However, I'm unable to read anything back using Matlab. In my script I want to check if the module is properly connected to the USB port, so I'm trying to read the serial number of the module after I open it.
I run
hSub20=calllib('sub20', 'sub_open', libpointer);
calllib('sub20', 'sub_lcd_write', hSub20, 'hello' );
and I can see that communication is working properly because the LCD is updated.

I tried:
calllib('sub20', 'get_serial_number', hSub20, libpointer)
and I get an error "Method was not found"

What is the proper syntax to read the serial number? In addition, is there a way to test whether 'sub_open' worked properly?

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Matlab loadlibrary, reading from SUB20

Post by serg »

Hi sanjosanjo,

Here is the code you can use

Code: Select all

loadlibrary('C:\WINDOWS\system32\sub20.dll','C:\projects\sub20\matlab\libsub.h');
hSub20=calllib('sub20', 'sub_open', libpointer);
buf = 'a':'z';
[size, dummy, SerialNumber] = calllib('sub20', 'sub_get_serial_number', hSub20, buf, length(buf) );
status=calllib('sub20', 'sub_close', hSub20);
Please note, that Matlab doesn't support "C" unions and variable number of arguments in the header file, thus it cannot convert the original "C:\Program Files\SUB-20\inc\libsub.h" into internal format. The header file should be slightly modified by removing unsupported elements. I have attached the modified version of the header file.

If sub_open worked properly the value of the hSub20 handle is not NULL. You can also check the "size" return value. If call was success it will contain "5". In case of error it will contain a "negative" number corresponding to the error occured. You can refer to the SUB-20 "User manual" for the error codes.
Attachments
libsub.zip
(4.36 KiB) Downloaded 859 times

sanjosanjo
Posts: 4
Joined: Wed Jan 11, 2012 2:00 pm

Re: Matlab loadlibrary, reading from SUB20

Post by sanjosanjo »

Thanks for the quick reply!

The first thing I notice is some warnings with the new .h file when I perform "loadlibrary(...)":
Warning: The function 'sub_reset' was not found in the library
Warning: The function 'sub_eep_read' was not found in the library
Warning: The function 'sub_eep_write' was not found in the library

I'm using SUB-20-110912 release.

I was able to read back the serial number, so that is fine.

Regarding the value of hSub20 returned from the sub_open command, I can't read any information from it. The value of hSub20 is simply "libpointer", regardless of whether sub_open was successful or not (I tried opening it with and without the hardware plugged in). So I can't gather any information from the sub_open command.

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Matlab loadlibrary, reading from SUB20

Post by serg »

Yes, these warnings due to the newer version of the header file.

Starting MATLAB 7.6(R2008a), you can use the function 'IsNull' to verify if a LIBPOINTER is pointing to a null reference.
For previous product releases use a try-catch block
http://www.mathworks.com/support/soluti ... n=1-4WQ8HZ

I was wrong about the "size" return value in my previous post. If the liibpointer is null, you would probably get an exception, so the IsNull function is the right solution in this case

Post Reply