Author Topic: Create Virtual COM Port  (Read 15382 times)

ami

  • Guest
Create Virtual COM Port
« on: November 17, 2010, 05:15:37 PM »
Hi Mike,

Here's the issue i'm trying to solve.

I'm creating a tool to be used on our production floor. I have a legacy piece of code that uses a COM Port to talk a device. I'm currently using the WCL and the Microsoft BT stack. I'm using the wclAPI, wclBluetoothDiscover and wclVirtualCOMPort components.

I scan for a device using a TwclBluetoothDiscover. I place the address and the and the friendly name in a listbox similar to the demo apps. 

I then have a connect button that creates a thread to do the following:

1. Closes the wclVirtualCOMPort Component, to destroy any old connections
2. Gets the pin from the friendly name
3. Pairs the device if necessary
4. Assigns the device address, radio, Service class
5. Opens the virtual port.

I then return the resulting COMPort number/handle to the legacy section of the code where i assign it to the 3rd party serial port component and flag the user that the connection has been established.


Here's a snippet of the code:
Code: [Select]
function TConnectThread.SetupBluetoothConnection( ): Integer;
var
  Device :TwclBluetoothDevice;
  radio :TwclBluetoothRadio;
  Name: WideString;
  pin: string;
  isPaired : boolean;
  j: Integer;
  res : Integer;
begin
   Result := -1;
  radio := TwclBluetoothRadio.Create;
  try
    radio.API := baMicrosoft;

    Device := TwclBluetoothDevice.Create;

    try
      if ( _activeAddress <> '') then
      begin

        _criticalSection.Enter;
        try
          _virtualPort.Close();
        finally
          _criticalSection.Leave;
        end;

        // DEBUG
        //sleep(100);

        Device.Address := _activeAddress;

        // Reconmended in examples
        for j := 0 to 2 do
        begin
          res := device.GetName(radio, Name);
          if (Name <> '') or (Res <> WCL_E_SUCCESS) then Break;
        end;

        if ( pos( 'VR4U', Name) = 0 ) then
          raise Exception.Create('Unable to get Device Name.');

        pin := GetPinFromName( Name );

        if ( device.GetPaired( radio, isPaired ) <> WCL_E_SUCCESS ) then
          raise Exception.Create('Unabled to communicate with device');

        { TODO: might want to always unpair then re pair
        if ( (isPaired) and  ( device.Unpair( radio ) <> WCL_E_SUCCESS )) then
          raise Exception.Create('Unable to unpair device.');
        }
       
        if  (not isPaired) and (Device.Pair( radio, pin ) <> WCL_E_SUCCESS ) then
            raise Exception.Create('Unable to pair device.');

        _criticalSection.Enter;
        try
          _virtualPort.Address := _activeAddress;
          _virtualPort.Radio := radio;
          _virtualPort.Service := SerialPortServiceClass_UUID;
          wclShowError(_virtualPort.Open);
        finally
          _criticalSection.Leave;
        end;

      end;

      if not _virtualPort.Active then
      begin
        _exceptionMessage := 'COM Port was not created';
      end
      else
      begin
        Result := _virtualPort.Port
      end;

    finally
      Device.Free;
    end;
  finally
    radio.Free;
  end;
end;


Ideally i'd like to create and destroy the virtual port each time a user tries to establish a new connection. And the above seemed to work okay on my development machine.

Given the parameters, Is this a decent way to go about this?

When i tried the application on the target machine, things kinda went haywire. It seems like the _virtualPort.Open(), returns immediately saying it has setup the port, but infact it hasn't.
 



 

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #1 on: November 17, 2010, 05:24:15 PM »
First at all: you must not use any COM ports for Bluetooth. You should use wclClient instead. We do not guarantee work of TwclVirtualCOMPort creator as it was designed for just 1 our customer and as it works good for he we will not fix any issue/bug it in.

With regards to your question: I do not know what BT driver runs on your other PC but I can gueass that it is not MS as you got thread issue. You must have message loop in your thread to get WCL working in threads.

And I must repeat: use wclClient

ami

  • Guest
Re: Create Virtual COM Port
« Reply #2 on: November 17, 2010, 07:52:20 PM »
Thanks for the response Mike.

I had a look at wclClient, how do i create the virtual port with it?

Right now, the TwclVirtualComport creates it for me and returns the port number.

Just so we're on the same page, i have a legacy piece of code that needs to communicate through a serial port.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #3 on: November 17, 2010, 07:54:47 PM »
WCL connects directly to a Bluetooth device without anyh COM ports. It is wrong way to work with WCL if you are using vCOMs.

ami

  • Guest
Re: Create Virtual COM Port
« Reply #4 on: November 17, 2010, 08:04:38 PM »
So wclClient cannot create the port?


ami

  • Guest
Re: Create Virtual COM Port
« Reply #5 on: November 17, 2010, 08:17:07 PM »
All i'm trying to do here is automate the Virtual Port Project...

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #6 on: November 17, 2010, 08:33:18 PM »
Of course no as no one COM port is required for Bluetooth communication.

It is very bad idea to use vCOMs for Bluetooth communication. Use direct connection with wclClient.

ami

  • Guest
Re: Create Virtual COM Port
« Reply #7 on: November 17, 2010, 08:48:50 PM »
In an ideal world, i would communicate directly. But like i've stated. I have a legacy piece of code that uses a 3rd party data packet component that needs to communicated through a serial port.

Unless i'm missing something, I think i have to go down this virtual COM port road?

Right now, the way it is being used, a user goes into control panel, scans, pairs and creates a virtual port. They then go into the software and open a connection on the created virtual com port.

I need to automate this, and when evaluating the WCL it looked like the Virtual COMPort demo was the exact solution. Is it not?

Is there a way i can automate this procedure without using the wclVirtualCOMPort component and without modifying the legacy code?



Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #8 on: November 17, 2010, 08:54:16 PM »
Well, that's wrong way for BT.

But if you have legacy code then yes, TwclVirtualCOMPort is what you need and what it was designed for (there is no other way to automate it). But I must repate - it is wrong way.

So, for your case with thread.

1. I do recomend to do not use Threads for now
2. If you need it - you must have message loop in your thread.

I think it may be solved much faster if you contact me by MSN/ICQ/Yahoo so I can help better.

ami

  • Guest
Re: Create Virtual COM Port
« Reply #9 on: November 17, 2010, 09:09:56 PM »
Great. Thanks for your help. I will contact you.

Yes, i understand that it isn't ideal and i'm looking at rewriting the communication side of things over the next year. This is mainly a temporary solution. The intended user isn't very technical and sifting around control panel, scanning, manually pairing, noting the port created, has proved to be quite troublesome for the person.



ami

  • Guest
Re: Create Virtual COM Port
« Reply #10 on: November 17, 2010, 10:03:38 PM »
Okay ive, sent you the friend request on messenger. axxxe_cxxxxxxxd@hotmail.com


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #11 on: November 17, 2010, 10:46:54 PM »
Added

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Create Virtual COM Port
« Reply #12 on: November 19, 2010, 11:13:20 PM »
The issue has been fixed. Who has the same issue please contact us - we will send you fixed version. E-mail on support@btframework.com

 

Sitemap 1 2 3 4 5 6 7