Enumerating serial ports

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

I have working demo program from my fiscal printers vendor written in Borland Delphi, I think.
There is used such a function to detect COM ports:

Code: Select all

 procedure FindSerialPorts(Strings: TStrings);
  var
    vs: Cardinal;
    v: _COMMCONFIG;
    i: Integer;
  begin
    Strings.Clear;
    vs := SizeOf(v);
    for i := 1 to 32 do
      if GetDefaultCommConfig(PChar('COM' + IntToStr(i)), v, vs) then
        Strings.Add('COM' + IntToStr(i));
    Strings.Add('OICCommHub');
  end;
When googled, I found GetDefaultCommConfig

Code: Select all

BOOL WINAPI GetDefaultCommConfig(
  _In_     LPCTSTR lpszName,
  _Out_    LPCOMMCONFIG lpCC,
  _Inout_  LPDWORD lpdwSize
);
in

http://msdn.microsoft.com/en-us/library ... 85%29.aspx" onclick="window.open(this.href);return false;


But, I don't know how to use it with harbour.
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

There is also harbour function win_comIsValid, but I don't know how to use it...
User avatar
mol
Posts: 3774
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Enumerating serial ports

Post by mol »

I've tried such a code:

Code: Select all

   LOCAL cCom       := "COM1"
   LOCAL nBaudeRate := 9600
   LOCAL nDatabits  := 8
   LOCAL nParity    := 0 /* none */
   LOCAL nStopbit   := 1
   LOCAL nBuff      := 8000

aPorts := {}
for i:=1 to 32
	cPortName := "COM"+alltrim(str(i))
   s_nHandle := INIT_PORT( cPortName, nBaudeRate, nDatabits, nParity, nStopbit, nBuff )
   IF s_nHandle > 0 	
			aAdd(aPorts,cPortName)
	endif
next i

MsgDebug1("Com port list",aPorts)
and works OK

PS. rememeber to link with hbcomm library
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Enumerating serial ports

Post by Pablo César »

Our good friend Andrés gave us a solution for parallel ports and adapting for serial it will work perfectly too. See at: http://hmgforum.com/viewtopic.php?p=34894#p34894
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Javier Tovar
Posts: 1275
Joined: Tue Sep 03, 2013 4:22 am
Location: Tecámac, México

Re: Enumerating serial ports

Post by Javier Tovar »

Gracias a todos por compartir sus conocimientos!

Saludos
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thank you all for sharing your knowledge!

regards
Post Reply