April 17, 2024, 08:47:21 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Mysterious error

Started by Egil, May 16, 2016, 10:18:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

A couple of years ago I wrote a small "bare bones" serial port program for my neighbours then ten years old son. He has used it to experiment with an AIS receiver (decoding ship positions) and for communicating with a couple of friends using a simple interface to lisence-free radios. They have had a lot of fun for two years. But yesterday the program stopped working. Most probable some of the code has been changed, but I am not able to find the error. Neither had I saved a copy of the code on my own computer.

Here is the code for the console mode terminal program:
'
'---------------------------------------------------------------------------------------------------
' term.cba
'---------------------------------------------------------------------------------------------------
'
AUTODEFINE "OFF"

SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

type COMMTIMEOUT
    DEF ReadIntervalTimeout:INT 
    DEF ReadTotalTimeoutMultiplier:INT
    DEF ReadTotalTimeoutConstant:INT
    DEF WriteTotalTimeoutMultiplier:INT
    DEF WriteTotalTimeoutConstant:INT
endtype

type DCB
    DEF DCBlength:int
    DEF BaudRate:int
DEF flags:int
    DEF wReserved:word
    DEF XonLim:word
    DEF XoffLim:word
    DEF ByteSize:char
    DEF Parity:char
    DEF StopBits:char
    DEF XonChar:char
    DEF XoffChar:char
    DEF ErrorChar:char
    DEF EofChar:char
    DEF EvtChar:char
    DEF wReserved1:word
ENDTYPE

type COMSTAT
def flags:uint
def cbInQue:uint
def cbOutQue:uint
endtype

DECLARE "Kernel32",CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE "Kernel32",GetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",SetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",CloseHandle(handle:int),int
DECLARE "Kernel32",WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE "Kernel32",ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
DECLARE "Kernel32",SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int
DECLARE "Kernel32.dll",GetCommTimeouts(handle:int,timeout:pointer),int
DECLARE "Kernel32.dll",GetCommModemStatus( hFile:pointer lpModemStat:UINT),int
DECLARE "Kernel32.dll",ClearCommError( hFile:pointer lpdwErrors:pointer lpCommStat:UINT),int
DECLARE "Kernel32.dll",ZeroMemory alias RtlZeroMemory( Destination:pointer, Length:int)

def dcb1:DCB
def timeout:COMMTIMEOUT
def data,n$:string
dcb1.DCBlength = 26
timeout.ReadIntervalTimeout = 1
timeout.ReadTotalTimeoutMultiplier = 1
timeout.ReadTotalTimeoutConstant = 1

DECLARE "kernel32.dll", SetConsoleTitle Alias SetConsoleTitleA(lpConsoleTitle:STRING),INT
DECLARE ComPortOpen(pnum:int)
DECLARE ComPortRead(hc:int)
DECLARE ComPortSetup(hc:int)
DECLARE ComPortSend(hc:int,msg$:string)
DECLARE ComPortClose(hc:int)


def hcom:int
def rxd$:string
def key$,tx$,parm$:string
def port_number,bd:int
key$="":rxd$="":tx$ = ""

OPENCONSOLE

' Find available ComPort(-s):
print "Searching for available ComPorts.....":print
color 14,0

for port_number=1 to 255
hcom = ComPortOpen(port_number)
IF hcom <> -1
print "COM",port_number," available"
ComPortClose(hcom)
ENDIF
next port_number

color 7,0
print


' Choose port and baud:
INPUT "Choose Port number: ",port_number
INPUT "Choose baud       : ",bd

' Open selected port:
hcom = ComPortOpen(port_number)

IF hcom <> -1
ComPortSetup(hcom,bd,8,@ONESTOPBIT,@NOPARITY)
parm$ = "COM" + ltrim$(str$(port_number))+":" + ltrim$(str$(bd))+",8,N,1"
SetConsoleTitle(" Console Terminal Program   " + parm$ + "  -  Press ESC to close")
ELSE
color 12,0
if port_number=0
print:print"Invalid ComPort selected!
else
print:print "COM",port_number," not found"
SetConsoleTitle(" Selected ComPort NOT FOUND!  -  Press ESC to close")
endif
color 7,0
ENDIF

' Loop until ESC is pressed:
DO

' Rxd:
rxd$ = ComPortRead(hcom)
if rxd$ <> "" then print rxd$,

