Recent

Author Topic: LazSerial: List of USB ports and virtual port issue  (Read 5732 times)

dseligo

  • Sr. Member
  • ****
  • Posts: 343
Re: LazSerial: List of USB ports and virtual port issue
« Reply #15 on: July 14, 2021, 02:28:34 pm »
Hi
You are so right ;) No serial port but I want to use USB with adapter for that.
By the way serial ports with 9-pins or 32-pins are rare on PC's these days.
I need to do a serial protocol called OSM6 on top of a RS-485 hardware setup.

Buttomline is that I want my users via a combox to select the usb ports avaiable on the computer.

Can you be more specific how do you plan to connect two PCs? Do you already have hardware or are you in planning phase? What hardware is that?
AFAIK, you can't just connect two computers via USB - you need some 'adapter' in between.

MoellerCLaus

  • Full Member
  • ***
  • Posts: 110
    • Vig Foreningsprogram
Re: LazSerial: List of USB ports and virtual port issue
« Reply #16 on: July 14, 2021, 02:51:38 pm »
Hi
I need to test my implementation of the protocol. Therefore it could be could to have virtual ports on one PC. One application sending another receiving. This can now be done with COM0COM approach.

Hardware: there is a USB to RS-485 adapter between PC an the box with 9-pin RS-485 interface.

 :) I hope I should be well off with this approach.

I struckled until a minute ago to find about which USB-devices on the PC is doing what.
Code: Pascal  [Select][+][-]
  1.  
  2. // Uses Utilwmi
  3. procedure TFormSerialTest.Button1Click(Sender: TObject);
  4. var
  5.   WMIResult         : TFPObjectList;
  6.   i                 : Integer;
  7.   PropNamesIndex    : Integer;
  8.   PropNames         : Array[0..2] of String =(
  9.   'DeviceId','PNPDeviceID','Description');
  10.  
  11. begin
  12.   Memo1.Clear;
  13.   WMIResult := GetWMIInfo('Win32_USBHub', PropNames,'');
  14.   for i := 0 to Pred(WMIResult.Count) do
  15.   begin
  16.     Memo1.Append('=================');
  17.     for PropNamesIndex := Low(PropNames) to High(PropNames) do
  18.     begin
  19.       Memo1.Append(TStringList(WMIResult[i]).Names[PropNamesIndex] + ' : ' +
  20.               TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex]);
  21.     end;
  22.   end;
  23.   // Clean up
  24.   WMIResult.Free;
  25. end;
  26.  
  27.  

Here the field description gives the ansver.

y.ivanov

  • Full Member
  • ***
  • Posts: 229
Re: LazSerial: List of USB ports and virtual port issue
« Reply #17 on: July 14, 2021, 03:09:12 pm »
Hi
You are so right ;) No serial port but I want to use USB with adapter for that.
By the way serial ports with 9-pins or 32-pins are rare on PC's these days.
I need to do a serial protocol called OSM6 on top of a RS-485 hardware setup.

Buttomline is that I want my users via a combox to select the usb ports avaiable on the computer.
As far as I understand you want to connect 2 PCs with USB/RS-232 dongles. In that case you don't need USB ports scanned, you'll need to list the available serial ports.
The common case with the contemporary computers with no serial ports included, there is a big chance to have only one serial port available - namely that of the dongle.

You can use QueryDosDevice() (in Windows) for searching:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows, Math {for IfThen};
  3.  
  4. const
  5.   BUFSIZE = 1024;
  6.  
  7. function EnumComPorts: TStringList;
  8. var
  9.   lpTargetPath: PChar;
  10.   I: Integer;
  11. begin
  12.   lpTargetPath := GetMem(BUFSIZE);
  13.   try
  14.     Result := TStringList.Create;
  15.     for I := 1 to 99 do
  16.       if QueryDosDeviceA(PChar('COM' + IntToStr(I)), lpTargetPath, BUFSIZE) > 0 then
  17.         Result.Add(IfThen(I < 10, '', '\\.\') + 'COM' + IntToStr(I));
  18.   finally
  19.     FreeMem(lpTargetPath);
  20.   end;
  21. end;

Edit: As long for RS-485 - If your communication is half-duplex and you don't have a dedicated hardware, you can't expect nominal operation since in Windows there is no adequate way to control the direction of the RS-485 trancievers.
« Last Edit: July 14, 2021, 03:24:10 pm by y.ivanov »

MoellerCLaus

  • Full Member
  • ***
  • Posts: 110
    • Vig Foreningsprogram
Re: LazSerial: List of USB ports and virtual port issue
« Reply #18 on: July 15, 2021, 07:56:32 am »
Hi y.ivanov

Thanks for helping.
Just to be clear it is RS-485; similar but not the same as RS-232.
Your code scans for serial ports and will on my system find nothing - because nothing is plugged in yet. The code in my previous post will show which USB's the user can choose from.

y.ivanov

  • Full Member
  • ***
  • Posts: 229
Re: LazSerial: List of USB ports and virtual port issue
« Reply #19 on: July 15, 2021, 09:33:28 am »
Just to be clear it is RS-485; similar but not the same as RS-232.
It is clear. I wrote in my previous post about difficulties handling the RS-485 direction control in Windows. 
 
Your code scans for serial ports and will on my system find nothing - because nothing is plugged in yet. The code in my previous post will show which USB's the user can choose from.
Sorry, but I didn't get that. How you'd expect it to work when it is not plugged? Why the user should choose from USB ports as long as they're made (more or less) equal?

MoellerCLaus

  • Full Member
  • ***
  • Posts: 110
    • Vig Foreningsprogram
Re: LazSerial: List of USB ports and virtual port issue
« Reply #20 on: July 15, 2021, 10:29:48 am »
Quote
Sorry, but I didn't get that. How you'd expect it to work when it is not plugged? Why the user should choose from USB ports as long as they're made (more or less) equal?
:) You have a point.
With the posted code I can detect if any USB port is free. This is actually a problem because newer smaller PC comes only with 2 or even 1 USB port.
Further when plugged it can be detected which one. So no need for a combo ::).

 

TinyPortal © 2005-2018