Changeset 234
- Timestamp:
- 08/16/08 00:05:29 (5 years ago)
- Location:
- branches/behrens/mfiles
- Files:
-
- 6 modified
-
COM_CloseNXT.m (modified) (9 diffs)
-
COM_CollectPacket.m (modified) (3 diffs)
-
COM_OpenNXTEx.m (modified) (24 diffs)
-
COM_SendPacket.m (modified) (4 diffs)
-
private/getLibusbErrorString.m (modified) (2 diffs)
-
private/libusb_win_proto.m (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/behrens/mfiles/COM_CloseNXT.m
r232 r234 63 63 % 64 64 % Signature 65 % Author: Linus Atorf (see AUTHORS)66 % Date: 2008/0 7/0965 % Author: Linus Atorf, Alexander Behrens (see AUTHORS) 66 % Date: 2008/08/15 67 67 % Copyright: 2007-2008, RWTH Aachen University 68 68 % … … 142 142 USB_CloseHandle_Windows(h); 143 143 case 2 % libusb library 144 USB_CloseHandle_ Windows_libusb(h);144 USB_CloseHandle_libusb(h); 145 145 otherwise 146 146 end 147 147 else % linux 148 USB_CloseHandle_ Linux(h);148 USB_CloseHandle_libusb(h); 149 149 end%if 150 150 else % BT … … 367 367 368 368 369 %% --- FUNCTION USB_CloseHandle_libusb 370 function 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 412 end%function 413 414 369 415 %% --- FUNCTION USB_CloseHandle_Windows_libusb 370 416 function USB_CloseHandle_Windows_libusb(h) … … 378 424 textOut(sprintf(' - Sending a zero length packet... ')); 379 425 ret = calllib('libusb0', 'usb_bulk_read', h.Handle, 1, buffer, LIBUSB_Interface, 1000); 380 displayLibusbStatus(ret );426 displayLibusbStatus(ret, h); 381 427 382 428 % // Turn off stream mode … … 385 431 textOut(sprintf(' - Turn off the stream mode... ')); 386 432 ret = calllib('libusb0', 'usb_control_msg', h.Handle, 65, 1, 0, 0, buffer, LIBUSB_Interface, 5000); 387 displayLibusbStatus(ret );433 displayLibusbStatus(ret, h); 388 434 389 435 % // Discard any data that is left in the buffer … … 400 446 textOut(sprintf(' - Releasing interface... ')); 401 447 ret = calllib('libusb0', 'usb_release_interface', h.Handle, LIBUSB_Interface); 402 displayLibusbStatus(ret );448 displayLibusbStatus(ret, h); 403 449 404 450 405 451 textOut(sprintf(' - Closing device... ')); 406 452 ret = calllib('libusb0', 'usb_close', h.Handle); 407 displayLibusbStatus(ret );453 displayLibusbStatus(ret, h); 408 454 409 455 end%function … … 418 464 textOut(sprintf(' - Releasing interface... ')); 419 465 ret = calllib('libusb', 'usb_release_interface', h.Handle, LIBUSB_Interface); 420 displayLibusbStatus(ret );466 displayLibusbStatus(ret, h); 421 467 422 468 423 469 textOut(sprintf(' - Closing device... ')); 424 470 ret = calllib('libusb', 'usb_close', h.Handle); 425 displayLibusbStatus(ret );471 displayLibusbStatus(ret, h); 426 472 427 473 % we don't unload libusb now, but if we did: … … 435 481 436 482 437 %% --- FUNCTION displayUSBWinStatus (status)483 %% --- FUNCTION displayUSBWinStatus 438 484 function displayUSBWinStatus(status) 439 485 if status … … 446 492 447 493 448 %% --- FUNCTION displayLibusbStatus (status)449 function displayLibusbStatus(status )494 %% --- FUNCTION displayLibusbStatus 495 function displayLibusbStatus(status, h) 450 496 if isnumeric(status) && (status < 0) 451 497 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'])) 453 499 else 454 500 textOut(sprintf('done.\n')); -
branches/behrens/mfiles/COM_CollectPacket.m
r232 r234 101 101 end 102 102 else % Linux 103 [type cmd statusbyte content] = USB_CollectPacket_ Linux(handle);103 [type cmd statusbyte content] = USB_CollectPacket_libusb(handle); 104 104 end%if 105 105 else % BT … … 436 436 [status something reply] = calllib(h.ConnectionLibraryName, 'usb_bulk_read', h.Handle, receivingEndpoint, buffer, length(buffer), timeout); 437 437 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)]; 439 439 warning('MATLAB:RWTHMindstormsNXT:USB:libusbErrorWhileReceivingData', msg); 440 440 end%if … … 514 514 [status something reply] = calllib('libusb', 'usb_bulk_read', h.Handle, receivingEndpoint, buffer, length(buffer), timeout); 515 515 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)]; 517 517 warning('MATLAB:RWTHMindstormsNXT:USB:Windows:libusbErrorWhileReceivingData', msg); 518 518 end%if -
branches/behrens/mfiles/COM_OpenNXTEx.m
r233 r234 245 245 % handle.IniFilename = h.IniFilename; 246 246 % 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; 250 250 % 251 % handle.SendSendPause = h.SendSendPause;251 % handle.SendSendPause = h.SendSendPause; 252 252 % handle.SendReceivePause = h.SendReceivePause; 253 253 % 254 254 % handle.NXTMAC = h.NXTMAC; 255 % handle.CreationTime = h.CreationTime;255 % handle.CreationTime = h.CreationTime; 256 256 % handle.Index = h.Index; 257 257 … … 391 391 392 392 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 400 410 % hOut = USB_OpenHandle_Windows(hIn, SuppressErrors); 401 hOut = USB_OpenHandle_Windows_libusb(hIn, false);411 % hOut = USB_OpenHandle_libusb(hIn, false); 402 412 elseif hIn.OSValue == 2 403 hOut = USB_OpenHandle_ Linux(hIn, SuppressErrors);413 hOut = USB_OpenHandle_libusb(hIn, SuppressErrors); 404 414 end%if 405 415 … … 579 589 580 590 581 %% --- FUNCTION displayLibusbStatus (status)591 %% --- FUNCTION displayLibusbStatus 582 592 % little convenient helper to save some lines of code with textOut 583 function displayLibusbStatus(status )593 function displayLibusbStatus(status, h) 584 594 % little convenient helper to save some lines of code with textOut 585 595 if isnumeric(status) && (status < 0) 586 596 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'])) 588 598 else 589 599 textOut(sprintf('done.\n')); … … 669 679 textOut(sprintf(' . finding busses... ')) 670 680 ret = calllib('libusb', 'usb_find_busses'); 671 displayLibusbStatus(ret );681 displayLibusbStatus(ret, hIn); 672 682 673 683 textOut(sprintf(' . finding devices... ')) 674 684 ret = calllib('libusb', 'usb_find_devices'); 675 displayLibusbStatus(ret );685 displayLibusbStatus(ret, hIn); 676 686 677 687 %% Get main root bus … … 680 690 p = calllib('libusb', 'usb_get_busses'); 681 691 bus = libstruct('usb_bus', p); 682 displayLibusbStatus(p );692 displayLibusbStatus(p, hIn); 683 693 684 694 … … 759 769 textOut(sprintf(' . opening device... ')); 760 770 DevHandle = calllib('libusb', 'usb_open', dev); 761 displayLibusbStatus(DevHandle );771 displayLibusbStatus(DevHandle, hIn); 762 772 763 773 textOut(sprintf(' . reading serial number... ')); … … 766 776 buffer = blanks(255); 767 777 [bytesRead newHandleOrWhat SerialNo] = calllib('libusb', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber, buffer, length(buffer)); 768 displayLibusbStatus(bytesRead );778 displayLibusbStatus(bytesRead, hIn); 769 779 770 780 SerialNo = strtrim(SerialNo); % just to be safe … … 782 792 textOut(sprintf(' . closing device... ')); 783 793 status = calllib('libusb', 'usb_close', DevHandle); 784 displayLibusbStatus(status );794 displayLibusbStatus(status, hIn); 785 795 end%if 786 796 … … 849 859 %calllib('libusb', 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); 850 860 ret = calllib('libusb', 'usb_set_configuration', DevHandle, LIBUSB_Configuration); 851 displayLibusbStatus(ret );861 displayLibusbStatus(ret, hIn); 852 862 if (ret < 0) 853 863 ErrorWhileOpening = true; … … 859 869 %calllib('libusb', 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); 860 870 ret = calllib('libusb', 'usb_claim_interface', DevHandle, LIBUSB_Interface); 861 displayLibusbStatus(ret );871 displayLibusbStatus(ret, hIn); 862 872 if (ret < 0) 863 873 ErrorWhileOpening = true; … … 878 888 textOut(sprintf(' . resetting device... ')); 879 889 ret = calllib('libusb', 'usb_reset', DevHandle); 880 displayLibusbStatus(ret );890 displayLibusbStatus(ret, hIn); 881 891 if (ret < 0) 882 892 ErrorWhileOpening = true; … … 912 922 913 923 914 %% --- FUNCTION USB_OpenHandle_ Linux915 function hOut = USB_OpenHandle_ Windows_libusb(hIn, SuppressErrors)924 %% --- FUNCTION USB_OpenHandle_libusb 925 function hOut = USB_OpenHandle_libusb(hIn, SuppressErrors) 916 926 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 917 927 % Temporary USB construction site for the RWTH - Mindstorms NXT Toolbox 918 928 % http://www.mindstorms.rwth-aachen.de 919 929 % 920 % Alexander Behrens, 15.08.2008930 % Linus Atorf, Alexander Behrens, 15.08.2008 921 931 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 922 932 … … 953 963 954 964 %% 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)); 957 967 try 958 968 % use "our" wrapper file... 959 loadlibrary( 'libusb0', @libusb_win_proto)969 loadlibrary(hIn.ConnectionLibraryName, @libusb_win_proto) 960 970 textOut(sprintf('done.\n')); 961 971 catch 962 972 textOut(sprintf('failed.\n')); 963 973 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!') 965 975 else 966 976 return … … 968 978 end%try 969 979 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 975 982 976 983 textOut(sprintf(' - Initializing and browsing USB busses\n')); 977 984 978 985 %% 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'); 981 988 982 989 % > - insert usb_set_debug(255) in pyusb.c right behind usb_init() … … 991 998 % devices since last call, don't neet it... 992 999 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); 995 1002 996 1003 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); 999 1006 1000 1007 %% Get main root bus 1001 1008 1002 1009 textOut(sprintf(' . getting root bus object... ')) 1003 p = calllib( 'libusb0', 'usb_get_busses');1010 p = calllib(hIn.ConnectionLibraryName, 'usb_get_busses'); 1004 1011 bus = libstruct('usb_bus', p); 1005 displayLibusbStatus(p );1012 displayLibusbStatus(p, hIn); 1006 1013 1007 1014 … … 1044 1051 % It's debug-info only anyway 1045 1052 1046 % open device to get a handle1047 DevHandle = calllib('libusb0', 'usb_open', dev);1048 1049 % now the string stuff1050 buffer = blanks(255);1051 % we don't need a real buffer or pointer, matlab seems to do this1052 % for us, so we pass that buffer variable, without really needing1053 % it. it seems like matlab "knows" how strings get written by1054 % reference and returns the new value from the function. if you1055 % compare the matlab-returnvalues of usb_get_string_simple using1056 % libfunctionsview libusb, you'll find that they don't match whats1057 % written inside usb.h. very nice and handy, thank you matlab :-)1058 %pBuffer = libpointer('cstring', buffer); not needed, see above1059 1053 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 1073 1084 1074 1085 … … 1081 1092 1082 1093 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); 1085 1096 1086 1097 textOut(sprintf(' . reading serial number... ')); … … 1088 1099 % int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); 1089 1100 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); 1092 1103 1093 1104 SerialNo = strtrim(SerialNo); % just to be safe … … 1104 1115 % don't forget to close the opened NXT that didnt match... 1105 1116 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); 1108 1119 end%if 1109 1120 … … 1148 1159 if ~SuppressErrors 1149 1160 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!') 1151 1162 else 1152 1163 % well, no NXT, exit silently … … 1171 1182 % somehow this doesn't work, so we use the hardcoded configuration 1! 1172 1183 %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); 1175 1186 if (ret < 0) 1176 1187 ErrorWhileOpening = true; … … 1181 1192 % perl, they do it the same way) 1182 1193 %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); 1185 1196 if (ret < 0) 1186 1197 ErrorWhileOpening = true; … … 1197 1208 % disp(sprintf('usb_strerror: %s', calllib('libusb', 'usb_strerror'))) 1198 1209 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 1234 1253 1235 1254 … … 1238 1257 if ~SuppressErrors 1239 1258 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'')!') 1241 1260 else 1242 1261 % again, exit silently when no success -
branches/behrens/mfiles/COM_SendPacket.m
r232 r234 32 32 % 33 33 % Signature 34 % Author: Linus Atorf (see AUTHORS)35 % Date: 2008/0 7/0934 % Author: Linus Atorf, Alexander Behrens (see AUTHORS) 35 % Date: 2008/08/15 36 36 % Copyright: 2007-2008, RWTH Aachen University 37 37 % … … 82 82 end 83 83 else % Linux 84 USB_SendPacket_ Linux(Packet(3:end), handle);84 USB_SendPacket_libusb(Packet(3:end), handle); 85 85 end%if 86 86 … … 217 217 218 218 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); 221 221 else % success 222 222 h.PacketsSent(1); … … 242 242 243 243 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)]; 245 245 warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileSendingData', msg); 246 246 else % success -
branches/behrens/mfiles/private/getLibusbErrorString.m
r232 r234 1 function msg = getLibusbErrorString(errNo )1 function msg = getLibusbErrorString(errNo, h) 2 2 % Returns description to the last error from libusb 3 3 % 4 4 % Syntax 5 % |msg = getLibusbErrorString(errNo )|5 % |msg = getLibusbErrorString(errNo, h)| 6 6 % 7 7 % Description … … 37 37 if errNo < 0 38 38 %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'); 40 40 41 41 % 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; 1 function [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 2 36 %LIBUSB_WIN_PROTO Create structures to define interfaces found in 'usb'. 3 37