' Txd:
key$ = INKEY$
print key$,
IF key$ <> ""
tx$=tx$+key$
if right$(tx$,1) = chr$(13)
ComPortSend(hcom,tx$)
print chr$(10)
tx$ = ""
endif
ENDIF
UNTIL (GETKEYSTATE(0x1B))

DO
UNTIL (GETKEYSTATE(0x1B))
ComPortClose(hcom)
CLOSECONSOLE
END

'
SUB ComPortOpen(pnum:int)
'---------------------------------------------------------------------------------------------------
' Opens comport - works for COM1 to COM255
'---------------------------------------------------------------------------------------------------
def p$:string
def ch:int

p$=str$(pnum) :' THIS LINE SUDDENLY PRODUCE AN ERROR!!!!!!!
plen=len(p$)-1
ch = CreateFileA("\\.\COM"+right$(p$,plen),@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)

RETURN ch

'
SUB ComPortRead(hc:int)
'---------------------------------------------------------------------------------------------------
' Read opened comport
'---------------------------------------------------------------------------------------------------
DO
IF ReadFile(hc,data,1,bytesread,0) <> 0
if data <> ""
print data,
endif
data=""
ENDIF
UNTIL (GETKEYSTATE(0x1B))
RETURN

'
SUB ComPortSend(hc:int,msg$:string)
'---------------------------------------------------------------------------------------------------
' Send string to opened comport
'---------------------------------------------------------------------------------------------------
def i,written:int
WriteFile(hc,msg$,len(msg$),written,0)
RETURN

'
SUB ComPortSetup(hc:int)
'---------------------------------------------------------------------------------------------------
' Set comport parameters
'---------------------------------------------------------------------------------------------------
SetCommTimeouts(hcom,timeout)
IF GetCommState(hcom,dcb1)
PRINT "Opened COM"+ltrim$(str$(port_number))
dcb1.BaudRate = 38400
dcb1.ByteSize = 8
dcb1.StopBits = @ONESTOPBIT
dcb1.Parity = @NOPARITY
IF SetCommState(hcom,dcb1) = 0
PRINT "Error setting com parameters"
ELSE
PRINT "Baud rate set to ",dcb1.BaudRate
SetConsoleTitle("COM"+ltrim$(str$(port_number))+":"+ltrim$(str$(dcb1.BaudRate))+"8,N,1")
print
ENDIF
ENDIF
RETURN

'
SUB ComPortClose(hc:int)
'---------------------------------------------------------------------------------------------------
'  Closing ComPort
'---------------------------------------------------------------------------------------------------
CloseHandle(hc)
RETURN




Running the program produce the error in the picture below. (str$ Not found)


The whole thing becomes a mystery when I compare the subroutine code with identical code in another small utility I have used for years, and that works fine:
'
' CB_FindComPorts.cba
'----------------------------------------------------------------------------------------
' Searches through ComPorts COM1 to COM255
' and reports available ports
'----------------------------------------------------------------------------------------
'
SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

DECLARE "kernel32.dll", SetConsoleTitle Alias SetConsoleTitleA(lpConsoleTitle:STRING),INT
DECLARE "Kernel32",CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE "Kernel32",GetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",SetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",CloseHandle(handle:int),int
DECLARE "Kernel32",WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE "Kernel32",ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
DECLARE "Kernel32",SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int
DECLARE "Kernel32.dll",GetCommTimeouts(handle:int,timeout:pointer),int
DECLARE "Kernel32.dll",GetCommModemStatus( hFile:pointer lpModemStat:UINT),int
DECLARE "Kernel32.dll",ClearCommError( hFile:pointer lpdwErrors:pointer lpCommStat:UINT),int
DECLARE "Kernel32.dll",ZeroMemory alias RtlZeroMemory( Destination:pointer, Length:int)

type COMMTIMEOUT
    DEF ReadIntervalTimeout:INT 
    DEF ReadTotalTimeoutMultiplier:INT
    DEF ReadTotalTimeoutConstant:INT
    DEF WriteTotalTimeoutMultiplier:INT
    DEF WriteTotalTimeoutConstant:INT
endtype

type DCB
    DEF DCBlength:int
    DEF BaudRate:int
