Changeset 234

Show
Ignore:
Timestamp:
08/16/08 00:05:29 (5 years ago)
Author:
behrens
Message:

add libusb windows modifications (only one libusb subfunction win and linux), Version seems to be stable! :-)

Location:
branches/behrens/mfiles
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • branches/behrens/mfiles/COM_CloseNXT.m

    r232 r234  
    6363% 
    6464% Signature 
    65 %   Author: Linus Atorf (see AUTHORS) 
    66 %   Date: 2008/07/09 
     65%   Author: Linus Atorf, Alexander Behrens (see AUTHORS) 
     66%   Date: 2008/08/15 
    6767%   Copyright: 2007-2008, RWTH Aachen University 
    6868% 
     
    142142                        USB_CloseHandle_Windows(h); 
    143143                    case 2 % libusb library 
    144                         USB_CloseHandle_Windows_libusb(h); 
     144                        USB_CloseHandle_libusb(h); 
    145145                    otherwise 
    146146                end 
    147147            else % linux 
    148                 USB_CloseHandle_Linux(h); 
     148                USB_CloseHandle_libusb(h); 
    149149            end%if 
    150150        else % BT 
     
    367367 
    368368 
     369%% --- FUNCTION USB_CloseHandle_libusb 
     370function USB_CloseHandle_libusb(h) 
     371 
     372    LIBUSB_Interface = 0; 
     373 
     374     
     375    if h.ConnectionLibraryValue == 1  % fantom library 
     376        %   // Send EOF marker, a zero length packet 
     377        %   usb_bulk_write(nxt->hdl, 0x1, buf, 0, 1000); 
     378        buffer = uint8(zeros(64,1)); 
     379        textOut(sprintf('  - Sending a zero length packet... '));     
     380        ret = calllib(h.ConnectionLibraryName, 'usb_bulk_read', h.Handle, 1, buffer, LIBUSB_Interface, 1000); 
     381        displayLibusbStatus(ret, h); 
     382 
     383        %   // Turn off stream mode 
     384        %   usb_control_msg(nxt->hdl, 0x41, 0x1, 0, 0, NULL, 0, 5000); 
     385        buffer = blanks(64); 
     386        textOut(sprintf('  - Turn off the stream mode... '));         
     387        ret = calllib(h.ConnectionLibraryName, 'usb_control_msg', h.Handle, 65, 1, 0, 0, buffer, LIBUSB_Interface, 5000); 
     388        displayLibusbStatus(ret, h); 
     389 
     390        %   // Discard any data that is left in the buffer 
     391        %   while (usb_bulk_read(nxt->hdl, 0x82, buf, sizeof(buf), 1) > 0) 
     392        %     ; 
     393        textOut(sprintf('  - Discard any data that is left in the buffer... '));         
     394        ret = 1; 
     395        while ret > 0 
     396          buffer = uint8(zeros(64,1));        
     397          ret = calllib(h.ConnectionLibraryName, 'usb_bulk_read', h.Handle, 130, buffer, 64, 1); 
     398        end 
     399    end % end if fantom 
     400     
     401     
     402    % interface number is hardcoded, should it be that way? 
     403    textOut(sprintf('  - Releasing interface... ')); 
     404    ret = calllib(h.ConnectionLibraryName, 'usb_release_interface', h.Handle, LIBUSB_Interface); 
     405    displayLibusbStatus(ret, h); 
     406 
     407 
     408    textOut(sprintf('  - Closing device... ')); 
     409    ret = calllib(h.ConnectionLibraryName, 'usb_close', h.Handle); 
     410    displayLibusbStatus(ret, h); 
     411 
     412end%function 
     413 
     414 
    369415%% --- FUNCTION USB_CloseHandle_Windows_libusb 
    370416function USB_CloseHandle_Windows_libusb(h) 
     
    378424    textOut(sprintf('  - Sending a zero length packet... '));     
    379425    ret = calllib('libusb0', 'usb_bulk_read', h.Handle, 1, buffer, LIBUSB_Interface, 1000); 
    380     displayLibusbStatus(ret); 
     426    displayLibusbStatus(ret, h); 
    381427     
    382428    %   // Turn off stream mode 
     
    385431    textOut(sprintf('  - Turn off the stream mode... '));         
    386432    ret = calllib('libusb0', 'usb_control_msg', h.Handle, 65, 1, 0, 0, buffer, LIBUSB_Interface, 5000); 
    387     displayLibusbStatus(ret); 
     433    displayLibusbStatus(ret, h); 
    388434     
    389435    %   // Discard any data that is left in the buffer 
     
    400446    textOut(sprintf('  - Releasing interface... ')); 
    401447    ret = calllib('libusb0', 'usb_release_interface', h.Handle, LIBUSB_Interface); 
    402     displayLibusbStatus(ret); 
     448    displayLibusbStatus(ret, h); 
    403449 
    404450 
    405451    textOut(sprintf('  - Closing device... ')); 
    406452    ret = calllib('libusb0', 'usb_close', h.Handle); 
    407     displayLibusbStatus(ret); 
     453    displayLibusbStatus(ret, h); 
    408454 
    409455end%function 
     
    418464    textOut(sprintf('  - Releasing interface... ')); 
    419465    ret = calllib('libusb', 'usb_release_interface', h.Handle, LIBUSB_Interface); 
    420     displayLibusbStatus(ret); 
     466    displayLibusbStatus(ret, h); 
    421467 
    422468 
    423469    textOut(sprintf('  - Closing device... ')); 
    424470    ret = calllib('libusb', 'usb_close', h.Handle); 
    425     displayLibusbStatus(ret); 
     471    displayLibusbStatus(ret, h); 
    426472 
    427473    % we don't unload libusb now, but if we did: 
     
    435481 
    436482 
    437 %% --- FUNCTION displayUSBWinStatus(status) 
     483%% --- FUNCTION displayUSBWinStatus 
    438484function displayUSBWinStatus(status) 
    439485    if status 
     
    446492 
    447493 
    448 %% --- FUNCTION displayLibusbStatus(status) 
    449 function displayLibusbStatus(status) 
     494%% --- FUNCTION displayLibusbStatus 
     495function displayLibusbStatus(status, h) 
    450496    if isnumeric(status) && (status < 0) 
    451497        textOut(sprintf('failed.\n')); 
    452         textOut(sprintf(['Libusb error ' num2str(status) ': ' getLibusbErrorString(status) '\n'])) 
     498        textOut(sprintf(['Libusb error ' num2str(status) ': ' getLibusbErrorString(status, h) '\n'])) 
    453499    else 
    454500        textOut(sprintf('done.\n')); 
  • branches/behrens/mfiles/COM_CollectPacket.m

    r232 r234  
    101101            end 
    102102        else % Linux 
    103             [type cmd statusbyte content] = USB_CollectPacket_Linux(handle); 
     103            [type cmd statusbyte content] = USB_CollectPacket_libusb(handle); 
    104104        end%if 
    105105    else % BT 
     
    436436    [status something reply] = calllib(h.ConnectionLibraryName, 'usb_bulk_read', h.Handle, receivingEndpoint, buffer, length(buffer), timeout);     
    437437    if status < 0 
    438         msg = ['Libusb error ' num2str(status) ' while receiving data: ' getLibusbErrorString(status)]; 
     438        msg = ['Libusb error ' num2str(status) ' while receiving data: ' getLibusbErrorString(status, h)]; 
    439439        warning('MATLAB:RWTHMindstormsNXT:USB:libusbErrorWhileReceivingData', msg); 
    440440    end%if 
     
    514514    [status something reply] = calllib('libusb', 'usb_bulk_read', h.Handle, receivingEndpoint, buffer, length(buffer), timeout);     
    515515    if status < 0 
    516         msg = ['Libusb error ' num2str(status) ' while receiving data: ' getLibusbErrorString(status)]; 
     516        msg = ['Libusb error ' num2str(status) ' while receiving data: ' getLibusbErrorString(status, h)]; 
    517517        warning('MATLAB:RWTHMindstormsNXT:USB:Windows:libusbErrorWhileReceivingData', msg); 
    518518    end%if 
  • branches/behrens/mfiles/COM_OpenNXTEx.m

    r233 r234  
    245245%     handle.IniFilename           = h.IniFilename; 
    246246%     handle.ComPort               = h.ComPort; 
    247 %     handle.BaudRate                    = h.BaudRate; 
    248 %     handle.DataBits                    = h.DataBits; 
    249 %     handle.Timeout                     = h.Timeout; 
     247%     handle.BaudRate              = h.BaudRate; 
     248%     handle.DataBits              = h.DataBits; 
     249%     handle.Timeout               = h.Timeout; 
    250250%  
    251 %     handle.SendSendPause               = h.SendSendPause; 
     251%     handle.SendSendPause         = h.SendSendPause; 
    252252%     handle.SendReceivePause      = h.SendReceivePause; 
    253253%  
    254254%     handle.NXTMAC                = h.NXTMAC; 
    255 %     handle.CreationTime               = h.CreationTime;   
     255%     handle.CreationTime         = h.CreationTime;   
    256256%     handle.Index                 = h.Index; 
    257257     
     
    391391     
    392392    if hIn.OSValue == 1 
    393 %         switch (hIn.ConnectionLibraryValue) 
    394 %             case 1 % fantom library 
    395 %                 USB_CloseHandle_Windows(h); 
    396 %             case 2 % libusb library 
    397 %                 USB_CloseHandle_Windows_libusb(h); 
    398 %             otherwise 
    399 %           end 
     393        switch (hIn.ConnectionLibraryValue) 
     394            case 1 % fantom library 
     395                hOut = USB_OpenHandle_Windows(hIn, SuppressErrors); 
     396            case 2 % libusb library 
     397                hOut = USB_OpenHandle_libusb(hIn, SuppressErrors); 
     398            otherwise 
     399                % use libusb0 as default 
     400                unknownLibrary = hIn.ConnectionLibraryValue; 
     401                if isnan(unknownLibrary) 
     402                    unknownLibrary = num2str(unknownLibrary); 
     403                end 
     404                hIn.ConnectionLibraryName  = 'libusb0'; 
     405                hIn.ConnectionLibraryValue = 2; 
     406                msg = ['Unkown library ' unknownLibrary '. Use as default: ' hIn.ConnectionLibraryName]; 
     407                warning('MATLAB:RWTHMindstormsNXT:USB:UnknownNXTDriverLibrary', msg); 
     408                hOut = USB_OpenHandle_libusb(hIn, SuppressErrors); 
     409        end 
    400410%         hOut = USB_OpenHandle_Windows(hIn, SuppressErrors); 
    401         hOut = USB_OpenHandle_Windows_libusb(hIn, false);         
     411%         hOut = USB_OpenHandle_libusb(hIn, false);         
    402412    elseif hIn.OSValue == 2 
    403         hOut = USB_OpenHandle_Linux(hIn, SuppressErrors); 
     413        hOut = USB_OpenHandle_libusb(hIn, SuppressErrors); 
    404414    end%if 
    405415     
     
    579589 
    580590 
    581 %% --- FUNCTION displayLibusbStatus(status) 
     591%% --- FUNCTION displayLibusbStatus 
    582592% little convenient helper to save some lines of code with textOut 
    583 function displayLibusbStatus(status) 
     593function displayLibusbStatus(status, h) 
    584594% little convenient helper to save some lines of code with textOut 
    585595    if isnumeric(status) && (status < 0) 
    586596        textOut(sprintf('failed.\n')); 
    587         textOut(sprintf(['Libusb error ' num2str(status) ': ' getLibusbErrorString(status) '\n'])) 
     597        textOut(sprintf(['Libusb error ' num2str(status) ': ' getLibusbErrorString(status, h) '\n'])) 
    588598    else 
    589599        textOut(sprintf('done.\n')); 
     
    669679    textOut(sprintf('    . finding busses... ')) 
    670680    ret = calllib('libusb', 'usb_find_busses'); 
    671     displayLibusbStatus(ret); 
     681    displayLibusbStatus(ret, hIn); 
    672682     
    673683    textOut(sprintf('    . finding devices... ')) 
    674684    ret = calllib('libusb', 'usb_find_devices'); 
    675     displayLibusbStatus(ret); 
     685    displayLibusbStatus(ret, hIn); 
    676686 
    677687%% Get main root bus 
     
    680690    p = calllib('libusb', 'usb_get_busses'); 
    681691    bus = libstruct('usb_bus', p); 
    682     displayLibusbStatus(p); 
     692    displayLibusbStatus(p, hIn); 
    683693     
    684694 
     
    759769                textOut(sprintf('    . opening device... ')); 
    760770                DevHandle = calllib('libusb', 'usb_open', dev); 
    761                 displayLibusbStatus(DevHandle); 
     771                displayLibusbStatus(DevHandle, hIn); 
    762772 
    763773                textOut(sprintf('    . reading serial number... ')); 
     
    766776                buffer = blanks(255); 
    767777                [bytesRead newHandleOrWhat SerialNo] = calllib('libusb', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber,  buffer, length(buffer)); 
    768                 displayLibusbStatus(bytesRead); 
     778                displayLibusbStatus(bytesRead, hIn); 
    769779                                 
    770780                SerialNo = strtrim(SerialNo); % just to be safe 
     
    782792                    textOut(sprintf('    . closing device... ')); 
    783793                    status = calllib('libusb', 'usb_close', DevHandle); 
    784                     displayLibusbStatus(status); 
     794                    displayLibusbStatus(status, hIn); 
    785795                end%if 
    786796                 
     
    849859    %calllib('libusb', 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); 
    850860    ret = calllib('libusb', 'usb_set_configuration', DevHandle, LIBUSB_Configuration); 
    851     displayLibusbStatus(ret); 
     861    displayLibusbStatus(ret, hIn); 
    852862    if (ret < 0) 
    853863        ErrorWhileOpening = true; 
     
    859869    %calllib('libusb', 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); 
    860870    ret = calllib('libusb', 'usb_claim_interface', DevHandle, LIBUSB_Interface); 
    861     displayLibusbStatus(ret); 
     871    displayLibusbStatus(ret, hIn); 
    862872    if (ret < 0) 
    863873        ErrorWhileOpening = true; 
     
    878888    textOut(sprintf('    . resetting device... ')); 
    879889    ret = calllib('libusb', 'usb_reset', DevHandle); 
    880     displayLibusbStatus(ret); 
     890    displayLibusbStatus(ret, hIn); 
    881891    if (ret < 0) 
    882892        ErrorWhileOpening = true; 
     
    912922 
    913923 
    914 %% --- FUNCTION USB_OpenHandle_Linux 
    915 function hOut = USB_OpenHandle_Windows_libusb(hIn, SuppressErrors) 
     924%% --- FUNCTION USB_OpenHandle_libusb 
     925function hOut = USB_OpenHandle_libusb(hIn, SuppressErrors) 
    916926% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
    917927% Temporary USB construction site for the RWTH - Mindstorms NXT Toolbox 
    918928%           http://www.mindstorms.rwth-aachen.de 
    919929% 
    920 % Alexander Behrens, 15.08.2008 
     930% Linus Atorf, Alexander Behrens, 15.08.2008 
    921931% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
    922932 
     
    953963     
    954964%% Load libusb library (only if necessary) 
    955     if ~libisloaded('libusb0') 
    956         textOut(sprintf('  - Loading library "libusb0"... ')); 
     965    if ~libisloaded(hIn.ConnectionLibraryName) 
     966        textOut(sprintf('  - Loading library "%s"... ', hIn.ConnectionLibraryName)); 
    957967        try 
    958968            % use "our" wrapper file... 
    959             loadlibrary('libusb0', @libusb_win_proto) 
     969            loadlibrary(hIn.ConnectionLibraryName, @libusb_win_proto) 
    960970            textOut(sprintf('done.\n')); 
    961971        catch  
    962972            textOut(sprintf('failed.\n')); 
    963973            if ~SuppressErrors 
    964                 error('MATLAB:RWTHMindstormsNXT:USB:Linux:couldNotLoadLibraryLibusb', 'The "libusb" library could not be loaded (file "libusb0.sys"). Make sure it is installed and paths are set correctly!') 
     974                error('MATLAB:RWTHMindstormsNXT:USB:couldNotLoadLibraryLibusb', 'The "libusb" library could not be loaded. Make sure it is installed and paths are set correctly!') 
    965975            else 
    966976                return 
     
    968978        end%try 
    969979    else 
    970         textOut(sprintf('  - Library "libusb0" already loaded.\n')); 
    971     end%if 
    972  
    973     hOut.ConnectionLibraryName = 'libusb0';  % String, 'fantom' or 'libusb' / 'libusb0' 
    974     hOut.ConnectionLibraryValue = 2; % fantom = 1, libusb = 2 
     980        textOut(sprintf('  - Library "%s" already loaded.\n',hIn.ConnectionLibraryName)); 
     981    end%if 
    975982 
    976983    textOut(sprintf('  - Initializing and browsing USB busses\n')); 
    977984 
    978985%% Init libusb 
    979     textOut(sprintf('    . initializing libusb0.\n')) 
    980     calllib('libusb0', 'usb_init'); 
     986    textOut(sprintf('    . initializing %s.\n',hIn.ConnectionLibraryName)) 
     987    calllib(hIn.ConnectionLibraryName, 'usb_init'); 
    981988     
    982989    % > - insert usb_set_debug(255) in pyusb.c right behind usb_init() 
     
    991998    % devices since last call, don't neet it... 
    992999    textOut(sprintf('    . finding busses... ')) 
    993     ret = calllib('libusb0', 'usb_find_busses'); 
    994     displayLibusbStatus(ret); 
     1000    ret = calllib(hIn.ConnectionLibraryName, 'usb_find_busses'); 
     1001    displayLibusbStatus(ret, hIn); 
    9951002     
    9961003    textOut(sprintf('    . finding devices... ')) 
    997     ret = calllib('libusb0', 'usb_find_devices'); 
    998     displayLibusbStatus(ret); 
     1004    ret = calllib(hIn.ConnectionLibraryName, 'usb_find_devices'); 
     1005    displayLibusbStatus(ret, hIn); 
    9991006 
    10001007%% Get main root bus 
    10011008     
    10021009    textOut(sprintf('    . getting root bus object... ')) 
    1003     p = calllib('libusb0', 'usb_get_busses'); 
     1010    p = calllib(hIn.ConnectionLibraryName, 'usb_get_busses'); 
    10041011    bus = libstruct('usb_bus', p); 
    1005     displayLibusbStatus(p); 
     1012    displayLibusbStatus(p, hIn); 
    10061013     
    10071014 
     
    10441051            % It's debug-info only anyway 
    10451052 
    1046            % open device to get a handle 
    1047             DevHandle = calllib('libusb0', 'usb_open', dev); 
    1048       
    1049             % now the string stuff 
    1050             buffer = blanks(255); 
    1051             % we don't need a real buffer or pointer, matlab seems to do this 
    1052             % for us, so we pass that buffer variable, without really needing 
    1053             % it. it seems like matlab "knows" how strings get written by 
    1054             % reference and returns the new value from the function. if you 
    1055             % compare the matlab-returnvalues of usb_get_string_simple using 
    1056             % libfunctionsview libusb, you'll find that they don't match whats 
    1057             % written inside usb.h. very nice and handy, thank you matlab :-) 
    1058             %pBuffer = libpointer('cstring', buffer); not needed, see above 
    10591053             
    1060             % now the actual call: 
    1061             [bytesRead newHandleOrWhat ManufacturerName] = calllib('libusb0', 'usb_get_string_simple', ... 
    1062                        DevHandle, dev.descriptor.iManufacturer,  buffer, length(buffer)); 
    1063             % again: 
    1064             [bytesRead newHandleOrWhat ProductName] = calllib('libusb0', 'usb_get_string_simple', ... 
    1065                        DevHandle, dev.descriptor.iProduct,  buffer, length(buffer)); 
    1066                     
    1067             disp(sprintf('        Manufacturer: %s', ManufacturerName)) 
    1068             disp(sprintf('        Product: %s', ProductName)) 
    1069              
    1070             % close device again 
    1071             ret = calllib('libusb0', 'usb_close', DevHandle); 
    1072             clear DevHandle %better doing it now than forgetting it later 
     1054            if hIn.OSValue == 1 % windows 
     1055                % open device to get a handle 
     1056                DevHandle = calllib(hIn.ConnectionLibraryName, 'usb_open', dev); 
     1057 
     1058                % now the string stuff 
     1059                buffer = blanks(255); 
     1060                % we don't need a real buffer or pointer, matlab seems to do this 
     1061                % for us, so we pass that buffer variable, without really needing 
     1062                % it. it seems like matlab "knows" how strings get written by 
     1063                % reference and returns the new value from the function. if you 
     1064                % compare the matlab-returnvalues of usb_get_string_simple using 
     1065                % libfunctionsview libusb, you'll find that they don't match whats 
     1066                % written inside usb.h. very nice and handy, thank you matlab :-) 
     1067                %pBuffer = libpointer('cstring', buffer); not needed, see above 
     1068 
     1069                % now the actual call: 
     1070                [bytesRead newHandleOrWhat ManufacturerName] = calllib(hIn.ConnectionLibraryName, 'usb_get_string_simple', ... 
     1071                           DevHandle, dev.descriptor.iManufacturer,  buffer, length(buffer)); 
     1072                % again: 
     1073                [bytesRead newHandleOrWhat ProductName] = calllib(hIn.ConnectionLibraryName, 'usb_get_string_simple', ... 
     1074                           DevHandle, dev.descriptor.iProduct,  buffer, length(buffer)); 
     1075 
     1076                disp(sprintf('        Manufacturer: %s', ManufacturerName)) 
     1077                disp(sprintf('        Product: %s', ProductName)) 
     1078 
     1079                % close device again 
     1080                ret = calllib(hIn.ConnectionLibraryName, 'usb_close', DevHandle); 
     1081                displayLibusbStatus(ret, hIn); 
     1082                clear DevHandle %better doing it now than forgetting it later 
     1083            end % end if windows 
    10731084 
    10741085 
     
    10811092 
    10821093                textOut(sprintf('    . opening device... ')); 
    1083                 DevHandle = calllib('libusb0', 'usb_open', dev); 
    1084                 displayLibusbStatus(DevHandle); 
     1094                DevHandle = calllib(hIn.ConnectionLibraryName, 'usb_open', dev); 
     1095                displayLibusbStatus(DevHandle, hIn); 
    10851096 
    10861097                textOut(sprintf('    . reading serial number... ')); 
     
    10881099                % int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); 
    10891100                buffer = blanks(255); 
    1090                 [bytesRead newHandleOrWhat SerialNo] = calllib('libusb0', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber,  buffer, length(buffer)); 
    1091                 displayLibusbStatus(bytesRead); 
     1101                [bytesRead newHandleOrWhat SerialNo] = calllib(hIn.ConnectionLibraryName, 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber,  buffer, length(buffer)); 
     1102                displayLibusbStatus(bytesRead, hIn); 
    10921103                                 
    10931104                SerialNo = strtrim(SerialNo); % just to be safe 
     
    11041115                    % don't forget to close the opened NXT that didnt match... 
    11051116                    textOut(sprintf('    . closing device... ')); 
    1106                     status = calllib('libusb0', 'usb_close', DevHandle); 
    1107                     displayLibusbStatus(status); 
     1117                    status = calllib(hIn.ConnectionLibraryName, 'usb_close', DevHandle); 
     1118                    displayLibusbStatus(status, hIn); 
    11081119                end%if 
    11091120                 
     
    11481159        if ~SuppressErrors 
    11491160            errordlg('No NXT found on USB bus! Make sure the NXT is turned on and access rights in /dev/ are properly set. Rebooting your NXT might help!') 
    1150             error('MATLAB:RWTHMindstormsNXT:USB:Linux:noNXTfound', 'No NXT found on USB bus! Make sure the NXT is turned on and access rights in /dev/ are properly set. Rebooting your NXT might help!') 
     1161            error('MATLAB:RWTHMindstormsNXT:USB:noNXTfound', 'No NXT found on USB bus! Make sure the NXT is turned on and access rights in /dev/ are properly set. Rebooting your NXT might help!') 
    11511162        else 
    11521163            % well, no NXT, exit silently 
     
    11711182    % somehow this doesn't work, so we use the hardcoded configuration 1! 
    11721183    %calllib('libusb', 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); 
    1173     ret = calllib('libusb0', 'usb_set_configuration', DevHandle, LIBUSB_Configuration); 
    1174     displayLibusbStatus(ret); 
     1184    ret = calllib(hIn.ConnectionLibraryName, 'usb_set_configuration', DevHandle, LIBUSB_Configuration); 
     1185    displayLibusbStatus(ret, hIn); 
    11751186    if (ret < 0) 
    11761187        ErrorWhileOpening = true; 
     
    11811192    % perl, they do it the same way) 
    11821193    %calllib('libusb', 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); 
    1183     ret = calllib('libusb0', 'usb_claim_interface', DevHandle, LIBUSB_Interface); 
    1184     displayLibusbStatus(ret); 
     1194    ret = calllib(hIn.ConnectionLibraryName, 'usb_claim_interface', DevHandle, LIBUSB_Interface); 
     1195    displayLibusbStatus(ret, hIn); 
    11851196    if (ret < 0) 
    11861197        ErrorWhileOpening = true; 
     
    11971208    % disp(sprintf('usb_strerror: %s', calllib('libusb', 'usb_strerror'))) 
    11981209 
    1199 %     % is this necessary? but found it in perl and python versions... 
    1200 %     % the main point: never touch a running system 
    1201 %     textOut(sprintf('    . resetting device... ')); 
    1202 %     ret = calllib('libusb0', 'usb_reset', DevHandle); 
    1203 %     displayLibusbStatus(ret); 
    1204 %     if (ret < 0) 
    1205 %         ErrorWhileOpening = true; 
    1206 %     end%if 
    1207      
    1208      
    1209     %   // Discard any data that is left in the buffer 
    1210     %   while (usb_bulk_read(nxt->hdl, 0x82, buf, sizeof(buf), 1) > 0) 
    1211     %     ; 
    1212     disp('    . discarding any data') 
    1213     buffer = char(uint8(blanks(64))); 
    1214     buffer = uint8(zeros(64,1)); 
    1215     ret = 1; 
    1216     while ret > 0 
    1217        ret = calllib('libusb0', 'usb_bulk_read', DevHandle, uint8(130), buffer, 5, 1); 
    1218     end 
    1219  
    1220     %   // try to set the stream I/O feature 
    1221     %   ret = usb_control_msg(nxt->hdl, 0x41, 0x3, 0, 0, NULL, 0, 1000); 
    1222     %   if (ret >= 0) 
    1223     %   { 
    1224     %     nxt->stream_mode = 1; 
    1225     %   } 
    1226     disp('    . open stream modus') 
    1227     buffer = char(uint8(blanks(64))); 
    1228     ret = calllib('libusb0', 'usb_control_msg', DevHandle, 65, 3, 0, 0, buffer, 0, 1000); 
    1229     if ret < 0 
    1230       msg = ['Libusb error ' num2str(ret) ' while open stream mode: ' getLibusbErrorString(ret)]; 
    1231       warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileOpenStreamMode', msg); 
    1232     end%if 
    1233      
     1210    if hIn.OSValue == 2 % linux 
     1211        % is this necessary? but found it in perl and python versions... 
     1212        % the main point: never touch a running system 
     1213        textOut(sprintf('    . resetting device... ')); 
     1214        ret = calllib(hIn.ConnectionLibraryName, 'usb_reset', DevHandle); 
     1215        displayLibusbStatus(ret, hIn); 
     1216        if (ret < 0) 
     1217            ErrorWhileOpening = true; 
     1218        end%if 
     1219    end % end if linux 
     1220     
     1221 
     1222    if hIn.OSValue == 1 % windows 
     1223%         NOTE: really necessary? 
     1224%         %   // Discard any data that is left in the buffer 
     1225%         %   while (usb_bulk_read(nxt->hdl, 0x82, buf, sizeof(buf), 1) > 0); 
     1226%         textOut(sprintf('    . discarding any data... ')); 
     1227%         buffer = uint8(zeros(64,1)); 
     1228%         ret = 1; 
     1229%         while ret > 0 
     1230%            ret = calllib(hIn.ConnectionLibraryName, 'usb_bulk_read', DevHandle, uint8(130), buffer, 5, 1); 
     1231%            displayLibusbStatus(ret, hIn); 
     1232%         end 
     1233% %         displayLibusbStatus(ret, hIn); 
     1234 
     1235        %   // try to set the stream I/O feature 
     1236        %   ret = usb_control_msg(nxt->hdl, 0x41, 0x3, 0, 0, NULL, 0, 1000); 
     1237        %   if (ret >= 0) 
     1238        %   { 
     1239        %     nxt->stream_mode = 1; 
     1240        %   } 
     1241        textOut(sprintf('    . open stream modus... ')); 
     1242        buffer = char(uint8(blanks(64))); 
     1243        ret = calllib(hIn.ConnectionLibraryName, 'usb_control_msg', DevHandle, 65, 3, 0, 0, buffer, 0, 1000); 
     1244        displayLibusbStatus(ret, hIn); 
     1245        if (ret < 0) 
     1246            ErrorWhileOpening = true; 
     1247        end%if 
     1248%         if ret < 0 
     1249%           msg = ['Libusb error ' num2str(ret) ' while open stream mode: ' getLibusbErrorString(ret, hIn)]; 
     1250%           warning('MATLAB:RWTHMindstormsNXT:USB:libusbErrorWhileOpenStreamMode', msg); 
     1251%         end%if 
     1252    end % end if windows 
    12341253     
    12351254     
     
    12381257        if ~SuppressErrors 
    12391258            errordlg('Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') 
    1240             error('MATLAB:RWTHMindstormsNXT:USB:Linux:couldNotOpenNXT', 'Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') 
     1259            error('MATLAB:RWTHMindstormsNXT:USB:couldNotOpenNXT', 'Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') 
    12411260        else 
    12421261            % again, exit silently when no success 
  • branches/behrens/mfiles/COM_SendPacket.m

    r232 r234  
    3232% 
    3333% Signature 
    34 %   Author: Linus Atorf (see AUTHORS) 
    35 %   Date: 2008/07/09 
     34%   Author: Linus Atorf, Alexander Behrens (see AUTHORS) 
     35%   Date: 2008/08/15 
    3636%   Copyright: 2007-2008, RWTH Aachen University 
    3737% 
     
    8282            end 
    8383        else % Linux 
    84             USB_SendPacket_Linux(Packet(3:end), handle); 
     84            USB_SendPacket_libusb(Packet(3:end), handle); 
    8585        end%if 
    8686         
     
    217217     
    218218    if ret < 0 
    219         msg = ['Libusb error ' num2str(ret) ' while sending data: ' getLibusbErrorString(ret)]; 
    220         warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileSendingData', msg); 
     219        msg = ['Libusb error ' num2str(ret) ' while sending data: ' getLibusbErrorString(ret, h)]; 
     220        warning('MATLAB:RWTHMindstormsNXT:USB:libusbErrorWhileSendingData', msg); 
    221221    else % success 
    222222        h.PacketsSent(1); 
     
    242242     
    243243    if ret < 0 
    244         msg = ['Libusb error ' num2str(ret) ' while sending data: ' getLibusbErrorString(ret)]; 
     244        msg = ['Libusb error ' num2str(ret) ' while sending data: ' getLibusbErrorString(ret, h)]; 
    245245        warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileSendingData', msg); 
    246246    else % success 
  • branches/behrens/mfiles/private/getLibusbErrorString.m

    r232 r234  
    1 function msg = getLibusbErrorString(errNo) 
     1function msg = getLibusbErrorString(errNo, h) 
    22% Returns description to the last error from libusb 
    33% 
    44% Syntax 
    5 %   |msg = getLibusbErrorString(errNo)| 
     5%   |msg = getLibusbErrorString(errNo, h)| 
    66% 
    77% Description 
     
    3737    if  errNo < 0 
    3838        %msg = sprintf('Error %d in libusb: %s', errNo, calllib('libusb', 'usb_strerror')); 
    39         msg =  calllib('libusb0', 'usb_strerror'); 
     39        msg =  calllib(h.ConnectionLibraryName, 'usb_strerror'); 
    4040 
    4141    % basically taht was it, leave the other strings below just in case... 
  • branches/behrens/mfiles/private/libusb_win_proto.m

    r231 r234  
    1 function [methodinfo,structs,enuminfo]=libusb_win_proto; 
     1function [methodinfo,structs,enuminfo]=libusb_win_proto 
     2% Prototype file for library "libusb0" (used in Indows USB communication) 
     3% 
     4% Syntax 
     5%   [methodinfo,structs,enuminfo]=libusb_win_proto 
     6% 
     7% Description 
     8%   This function was generated by loadlibrary.m 
     9% 
     10%   It is based on the libusb0 header-file usb.h, whose method 
     11%   usb_bulk_read ans usb_bulk_write were modified in order to avoid returning zero-terminated 
     12%   strings (which get truncated by MATLAB with binary data). 
     13% 
     14% Signature 
     15%   Author: Linus Atorf, Alexander Behrens (see AUTHORS) 
     16%   Date: 2008/08/15 
     17%   Copyright: 2007-2008, RWTH Aachen University 
     18; 
     19% 
     20% *********************************************************************************************** 
     21% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    * 
     22% *                                                                                             * 
     23% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  * 
     24% *  it under the terms of the GNU General Public License as published by the Free Software     * 
     25% *  Foundation, either version 3 of the License, or (at your option) any later version.        * 
     26% *                                                                                             * 
     27% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       * 
     28% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  * 
     29% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             * 
     30% *                                                                                             * 
     31% *  You should have received a copy of the GNU General Public License along with the           * 
     32% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 * 
     33% ********************************************************************************************* 
     34% ** 
     35 
    236%LIBUSB_WIN_PROTO Create structures to define interfaces found in 'usb'. 
    337