Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.pascal.delphi.misc > #572

Detecting Bluetooth COM port(s) in Delphi 4

From "P E Schoen" <paul@peschoen.com>
Newsgroups comp.lang.pascal.delphi.misc
Subject Detecting Bluetooth COM port(s) in Delphi 4
Date 2013-12-29 08:03 -0500
Organization A noiseless patient Spider
Message-ID <l9p6jb$k53$1@dont-email.me> (permalink)

Show all headers | View raw


I haven't been around much lately as my efforts have been elsewhere, but 
recently one of my customers had problems with the USB connection from his 
computer to my Ortmaster device, where he has a 13 kV AC supply operating a 
closing coil on a recloser, and it causes communication to stop, requiring a 
reset of the device and removal/reinsertion of the USB connector. I tried a 
USB isolator but no joy.

So, I thought maybe a direct serial connection would be more robust, and 
since he is using a desktop computer it should be possible, whereas for 
laptops the serial port is a rarity except in the Panasonic "Toughbooks". I 
bought one of the cheap ($8 or so) Bluetooth modules on eBay and I used it 
at its default 9600 baud rate to collect some data, and it seemed to work 
very well, even as far as 30 feet away through two doors, and with the 
device inside a metal enclosure.

For my Ortmaster, I need to use 57.6 kB, and I was able to set the baud rate 
by using a USB-Serial emulator in the form of a Microchip PICdem FSUSB 
board.

I was able to hard code the port number (COM14) into the software and it 
worked OK, but I needed to have it check for the proper port number, as it 
does now for the USB serial ports using the registry. But the Bluetooth 
enumeration is a bit different, and the USB Bluetooth adapter shows up as 
COM13 and COM14. However, only COM14 can be opened and used for 
communication with the remote device. I found a way to do this, and although 
it may not be elegant, it seems to work just fine. It's been a long time 
since I programmed in Delphi so I'm a bit rusty, and there may be better 
ways to do this. But here it is, and hopefully someone may find it useful. I 
still don't know if this will work with my customer's setup, and he is a 
utility in Maine and very busy now dealing with the widespread power outages 
and cold weather problems.

Paul

===================================================================================

function   GetBlutoothComPort: String;
var        S, S1: String;
           Item: Integer;
           Reg: TRegistry;
           KeyList1, KeyList2: TStringList;

begin
  Reg := TRegistry.Create;
  if Reg = nil then begin
    fmDebug.AddDebugItem( 'Error creating Reg ' );
    exit; end;
  KeyList1 := TStringList.Create;
  if KeyList1 = nil then begin
    fmDebug.AddDebugItem( 'Error creating KeyList1 ' );
    exit; end;
  KeyList2 := TStringList.Create;
  if KeyList2 = nil then begin
    fmDebug.AddDebugItem( 'Error creating KeyList2 ' );
    exit; end;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    S := '\SYSTEM\CurrentControlSet\Enum\BTHENUM\';
    fmDebug.AddDebugItem( 'Key: ' + S );
    try
      Reg.OpenKeyReadOnly(S);
      Reg.GetKeyNames(KeyList1);
      For Item := 0 to KeyList1.Count-1 do begin
        S1 := KeyList1.Strings[Item];
        Reg.OpenKeyReadOnly(S + S1);
        Reg.GetKeyNames(KeyList2);
        S1 := S + KeyList1.Strings[Item] + '\' + KeyList2.Strings[0] + 
'\Device Parameters';
        fmDebug.AddDebugItem( 'Key: ' + S1 );
        Reg.OpenKeyReadOnly(S1);
        If Reg.ValueExists('RxFIFO') then begin
           S1 := Reg.ReadString('PortName');
           result := S1;
           fmDebug.AddDebugItem( 'Working Bluetooth Port determined: ' + 
S1 );
           break; end;
        fmDebug.AddDebugItem( 'Bluetooth Port detected: ' + 
Reg.ReadString('PortName') );
        end;
     except
        fmDebug.AddDebugItem( 'Error Bluetooth' );
    end;
  finally
    Reg.Free;
    KeyList1.Free;
    KeyList2.Free;
  end;
end; 

Back to comp.lang.pascal.delphi.misc | Previous | NextNext in thread | Find similar


Thread

Detecting Bluetooth COM port(s) in Delphi 4 "P E Schoen" <paul@peschoen.com> - 2013-12-29 08:03 -0500
  Re: Detecting Bluetooth COM port(s) in Delphi 4 Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2013-12-29 16:45 +0100
    Re: Detecting Bluetooth COM port(s) in Delphi 4 "P E Schoen" <paul@peschoen.com> - 2013-12-29 13:24 -0500

csiph-web