DEF flags:int
    DEF wReserved:word
    DEF XonLim:word
    DEF XoffLim:word
    DEF ByteSize:char
    DEF Parity:char
    DEF StopBits:char
    DEF XonChar:char
    DEF XoffChar:char
    DEF ErrorChar:char
    DEF EofChar:char
    DEF EvtChar:char
    DEF wReserved1:word
ENDTYPE

type COMSTAT
def flags:uint
def cbInQue:uint
def cbOutQue:uint
endtype


def dcb1:DCB
def timeout:COMMTIMEOUT
def hcom:int
dcb1.DCBlength = 26

timeout.ReadIntervalTimeout = -1
timeout.ReadTotalTimeoutMultiplier = 1
timeout.ReadTotalTimeoutConstant = 1

DECLARE ConsoleTitle(title:string)
DECLARE ComPortOpen(pn:int)

def port_number:int

OPENCONSOLE

ConsoleTitle("  CB ComPort Finder")
print
print "Serching for comports...."
print
print
color 14,0

for port_number=1 to 255
hcom = ComPortOpen(port_number)
IF hcom <> -1
print "COM",port_number," available"
CloseHandle(hcom)
ENDIF
next port_number

color 7,0
print
print
PRINT
PRINT "< Press ESC to close >"
PRINT
DO
UNTIL (GETKEYSTATE(0x1B))
CloseHandle(hcom)
CLOSECONSOLE
END

'
'---------------------------------------------------------------------------------------------------
' Open Comport - works for ports COM1 to COM255
'---------------------------------------------------------------------------------------------------
SUB ComPortOpen(pn:int)
def p$:string
def ch,plen:int
p$=str$(pn)
plen=len(p$)-1
ch = CreateFileA("\\.\COM"+right$(p$,plen),@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)
RETURN ch

'
'---------------------------------------------------------------------------------------------------
'  ConsoleTitle   -   Setting Console Titles
'---------------------------------------------------------------------------------------------------
SUB ConsoleTitle(title:string)
SetConsoleTitle(title)
RETURN



The subroutines (ComPortOpen) are identical in the two programs. But in the terminal program that subroutine suddenly started to produce an error.

Any explanation would be very much appreciated.

Support Amateur Radio  -  Have a ham  for dinner!

Andy

May 16, 2016, 11:49:57 AM #1 Last Edit: May 16, 2016, 11:52:58 AM by Andy
Egil,

My shot in the dark if no one else spots a programming fault....

I've similar problems (rarely) in the past with non displayable characters in the source file.

How about deleting some lines, start a few lines before the error, and a few lines after the error.

Then manually (not copy and paste) re-type all the lines including the error line.

That's how I solved it in the past.

Also you have a ":" in one example and not the other, try adding in the ":" or removing it....

Shot in the dark, for what it's worth.

Andy.
:)



Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

GWS

Hi Egil,

Missing the terminating double quote on the line :

print:print"Invalid ComPort selected!"   ;D

I've fiddled with it a bit .. now using an array of found ports .. found[256]
Then if the user chooses a port which has not been found, he will get the above message .. ;D

Here's the modified code:


'----Mod: May 2016 with found port array found[] ----------------------------------------
'
AUTODEFINE "OFF"

SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

type COMMTIMEOUT
    DEF ReadIntervalTimeout:INT 
    DEF ReadTotalTimeoutMultiplier:INT
    DEF ReadTotalTimeoutConstant:INT
    DEF WriteTotalTimeoutMultiplier:INT
    DEF WriteTotalTimeoutConstant:INT
endtype

type DCB
    DEF DCBlength:int
    DEF BaudRate:int
DEF flags:int
    DEF wReserved:word
    DEF XonLim:word
    DEF XoffLim:word
    DEF ByteSize:char
    DEF Parity:char
    DEF StopBits:char
    DEF XonChar:char
    DEF XoffChar:char
    DEF ErrorChar:char
    DEF EofChar:char
    DEF EvtChar:char
    DEF wReserved1:word
ENDTYPE

type COMSTAT
def flags:uint
def cbInQue:uint
def cbOutQue:uint
endtype

