Recent

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

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: LazSerial: List of USB ports and virtual port issue
« Reply #15 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 »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
Re: LazSerial: List of USB ports and virtual port issue
« Reply #16 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.

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: LazSerial: List of USB ports and virtual port issue
« Reply #17 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?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
Re: LazSerial: List of USB ports and virtual port issue
« Reply #18 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