# 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