DECLARE "Kernel32",CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE "Kernel32",GetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",SetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",CloseHandle(handle:int),int
DECLARE "Kernel32",WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE "Kernel32",ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
DECLARE "Kernel32",SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int
DECLARE "Kernel32.dll",GetCommTimeouts(handle:int,timeout:pointer),int
DECLARE "Kernel32.dll",GetCommModemStatus( hFile:pointer lpModemStat:UINT),int
DECLARE "Kernel32.dll",ClearCommError( hFile:pointer lpdwErrors:pointer lpCommStat:UINT),int
DECLARE "Kernel32.dll",ZeroMemory alias RtlZeroMemory( Destination:pointer, Length:int)

def dcb1:DCB
def timeout:COMMTIMEOUT
def data,n$:string
dcb1.DCBlength = 26
timeout.ReadIntervalTimeout = 1
timeout.ReadTotalTimeoutMultiplier = 1
timeout.ReadTotalTimeoutConstant = 1

DECLARE "kernel32.dll", SetConsoleTitle Alias SetConsoleTitleA(lpConsoleTitle:STRING),INT
DECLARE ComPortOpen(pnum:int)
DECLARE ComPortRead(hc:int)
DECLARE ComPortSetup(hc:int)
DECLARE ComPortSend(hc:int,msg$:string)
DECLARE ComPortClose(hc:int)

def hcom:int
def rxd$:string
def key$,tx$,parm$:string
def port_number,bd:int

def found[256]:int :' array of found com ports
for i = 0 to 255
found[i] = 0 :' initialise array
next i

key$="":rxd$="":tx$ = ""

OPENCONSOLE

' Find available ComPort(-s)
print "Searching for available ComPorts.....":print
color 14,0

for port_number = 1 to 255
hcom = ComPortOpen(port_number)
IF (hcom <> -1)
print "COM",port_number," available"
found[port_number] = 1
ComPortClose(hcom)
ENDIF
next port_number

color 7,0
print

' Choose port and baud:
INPUT "Choose Port number: ",port_number
INPUT "Choose baud       : ",bd

' Open selected port:
hcom = ComPortOpen(port_number)

IF (hcom <> -1)
ComPortSetup(hcom,bd,8,@ONESTOPBIT,@NOPARITY)
parm$ = "COM" + ltrim$(str$(port_number))+":" + ltrim$(str$(bd))+",8,N,1"
SetConsoleTitle(" Console Terminal Program   " + parm$ + "  -  Press ESC to close")
ELSE
color 12,0
if (found[port_number]=0)
print:print"Invalid ComPort selected!"
else
print:print "COM",port_number," not found"
SetConsoleTitle(" Selected ComPort NOT FOUND!  -  Press ESC to close")
endif
color 7,0
ENDIF


DO
UNTIL (GETKEYSTATE(0x1B))
ComPortClose(hcom)
CLOSECONSOLE
END

SUB ComPortOpen(pnum:int)
'---------------------------------------------------------------------------------------------------
' Opens comport - works for COM1 to COM255
'---------------------------------------------------------------------------------------------------
def p$:string
def ch,plen:int

p$=str$(pnum) :' THIS LINE SUDDENLY PRODUCE AN ERROR!!!!!!!
plen=len(p$)-1
ch = CreateFileA("\\.\COM"+right$(p$,plen),@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)

RETURN ch

SUB ComPortClose(hc:int)
'---------------------------------------------------------------------------------------------------
'  Closing ComPort
'---------------------------------------------------------------------------------------------------
CloseHandle(hc)
RETURN

SUB ComPortRead(hc:int)
'---------------------------------------------------------------------------------------------------
' Read opened comport
'---------------------------------------------------------------------------------------------------
DO
IF ReadFile(hc,data,1,bytesread,0) <> 0
if data <> ""
print data,
endif
data=""
ENDIF
UNTIL (GETKEYSTATE(0x1B))
RETURN

SUB ComPortSetup(hc:int)
'---------------------------------------------------------------------------------------------------
' Set comport parameters
'---------------------------------------------------------------------------------------------------
SetCommTimeouts(hcom,timeout)
IF GetCommState(hcom,dcb1)
PRINT "Opened COM"+ltrim$(str$(port_number))
dcb1.BaudRate = 38400
dcb1.ByteSize = 8
dcb1.StopBits = @ONESTOPBIT
dcb1.Parity = @NOPARITY
IF SetCommState(hcom,dcb1) = 0
PRINT "Error setting com parameters"
ELSE
PRINT "Baud rate set to ",dcb1.BaudRate
SetConsoleTitle("COM"+ltrim$(str$(port_number))+":"+ltrim$(str$(dcb1.BaudRate))+"8,N,1")
print
ENDIF
ENDIF
RETURN

