Hello,
I have written a simple script to test the communication and general interface to the SUB-20 with the LCD in Matlab using the .NET functionality. When I attempt to close the device I receive an error number 18 from the "sub20.GetLastError()" function call. This number is not documented in the user manual (goes up to 16 Feature Not Supported). The board seems to respond just fine after subsequent iterations of the code (no power cycle), but I wanted to make sure there wasn't something I was missing.
Apparently the "upload attachment" function doesn't support .m or .txt extensions, so I have included the code below. In future development I will want to continually read the I2C bus (hence the while(1) loop) and the easiest way I found to ensure the board closes properly is to use the callback on the pushbutton. If there are easier methods known, I'm certainly open to suggestion!
Thanks,
- Mark
% This sample code demonstrates writing to the SUB-20 LCD
%% Start from scratch...
clear all; close all;
disp('Starting LCD test...');
%% Load SUB-20 library
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
%% Setup close GUI
scrsz = get(0,'ScreenSize');
f = figure('Position',[scrsz(3)/2 scrsz(4)/2 100 70]);
h = uicontrol(f,'Style', 'pushbutton', 'String', 'Close SUB-20',...
'Position', [10 1 100 70], 'Callback', 'sub20_close_handle_error(sub20)');
%% Lets see what happens...
count = 0;
% Open first available SUB-20 device
if ~ sub20.Open(0)
sub20_handle_error(sub20);
end
while(1)
% Output dummy string to LCD
success = sub20.LCD_Write(['\fHello...\n ' num2str(count)]);
if ~ success
break;
end
count = count + 1;
pause(2);
end
sub20_close_handle_error(sub20);
disp('Exiting LCD test...');
*** Separate m-file !!! ***
function sub20_close_handle_error(sub20)
% Close SUB-20 device
foo = sub20.Close();
% ERROR HANDLING
err = sub20.GetLastError();
if err > 0
switch (err)
case (1)
err_txt = 'SUB Device not found';
case (2)
err_txt = 'Cant open SUB device';
case (3)
err_txt = 'Cant set configuration';
case (4)
err_txt = 'Cant claim interface';
case (5)
err_txt = 'Failed to setup async transaction';
case (6)
err_txt = 'Failed to submis async transaction';
case (7)
err_txt = 'Bulk write failed';
case (8)
err_txt = 'Bulk read failed';
case (9)
err_txt = 'Bulk read incomplete';
case (10)
err_txt = 'Out buffer overflow';
case (11)
err_txt = 'I2C error';
case (12)
err_txt = 'Wrong tag code in response';
case (13)
err_txt = 'Wrong tag size in response';
case (14)
err_txt = 'Wrong parameter';
case (15)
err_txt = 'SPI disabled';
case (16)
err_txt = 'Feature not supported';
otherwise
err_txt = 'Unknown error';
end
error(['Error ' num2str(err) ': ' err_txt])
end
end
Receiving Error 18 when using Matlab .NET... normal???
Moderator: serg
Re: Receiving Error 18 when using Matlab .NET... normal???
Hello Mark,
I am not a Matlab expert. I don't know how the "callback" function works in matlab. I would guess that the error 18 is not the sub20.Close result but the sub20.LCD_Write, after you close the handle in the callback. I still don't understand how callback helps you to terminate the while(1) loop. I have modified your script a bit. By running it I didn't see any "Error 18" issues.
BTW you can use sub20.GetStrError instead of switch/case. Here is the script
%----------------------------------------------------------------------------
% This sample code demonstrates writing to the SUB-20 LCD
%% Start from scratch...
clear all; close all;
disp('Starting LCD test...');
%% Load SUB-20 library
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
%% Setup close GUI
scrsz = get(0,'ScreenSize');
f = figure('Position',[scrsz(3)/2 scrsz(4)/2 100 70]);
h = uicontrol(f,'Style', 'pushbutton', 'String', 'Close SUB-20',...
'Position', [10 1 100 70], 'Callback', 'run=0');
%% Lets see what happens...
count = 0;
% Open first available SUB-20 device
if ~ sub20.Open(0)
sub20_handle_error(sub20);
end
run=1
while(run)
% Output dummy string to LCD
success = sub20.LCD_Write(['\fHello...\n ' num2str(count)]);
if ~ success
break;
end
count = count + 1;
pause(2);
end
success = sub20.Close();
if ~ success
disp(sub20.GetStrError(sub20.GetLastError()));
end
disp('Exiting LCD test...');
%----------------------------------------------------------------------------
Regards
I am not a Matlab expert. I don't know how the "callback" function works in matlab. I would guess that the error 18 is not the sub20.Close result but the sub20.LCD_Write, after you close the handle in the callback. I still don't understand how callback helps you to terminate the while(1) loop. I have modified your script a bit. By running it I didn't see any "Error 18" issues.
BTW you can use sub20.GetStrError instead of switch/case. Here is the script
%----------------------------------------------------------------------------
% This sample code demonstrates writing to the SUB-20 LCD
%% Start from scratch...
clear all; close all;
disp('Starting LCD test...');
%% Load SUB-20 library
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
%% Setup close GUI
scrsz = get(0,'ScreenSize');
f = figure('Position',[scrsz(3)/2 scrsz(4)/2 100 70]);
h = uicontrol(f,'Style', 'pushbutton', 'String', 'Close SUB-20',...
'Position', [10 1 100 70], 'Callback', 'run=0');
%% Lets see what happens...
count = 0;
% Open first available SUB-20 device
if ~ sub20.Open(0)
sub20_handle_error(sub20);
end
run=1
while(run)
% Output dummy string to LCD
success = sub20.LCD_Write(['\fHello...\n ' num2str(count)]);
if ~ success
break;
end
count = count + 1;
pause(2);
end
success = sub20.Close();
if ~ success
disp(sub20.GetStrError(sub20.GetLastError()));
end
disp('Exiting LCD test...');
%----------------------------------------------------------------------------
Regards
Re: Receiving Error 18 when using Matlab .NET... normal???
Hi Serg,
Thank you for your speedy reply (apologies for my lengthy speed of reply). I am somewhat new to the 'callback' functionality in Matlab myself, but with a slight modification to the uicontrol function call (calling 'run=0;' as opposed to my 'sub20_close_handle_error(sub20)'), the simple piece of test code now exits the while loop and does not cause an error.
So for anybody reading this thread, the following is an easy way to pop up a button for the user to press when they want to break the while loop and exit the application:
h = uicontrol(f,'Style', 'pushbutton', 'String', 'Close SUB-20',...
'Position', [10 1 100 70], 'Callback', 'run = 0;');
Thanks again,
- Mark
Thank you for your speedy reply (apologies for my lengthy speed of reply). I am somewhat new to the 'callback' functionality in Matlab myself, but with a slight modification to the uicontrol function call (calling 'run=0;' as opposed to my 'sub20_close_handle_error(sub20)'), the simple piece of test code now exits the while loop and does not cause an error.
So for anybody reading this thread, the following is an easy way to pop up a button for the user to press when they want to break the while loop and exit the application:
h = uicontrol(f,'Style', 'pushbutton', 'String', 'Close SUB-20',...
'Position', [10 1 100 70], 'Callback', 'run = 0;');
Thanks again,
- Mark