Changeset 641
- Timestamp:
- 07/03/09 11:30:08 (4 years ago)
- Location:
- trunk/mfiles
- Files:
-
- 2 modified
-
COM_MakeBTConfigFile.m (modified) (4 diffs)
-
COM_OpenNXTEx.m (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/mfiles/COM_MakeBTConfigFile.m
r414 r641 59 59 prompt = {'Filename:', 'SerialPort:', 'BaudRate:', 'DataBits:', 'SendPause:', 'ReceivePause:', 'Timeout:'}; 60 60 else 61 prompt = {'Filename:', ' NXT Name:', 'or NXT MAC:', 'orSerialPort:', 'SendPause:', 'ReceivePause:', 'Timeout:'};61 prompt = {'Filename:', 'SerialPort:', 'SendPause:', 'ReceivePause:', 'Timeout:'}; 62 62 end; 63 63 num_lines = 1; … … 68 68 default_parameters = {'bluetooth.ini','COM3','9600', '8', '5', '30', '2'}; 69 69 else 70 default_parameters = {'bluetooth.ini', '','','/dev/rfcomm0', '5', '30', '2'};70 default_parameters = {'bluetooth.ini', '/dev/rfcomm0', '5', '30', '2'}; 71 71 end; 72 72 … … 79 79 baudrate = str2double(my_parameters{3}); 80 80 databits = str2double(my_parameters{4}); 81 sendpause = str2double(my_parameters{5}); 82 receivepause = str2double(my_parameters{6}); 83 timeout = str2double(my_parameters{7}); 81 84 else 82 name = my_parameters{2}; 83 mac = my_parameters{3}; 84 port = my_parameters{4}; 85 port = my_parameters{2}; 86 sendpause = str2double(my_parameters{3}); 87 receivepause = str2double(my_parameters{4}); 88 timeout = str2double(my_parameters{5}); 85 89 end; 86 sendpause = str2double(my_parameters{5});87 receivepause = str2double(my_parameters{6});88 timeout = str2double(my_parameters{7});89 90 90 91 … … 105 106 fwrite(h_file, sprintf('\r\n')); 106 107 108 fwrite(h_file, sprintf('SerialPort=%s\r\n', port)); 107 109 if ispc 108 fwrite(h_file, sprintf('SerialPort=%s\r\n', port));109 110 fwrite(h_file, sprintf('BaudRate=%d\r\n', baudrate)); 110 111 fwrite(h_file, sprintf('DataBits=%d\r\n', databits)); 111 else112 fwrite(h_file, sprintf('NXTName=%s\r\n', name));113 fwrite(h_file, sprintf('NXTMAC=%s\r\n', mac));114 fwrite(h_file, sprintf('SerialPort=%s\r\n', port));115 112 end; 116 113 fwrite(h_file, sprintf('\r\n')); -
trunk/mfiles/COM_OpenNXTEx.m
r438 r641 314 314 else % LINUX file handle version 315 315 316 NXTName = readFromIniFile( inisection, 'NXTName', inifilename ); 317 if isempty( NXTName ) 318 NXTMAC = readFromIniFile( inisection, 'NXTMAC', inifilename ); 319 else 320 NXTMAC = BT_Linux_ResolveName( NXTName ); 321 end; 322 if isempty( NXTMAC ) 323 ComPort = readFromIniFile( inisection, 'SerialPort', inifilename); 324 else 325 ComPort = BT_Linux_BindPort( NXTMAC ); 326 end; 316 ComPort = readFromIniFile( inisection, 'SerialPort', inifilename); 327 317 328 318 % on linux, there is no baudrate etc to set, so we ignore these ini … … 365 355 handle = fopen( ComPort, 'r+' ); 366 356 if handle == -1 367 % the creation of the device node after BindPort may take some time368 pause( .5 );369 handle = fopen( ComPort, 'r+' );370 end;371 if handle == -1372 357 error( 'MATLAB:RWTHMindstormsNXT:Bluetooth:couldNotOpenConnection', 'Could not open bluetooth connection using port %s.', ComPort ); 373 358 end; 374 % make sure device is in raw mode - if this fails, let's hope for the best 359 % Try to make sure serial port is in raw mode. This is just a safety measure 360 % and probably not needed if btconnect is used. However, it doesn't hurt. 375 361 unix( [ 'stty -F ' ComPort ' sane raw -echo -iexten' ] ); 376 362 end … … 957 943 958 944 959 %% --- FUNCTION BT_Linux_ResolveName960 function [ mac ] = BT_Linux_ResolveName( name )961 962 [ s, r ] = unix( 'hcitool scan' );963 if s964 error( 'MATLAB:RWTHMindstormsNXT:Bluetooth:Linux:scanFailed', 'hcitool command failed. no bluetooth hardware/software installed?' );965 end;966 967 scan_results = regexp( r, [ '^' 9 '(?<mac>\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)' 9 '(?<name>.+)$' ], ...968 'names', 'dotexceptnewline', 'lineanchors' );969 970 r = strcmp( name, { scan_results.name } );971 if sum( r ) ~= 1972 error( 'MATLAB:RWTHMindstormsNXT:Bluetooth:Linux:NxtNotFound', [ '"' name '" not found or not unique. Try specifying MAC address.' ] );973 end;974 mac = scan_results( r ).mac;975 976 end977 978 979 %% --- FUNCTION BT_Linux_BindPort980 function [ device ] = BT_Linux_BindPort( mac )981 982 % get list of currently bound ports983 [ s, r ] = unix( 'rfcomm' );984 if s985 error( 'MATLAB:RWTHMindstormsNXT:Bluetooth:Linux:RfCommFailed', 'rfcomm command failed (bluetooth tools not installed?)' );986 end;987 988 bound_ports = regexp( r, '^rfcomm(?<port>\d+): (?<mac>\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)', ...989 'names', 'dotexceptnewline', 'lineanchors' );990 991 r = strcmp( mac, { bound_ports.mac } );992 993 if sum( r ) > 0994 995 % MAC address already bound to996 port = bound_ports( find( r, 1 ) ).port;997 998 else999 1000 % find first free port and bind to it1001 port = int2str( find( accumarray( reshape( str2double( { bound_ports.port } ) + 1, [], 1 ), 1 ) == 0, 1 ) - 1 );1002 if isempty( port )1003 port = int2str( max( str2double( { bound_ports.port } ) ) + 1 );1004 end;1005 if isempty( port )1006 port = '0';1007 end;1008 1009 [ s, r ] = unix( [ 'sudo rfcomm -r bind ' port ' ' mac ] );1010 if s || numel( r )1011 error( 'MATLAB:RWTHMindstormsNXT:Bluetooth:Linux:sudoFailed', 'sudo rfcomm failed (rfcomm.conf or sudo not setup properly?)' );1012 end;1013 1014 end;1015 1016 device = [ '/dev/rfcomm' port ];1017 1018 end1019 1020 1021 945 %% --- FUNCTION StartMotorControl 1022 946 function h_out = StartMotorControl(progname, h_in)