SUB ComPortSend(hc:int,msg$:string)
'---------------------------------------------------------------------------------------------------
' Send string to opened comport
'---------------------------------------------------------------------------------------------------
def i,written:int
WriteFile(hc,msg$,len(msg$),written,0)
RETURN



I've a vague idea where the error message about str$ came from - the parser must have thought the unfinished string carried on .. and on ..

best wishes, :)

Graham



Tomorrow may be too late ..

Egil

May 17, 2016, 04:24:44 AM #3 Last Edit: May 17, 2016, 04:33:43 AM by Egil
Thanks Andy and Graham!

It appears that the boys have been experimenting to achieve more or less the same functionality as you have added. But they forgot to make a copy of the original file before changing the code. Guess they never do that again... ;)

This morning I  found the "original" code on a USB-stick (posted below). As you can see the code is a little different from what I posted yesterday. As I've said in other posts, many of the young kids here are very interested in robotics and remote control, using serial communication to program and control simple microcontollers and programmable sensors. So I made this code to help them do this while waiting for the CB Serial Component to be fixed.

Now I'll study Grahams idea.


Regards,

Egil


'
'------------------------------------------------------------------------------
' TncTerm.cba
' by Egil - February,2014
'------------------------------------------------------------------------------
'
AUTODEFINE "OFF"

InitComms()

def dcb1:DCB
def timeout:COMMTIMEOUT
def hcom,success,written,bytesread:int
def data,n$:string
dcb1.DCBlength = 26
timeout.ReadIntervalTimeout = 1
timeout.ReadTotalTimeoutMultiplier = 1
timeout.ReadTotalTimeoutConstant = 1

DECLARE "kernel32.dll", SetConsoleTitle Alias SetConsoleTitleA(lpConsoleTitle:STRING),INT

DECLARE ComPortSetup(hc:int,bd:int,dbits:int,sbits:int,pty:int)
DECLARE ComPortOpen(pnum:int)
DECLARE ComPortRead(hc:int)
DECLARE ComPortSend(hc:int,msg$:string)
DECLARE ComPortClose(hc:int)

DECLARE FindComPorts()

def rxd$:string
def key$,tx$,parm$:string
def port_number,bd:int
key$=""
rxd$=""
tx$ = ""  

OPENCONSOLE

FindComPorts()

input "Choose Port number: ",port_number
input "Choose Baudrate   : ",bd

hcom = ComPortOpen(port_number)

IF hcom <> -1

  ComPortSetup(hcom,bd,8,@ONESTOPBIT,@NOPARITY)

  parm$ = "COM" + ltrim$(str$(port_number))+":" + ltrim$(str$(bd))+",8,N,1"
  SetConsoleTitle(" LA2PJ's Console Terminal Program   " + parm$ + "  -  Press ESC to close")

  ' Loop until ESC is presed
  DO

     ' Rxd:
     rxd$ = ComPortRead(hcom)
     if rxd$ <> "" then print rxd$,

     ' Txd:
     key$ = INKEY$

        print key$,
     IF key$ <> ""
        tx$=tx$+key$
        if right$(tx$,1) = chr$(13)
           ComPortSend(hcom,tx$)
           print chr$(10)
           tx$ = ""
        endif  
     ENDIF
  UNTIL (GETKEYSTATE(0x1B))
else
  print "COM",port_number," not found"
ENDIF


DO
UNTIL (GETKEYSTATE(0x1B))
ComPortClose(hcom)
CLOSECONSOLE
END
'---------------------------------------------------------------------------------------------------


'
SUB ComPortRead(hc:int)
'---------------------------------------------------------------------------------------------------
' Read opened comport
'---------------------------------------------------------------------------------------------------
def data:string
data=""
  ReadFile(hc,data,1,bytesread,0)
RETURN data

'
SUB ComPortSend(hc:int,msg$:string)
'---------------------------------------------------------------------------------------------------
' Send string to opened comport
'---------------------------------------------------------------------------------------------------
def i,written:int
  WriteFile(hc,msg$,len(msg$),written,0)
RETURN

