How to Detect Valid COM Ports
Keywords: pComOpen pGetLastError(1)
Question:
I want to be able to search for a modem on COM ports 1-4. If a modem isn't found on
any of those ports, I'll display a message to that effect. But if I get all the way to COM4
without finding a modem, I get a WIL error I don't understand. Here's the relevant part
of the batch:
display(3,"Please wait...","Looking for modem on COM4")
port=pComOpen("COM4",0,9600,"8N1","XONXON")
pPutString(port,"ATZ")
pPutString(port,@cr)
if pWaitFor(port,"OK","",0,3000)==0
pComClose(port,-1)
goto NO_MODEM
else
ComPort=4
goto SHOWCOMPORT
endif
goto NO_MODEM
The error message is:
WIL Extender Error---------
port=pComOpen("COM4",0,9600,"8N1","XONXON")
Winbatch 32.97D
WIL Version 2.4dbp
There is no number in the Error Message, only a series of dashes.
The PC on which this error message was generated does not have a COM4, but I've
tested the same script on a PC that does, and get the same message. Is there a way to
check either the validity of a COM port, or for an error message from pComOpen() ?
Answer:
Try the following code instead...
;=======================snip===========================
DirChange("c:\program files\WinBatch\system")
AddExtender("wwser34I.dll")
debug(1)
display(3,"Please wait...","Looking for modem on COM4")
ErrorMode(@off)
port=pComOpen("COM4",0,9600,"8N1","XONXON")
ErrorMode(@cancel)
code=pGetLastError(1)
Message("Open err msg",code)
if port==- then exit
pPutString(port,"ATZ")
pPutString(port,@cr)
if pWaitFor(port,"OK","",0,3000)==0
pComClose(port,-1)
goto NO_MODEM
else
ComPort=4
goto SHOWCOMPORT
endif
goto NO_MODEM
;=======================================================
After capturing the error code, do a StrIndex to test whether the
code indicated an invalid port. Works great.
Article ID: W12570
Filename: Detecting Valid COM Ports.txt