COM-ports function

175 views
Skip to first unread message

TerjeC

unread,
May 3, 2026, 4:14:46 AM (7 days ago) May 3
to Harbour Users
I am compiling this program/function starting with this comment: "* HBCOMM compatibility library. EXPERIMENTAL CODE. USE AT YOUR OWN RISK. NO GUARANTEES.
 * Copyright 2010 Viktor Szakats (vszakats.net/harbour)"
It #includes HBCOM.CH  (HB30) and compiles fine. But linker cant't find 3 variables HB_Default, HB_Blen and HB_Bsubstr.
Does an upgraded version of HBCom.ch exist? Or can somebody suggest an other soloution to manage the serial-ports. Greatful for any help!

miro....@gmail.com

unread,
May 5, 2026, 5:36:49 PM (5 days ago) May 5
to Harbour Users
Create two standalone files: com_file_test.prg and com_file_test.hbp. This example has no external dependencies and is intended to compile with the latest
  Harbour 3.2dev toolchain using hbmk2.

VERSION WITHOUT hbcom. Instead, it opens the COM port as a Windows device file. Baud
  rate, parity, data bits, and stop bits must be configured beforehand in Windows Device Manager or with mode.

  ## com_file_test.prg

  #include "fileio.ch"

  #define TEST_COM_DEVICE   "\\.\COM1"
  #define TEST_COMMAND_TEXT "MSV?;"
  function Main()
  local nHandle := -1
  local cBuffer := ""
  local nBytesRead := 0

        qOut( "Opening " + TEST_COM_DEVICE )

        nHandle := fOpen( TEST_COM_DEVICE, FO_READWRITE )

        if nHandle < 0
                qOut( "Open failed. FError(): " + Str( fError() ) )
                return 1
        endif

        if !Empty( TEST_COMMAND_TEXT )
                qOut( "Sending: " + TEST_COMMAND_TEXT )

                if fWrite( nHandle, TEST_COMMAND_TEXT ) < Len( TEST_COMMAND_TEXT )
                        qOut( "Write failed or incomplete. FError(): " + Str( fError() ) )
                        fClose( nHandle )
                        return 2
                endif
        endif

        if TEST_READ_ONCE
                cBuffer := Space( 256 )

                qOut( "Reading once. This can block if Windows has no COM read timeout." )
                nBytesRead := fRead( nHandle, @cBuffer, Len( cBuffer ) )

                qOut( "Bytes read: " + Str( nBytesRead ) )

                if nBytesRead > 0
                        qOut( "Data: " + hb_BLeft( cBuffer, nBytesRead ) )
                endif
        endif

        fClose( nHandle )
        qOut( "Closed." )

  return 0

  ## com_file_test.hbp

  -hbexe
  -oComFileTest
  com_file_test.prg

  Build command:

  hbmk2 com_file_test.hbp

miro....@gmail.com

unread,
May 5, 2026, 5:44:11 PM (5 days ago) May 5
to Harbour Users
# Minimal Harbour COM Port Test

  ## com_port_test.prg

  procedure Main()
  local nPort           := 1   // COM1. Use 2 for COM2, 3 for COM3, etc.
  local nBaudRate       := 9600
  local cParity         := "N"
  local nDataBits       := 8
  local nStopBits       := 1
  local nTimeout        := 1000
  local cTransmitBuffer := "PING"
  local cReceiveBuffer  := space( 128 )
  local nBytesSent      := 0
  local nBytesReceived  := 0
  local nErrorCode      := 0
  local lPortOpened     := .f.
  local lPortConfigured := .f.

        qout( "Opening COM port:", nPort )

        lPortOpened := hb_comopen( nPort )

        if lPortOpened
                lPortConfigured := hb_cominit( nPort, nBaudRate, cParity, nDataBits, nStopBits )

                if lPortConfigured
                        hb_comflush( nPort )

                        nBytesSent := hb_comsend( nPort, cTransmitBuffer, len( cTransmitBuffer ), nTimeout )
                        nBytesReceived := hb_comrecv( nPort, @cReceiveBuffer, len( cReceiveBuffer ), nTimeout )
                        qout( "Bytes received:", nBytesReceived )

                        if nBytesReceived > 0
                                qout( "Received:", left( cReceiveBuffer, nBytesReceived ) )
                        endif
                else
                        nErrorCode := hb_comgeterror( nPort )
                        qout( "Could not configure COM port. Error:", nErrorCode )
                endif

                hb_comclose( nPort )
        else
                nErrorCode := hb_comgeterror( nPort )
                qout( "Could not open COM port. Error:", nErrorCode )
        endif

  return

  ## com_port_test.hbp

  -hbexe
  -ocom_port_test

  com_port_test.prg

  ## Compile Instructions

  1. Save both files in the same directory.
  2. Make sure hbmk2 is available in your PATH.
  3. Edit nPort in com_port_test.prg if you need another port: 1 = COM1, 2 = COM2, etc.
  4. Compile:

  hbmk2 com_port_test.hbp

  5. Run on Windows:

  .\com_port_test.exe

Tony Quick

unread,
May 6, 2026, 10:21:30 AM (4 days ago) May 6
to Harbour Users
I use the COM functions from hbct, which implements many of the COM functions from the Clipper Tools library.  I've had no issues with COM port communication  with barcode scanners using these function in Windows since 2013.  Sorry, I don't have any sample code to share.

Regards,
Tony

Reply all
Reply to author
Forward
0 new messages