
MSComm when no modem it hangs for 30 seconds
Quote:
> Hi Jeannine,
> I think I'd have to see your specific code to understand what you mean by "a
> timeout after 30 seconds." Any such timeout would be controlled by your
> code. MSComm simply sends the data -- it doesn't care if anything is
> connected to the serial port or not.
> So, it is something in your code that is looking for a modem response. If
> it isn't received within the 30 seconds, it displays the MsgBox. Simply
> change this timeout to some more reasonable number (2 seconds should always
> be sufficient).
> --
> Richard Grier (Microsoft Developer MVP)
> Hard & Software
> 12962 West Louisiana Avenue
> Lakewood, CO 80228
> 303-986-2179 (voice)
> 303-986-3143 (fax)
> Author of Visual Basic Programmer's Guide to Serial Communications, 2nd
> Edition (355 pages).
> For information look on my homepage at http://www.hardandsoftware.net.
> Use the Books link to order. For faster service contact the publisher at
> http://www.mabry.com.
What I mean is that while the modem is on the response is almost
immediatte.
Whne it is off the response is 30 seconds despite my timeing out in 2
1/2 seconds.
You comments do make me suspicious of my code however.
How does one reliably detect that a modem is uplugged or turned off?
My code (note the function ModemDoesNotRespond() does not work
corectly):
Option Explicit
Dim IP, EID, ARN, ASN As String
Dim IPID As Integer
Dim SentCode, CntrStarted As Boolean
Dim EOTBookMark As Integer
Dim NumberOfTries As Integer
Dim Uw As New ADODB.Connection
Dim Buff As Variant
Private Sub Command1_Click()
Label1.Caption = NextIPID
End Sub
Private Sub CommandProgram_Click()
Dim Ws As New ADODB.Connection
Dim Rs As New ADODB.Recordset
List1.AddItem ("begining of CommandProgram_Click " & Now)
If OrdersToFill Then
If ModemDoesNotRespond Then
MsgBox ("Modem not connected")
Else
NumberOfTries = 0
TextModemOutput = ""
SentCode = False
IPID = Trim(NextIPID)
If IPID > 0 Then
Ws.Open ("Provider=SQLOLEDB.1;Inte.... etc
Rs.Open "select IPID, IP from tblIP where IPStatus = 'Unused'
order by IPID", Ws, adOpenDynamic, adLockReadOnly
If Rs.EOF Then
IP = 0
Else
' Rs.MoveFirst
IP = Rs("IP")
End If
LabelFinishedIP.Caption = IP
CntrStarted = True
Timer1.Enabled = True
Else
CommandProgram.Enabled = False
LabelMsgToUser.FontBold = True
LabelMsgToUser.FontSize = 18
LabelMsgToUser.Caption = "ERROR: no more IPs available"
LabelMsgToUser.Refresh
End If ' If NextIPID > 0
Rs.Close
Ws.Close
Set Rs = Nothing
Set Ws = Nothing
End If ' end if DSRHolding
Else
MsgBox ("No orders to fill")
End
End If
List1.AddItem ("end of CommandProgram_Click " & Now)
End Sub
Function SendCommands()
Dim x As Integer
DoEvents
MSComm1.Output = "at" & Chr(13)
MSComm1.Output = "at" & Chr(13)
If Not SentCode Then
MSComm1.Output = "at\aProg,NBRM6934_12" & Chr(13)
MSComm1.Output = "at#1130 =" & IP & "/1" & Chr(13)
SentCode = True
End If
DoEvents
MSComm1.Output = "atI" & Chr(13)
DoEvents
MSComm1.Output = "atS110?" & Chr(13)
End Function
Function HasEOT(InStr)
Dim i As Integer
Dim EOTtstStr As String
HasEOT = False
For i = 1 To Len(TextModemOutput.Text)
EOTtstStr = Mid(TextModemOutput.Text, i, 5)
If UCase(EOTtstStr) = "HELLO" Then
EOTBookMark = (i - 4)
LabelModemInfo.Caption = LabelModemInfo.Caption & (i - 4) & " is
where <EOT> starts" & Chr(13)
HasEOT = True
End If
Next
End Function
Function DisplayResult()
Dim i As Integer
Dim smallStr As String
LabelModemInfo.Caption = ""
If EOTBookMark >= 57 Then
For i = (EOTBookMark - 57) To EOTBookMark
smallStr = Mid(TextModemOutput.Text, i, 1)
LabelModemInfo.Caption = LabelModemInfo.Caption & smallStr
DisplayResult = LabelModemInfo.Caption
Next
Else
LabelModemInfo.Caption = "Could not find modem. Please be sure modem
is 'ON' then retry. If this fails then plug in a new modem, thi sone may
be defective."
DisplayResult = ""
End If
End Function
Private Sub Form_Load()
Uw.Open ("Provider=SQLOLEDB.1;Integrated Secur... etc
CntrStarted = False
IPID = NextIPID
If IPID > 0 Then
NumberOfTries = 0
SentCode = False
MSComm1.PortOpen = True
Else
LabelMsgToUser.FontBold = True
LabelMsgToUser.FontSize = 18
LabelMsgToUser.Caption = "ERROR: no more IPs available"
CommandProgram.Enabled = False
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Uw.Close
Set Uw = Nothing
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent ' Handle each event or error by placing
' code below each case statement
' Errors
Case comNoOpen
Case comEventBreak ' A Break was received.
Case comEventFrame ' Framing Error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB] ' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars.
TextModemOutput.Text = TextModemOutput.Text & MSComm1.Input
' Buff = Buff & MSComm1.Input
' ResultStr = ResultStr & MSComm1.Input
Case comEvSend
' There are SThreshold number of
' characters in the transmit
' buffer.
Case comEvEOF ' An EOF charater was found in
' the input stream
End Select
End Sub
Private Sub Timer1_Timer()
Dim EOTFound As Boolean
Dim RcvData As String
Dim i, CommaCount As Integer
Dim smallStr, someStr As String
List1.AddItem ("Begining of Timer just did SendCommands, Number of
tries is " & NumberOfTries & " " & Now)
SendCommands
NumberOfTries = NumberOfTries + 1
List1.AddItem ("Begining of Timer just finished SendCommands, Number
of tries is " & NumberOfTries & " " & Now)
EOTFound = HasEOT(TextModemOutput.Text)
If ((EOTFound) Or (NumberOfTries > 5)) Then
Timer1.Enabled = False
End If
If EOTFound Then
List1.AddItem ("EOT found start parsing data " & Now)
RcvData = DisplayResult
EID = ""
smallStr = ""
someStr = ""
CommaCount = 0
For i = 1 To Len(RcvData)
smallStr = Mid(RcvData, i, 1)
If smallStr = "," Then
CommaCount = CommaCount + 1
If CommaCount = 1 Then
EID = Mid(someStr, 1, 100)
someStr = ""
Else
If CommaCount = 2 Then
' skip this one it is the IP
someStr = ""
Else
If CommaCount = 3 Then
ASN = Mid(someStr, 2, 100)
someStr = ""
Else
If CommaCount = 4 Then
ARN = Mid(someStr, 2, 100)
someStr = ""
Else
End If ' end If ARN
End If ' end If ASN
End If ' end If <skip>
End If ' end if EID
End If ' end If smallStr = ","
If ((smallStr <> Chr(13)) And (smallStr <> Chr(10))) Then
someStr = someStr & smallStr
End If
Next
LabelEID.Caption = EID
LabelEID.Refresh
LabelARN.Caption = ARN
LabelARN.Refresh
LabelASN.Caption = ASN
LabelASN.Refresh
LabelMsgToUser.Caption = "Trying . . ."
LabelMsgToUser.Refresh
List1.AddItem ("EOT found end parsing data " & Now)
If Not EIDAlreadyExists(FormatEID(EID)) Then
List1.AddItem ("start of UPDATE query" & Now)
Uw.Execute ("UPDATE tblIP SET IPStatus = 'Used', EID = '" &
FormatEID(EID) & "',ARN = '" & ARN & "',ASN = '" & ASN & "' where IPID =
" & IPID)
List1.AddItem ("end of UPDATE query" & Now)
MsgBox ("Programming Succesful - you may now disconnect the unit"
+ Chr(13) + Chr(10) + "To program another, hit enter.")
Else
LabelEID.Caption = ""
LabelARN.Caption = ""
LabelASN.Caption = ""
LabelFinishedIP.Caption = ""
MsgBox ("EID : " & EID & " already has been programmed. Please
try another unit.")
End If ' end If Not EIDAlreadyExists(FormatEID(EID))
End If ' end if EOT found
If NumberOfTries > 5 Then
MsgBox ("Modem seems to not respond, Please check it is plugged in
and 'ON'")
End If
List1.AddItem ("end of Timer, Number of tries is " & NumberOfTries & "
" & Now)
LabelMsgToUser.Caption = ""
LabelMsgToUser.Refresh
End Sub
Function OrdersToFill()
If Not AdodcOrders.Recordset.EOF Then
AdodcOrders.Recordset.MoveLast
End If
If AdodcOrders.Recordset.RecordCount > 0 Then
OrdersToFill = True
Else
OrdersToFill = False
End If
End Function
Function EIDAlreadyExists(EID)
Dim Ws As New ADODB.Connection
Dim Rs As New ADODB.Recordset
List1.AddItem ("in EIDAlreadyExists - start of opening recordset using
SELECT" & Now)
Ws.Open ("Provider=SQLOLEDB.1;Integra.....etc
Rs.Open "Select IPID,EID from tblIP where EID = '" & EID & "'", Ws,
adOpenDynamic, adLockReadOnly
List1.AddItem ("in EIDAlreadyExists - end of opening recordset using
SELECT" & Now)
If Not Rs.EOF Then
Rs.MoveFirst
If EID = Trim(Rs.Fields("EID")) Then
EIDAlreadyExists = True
Else
EIDAlreadyExists = False
End If
Else
EIDAlreadyExists = False
End If
Rs.Close
Ws.Close
Set Rs = Nothing
Set Ws = Nothing
List1.AddItem ("exiting EIDAlreadyExists " & Now)
End Function
Function FormatEID(EID)
Dim i As Integer
Dim someStr, smallStr As String
someStr = ""
For i = 1 To Len(EID)
smallStr = Mid(EID, i, 1)
If ((smallStr
...
read more »