MSComm when no modem it hangs for 30 seconds 
Author Message
 MSComm when no modem it hangs for 30 seconds

NOTE: Amazon is sending me Richard Grier's book as we speak. Also I read
the postings
on this control regularly. It is **TRICKY**. Many people are
experiencing what I am.
====================================================================================

My question:
When my modem is turned off my program hangs for 30 seconds (or so) then
sends
my timeout messsage. Why is this taking so long? I also had a BREAKPOINT
on
the OnComm event which never fired whent he modem was truned off! If I
am sending
commnds out that port shouldn't that event fire? I suspect
hardware/windows settings.

I am using Windows NT 4.0, VB 6.0, settings are 19200,N,8,1 RThreshhold
10 SThreshhold 1
DTREnabled True RTSEnable True Handshaking 3-comRTSXon/Xoff. I use the
interupt method
for capturing my data.

I have been able to succesfully create an MSComm app that writes
commands to a modem
then reads the return strings. I use a Timer. The Timer1_Timer event
fires every 1/2
second and sends four commands each terminated with <CR>. Cool! works
fine under
ideal circumstances. If I turn the modem off or unplug it then I hang
for 30 seconds.
I have a Counter in the Timer event that increments with each 1/2 second
firing of the
timer. I hoped to use this scheme for detecting that the modem is off
but it always
takes 30 seconds. I do get my MsgBox saying the modem does not respond,
but since I only
allow the Counter to be <= 5, and the timer fires every 1/2 second, then
I would expect
a response in about 2-3 seconds. It takes 30.

I hope Grier's book can clear this up once and for all. I come from a
delphi background
and while there are some limitations to TurboPower's ASYNC Pro control
it still behaves
better than the MsComm control, for me. I anxiously await Richard's
book.  BTW, for what
it is worth, I am loving VB and SQL 7.0. I liked Delphi but I see a
greater future in VB.

Thanks all
Jeannine Menger



Mon, 06 May 2002 03:00:00 GMT  
 MSComm when no modem it hangs for 30 seconds
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.



Mon, 06 May 2002 03:00:00 GMT  
 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 »



Mon, 06 May 2002 03:00:00 GMT  
 MSComm when no modem it hangs for 30 seconds
Hi Jeannine,

How does one reliably detect that a modem is uplugged or turned off?
<<

When you get my book you will find several examples for different
situations.  Basically,

Private Function ModemCommand(ATCommand As String) As Integer
    Dim Timeout As Integer
    Dim Buffer As String
    Comm1.Output = ATCommand & vbCr
    ModemCommand = 2                                     'assume a timeout
    Timeout = Timer + 1
    Do Until Timer > Timeout&
        DoEvents
        Buffer = Buffer & Comm1.Input
        If InStr(Buffer, "OK" & vbCrLf Then
            ModemCommand = 0                            'OK!
            Exit Do
        End If
        If InStr(Buffer, "ERROR" & vbCrLf) Then
           ModemCommand = 1                             'Error!
           Exit Do
        End If
    Loop
End Function

--
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.



Tue, 07 May 2002 03:00:00 GMT  
 MSComm when no modem it hangs for 30 seconds

Quote:

> Hi Jeannine,

> How does one reliably detect that a modem is uplugged or turned off?
> <<

> When you get my book you will find several examples for different
> situations.  Basically,

> Private Function ModemCommand(ATCommand As String) As Integer
>     Dim Timeout As Integer
>     Dim Buffer As String
>     Comm1.Output = ATCommand & vbCr
>     ModemCommand = 2                                     'assume a timeout
>     Timeout = Timer + 1
>     Do Until Timer > Timeout&
>         DoEvents
>         Buffer = Buffer & Comm1.Input
>         If InStr(Buffer, "OK" & vbCrLf Then
>             ModemCommand = 0                            'OK!
>             Exit Do
>         End If
>         If InStr(Buffer, "ERROR" & vbCrLf) Then
>            ModemCommand = 1                             'Error!
>            Exit Do
>         End If
>     Loop
> End Function

> --
> 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.

Actually your bookw as key. I placed a delay(1) right after a sending
an "AT", then I looped 5 times or less looking for my return value.
If I reached 5 times or more and no return vakue then I reported
a timeout : "Modem is unplugged or turned off".

Success story:
Thanks for the fine book. One of the best $35 I have ever spent. It
basically made me money with my client. They handed me a ***HAIRY***
programming project that the EEs were supposed to do, since I got
the other one done so quickly. Got that one done too. With the next
one we needed to delay 10 seconds on one command. We had to time
real world events and program around empirical results. Worked great!
Our production line is flowing nicely now. What we are doing is PINGing
the IPs in wireless modems.

Jeannine Menger



Sat, 15 Jun 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. createobject of Excel.Application.8 takes 30 seconds

2. Program takes 30 seconds to shutdown

3. 30 second timeout in Crystal.Net viewer or report

4. Data Environment command timeout exactly 30 seconds

5. (BUG): Hanging up the modem without mscomm

6. Hang opening second project

7. Delete Hang Up at second call

8. PC hangs after the second report start

9. Delete Hang Up at second call

10. Hanging up modem

11. hang up modem - how?

12. hang up modem?

 

 
Powered by phpBB® Forum Software