'
SUB ComPortOpen(pnum:int)
'---------------------------------------------------------------------------------------------------
' Opens comport - works for COM1 - COM256
'---------------------------------------------------------------------------------------------------
  def p$:string
  def ch,plen:int
  p$=str$(pnum)
  plen=len(p$)-1
  ch = CreateFileA("\\.\COM"+right$(p$,plen),@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)
RETURN ch

'
SUB ComPortSetup(hc:int,bd:int,dbits:int,sbits:int,pty:int)
'---------------------------------------------------------------------------------------------------
' Set comport parameters
'---------------------------------------------------------------------------------------------------
  SetCommTimeouts(hc,timeout)
  IF GetCommState(hc,dcb1)
     PRINT:PRINT "Opened COM"+ltrim$(str$(port_number))
     dcb1.BaudRate = bd
     dcb1.ByteSize = dbits
     dcb1.StopBits = sbits
     dcb1.Parity = pty
     IF SetCommState(hc,dcb1) = 0
        PRINT "Error setting com parameters"
     ELSE
        PRINT "Baud rate set to ",dcb1.BaudRate
        print
     ENDIF  
  ENDIF
RETURN

'
SUB FindComPorts()
'---------------------------------------------------------------------------------------------------
'  Find available comPorts
'---------------------------------------------------------------------------------------------------
print
print "Serching for comports...."
print:print
color 14,0

for port_number=1 to 256
  hcom = ComPortOpen(port_number)
  IF hcom <> -1
     print "COM",port_number," available"
     'CloseHandle(hcom)
     ComPortClose(hcom)
  ENDIF
next port_number

color 7,0
print:print
return

'
SUB ComPortClose(hc:int)
'---------------------------------------------------------------------------------------------------
'  Closing ComPort
'---------------------------------------------------------------------------------------------------
  CloseHandle(hc)
RETURN

'
SUB InitComms()
'---------------------------------------------------------------------------------------------------
'  ComPort declares and definitions:
'---------------------------------------------------------------------------------------------------
  DECLARE "Kernel32.dll",CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
  DECLARE "Kernel32.dll",GetCommState(handle:int,lpdcb:DCB),int
  DECLARE "Kernel32.dll",SetCommState(handle:int,lpdcb:DCB),int
  DECLARE "Kernel32.dll",CloseHandle(handle:int),int
  DECLARE "Kernel32.dll",WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
  DECLARE "Kernel32.dll",ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
  DECLARE "Kernel32.dll",SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int
  DECLARE "Kernel32.dll",GetCommTimeouts(handle:int,timeout:pointer),int
  DECLARE "Kernel32.dll",GetCommModemStatus( hFile:pointer lpModemStat:UINT),int
  DECLARE "Kernel32.dll",ClearCommError( hFile:pointer lpdwErrors:pointer lpCommStat:UINT),int
  DECLARE "Kernel32.dll",ZeroMemory alias RtlZeroMemory( Destination:pointer, Length:int)

  SETID "OPENEXISTING",3
  SETID "GENERICREAD",0x80000000
  SETID "GENERICWRITE",0x40000000
  SETID "NOPARITY",0
  SETID "ODDPARITY",1
  SETID "EVENPARITY",2
  SETID "MARKPARITY",3
  SETID "SPACEPARITY",4
  SETID "ONESTOPBIT",0
  SETID "TWOSTOPBITS",2

  type COMMTIMEOUT
      DEF ReadIntervalTimeout:INT  
      DEF ReadTotalTimeoutMultiplier:INT
      DEF ReadTotalTimeoutConstant:INT
      DEF WriteTotalTimeoutMultiplier:INT
      DEF WriteTotalTimeoutConstant:INT
  endtype

  type DCB
     DEF DCBlength:int
      DEF BaudRate:int
     DEF flags:int
     DEF wReserved:word
      DEF XonLim:word
      DEF XoffLim:word
      DEF ByteSize:char
      DEF Parity:char
      DEF StopBits:char
      DEF XonChar:char
      DEF XoffChar:char
      DEF ErrorChar:char
      DEF EofChar:char
      DEF EvtChar:char
      DEF wReserved1:word
  ENDTYPE

  type COMSTAT
     def flags:uint
     def cbInQue:uint
     def cbOutQue:uint
  endtype
RETURN




EDIT: Just had a phone call... two of the guys still use the original code on their laptops.  ::) :D ;D
Support Amateur Radio  -  Have a ham  for dinner!