MScomm error trap needed



hello all,

I have an VB 5.0  app  that takes  readings from an external sensor  and  updates  the application.....this  works  fine  but  what  I need  help  with  is  if  the  device  is not connected,  or there is another  problem....my  application  hangs  to the point  of  crashing  the machine.  I need  help  with  error  trapping  that  will allow  recovering  from this situation.  ( i.e.   the user can click connect,  and if after a specified time it does not begin  transmitting data it will  give  an error  and  go back to the previous screen.  The code  I have  is as  follows.

*******************************************
Private Sub cmdcon_Click()
'error trapping
If MSComm1.PortOpen = True Then
    Exit Sub
End If
'Comm port
MSComm1.CommPort = cbocomm.ListIndex + 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 5
MSComm1.RThreshold = 5
MSComm1.InBufferSize = 12
' Open the port.
MSComm1.PortOpen = True
End Sub
*********************************************

Like I said, If  the  sensor  is  not  present  or plugged in,  it  crashes  the app  big-time.  I just  need  something  suitable to prevent the crashing.


DaveMon
LVL 2
DaveMonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sage020999Commented:
Are you sending a poll command to the device or are you using the onComm event to let you know when you receive a reading?
0
Sage020999Commented:
..see if any of this is what you are looking for.




Private Sub MSComm1_OnComm()
         Dim InBuff As String

         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.

         ' This template is found in the Example
         ' section of the OnComm event Help topic
         ' in VB Help.

         ' Errors
            Case comEventBreak   ' A Break was received.
            Case comEventCDTO    ' CD (RLSD) Timeout.
            Case comEventCTSTO   ' CTS Timeout.
            Case comEventDSRTO   ' DSR Timeout.
            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.
               InBuff = MSComm1.Input
               Call HandleInput(InBuff)
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select

      End Sub

      Sub HandleInput(InBuff As String)
         ' This is where you will process your input. This
         ' includes trapping characters, parsing strings,
         ' separating data fields, etc. For this case, you
         ' are simply going to display the data in the TextBox.
         Text1.SelStart = Len(Text1.Text)
         Text1.SelText = InBuff
      End Sub
 

0
DaveMonAuthor Commented:


Im not sure, exactly,  Im  not  sending  anything to the device, here  is some  additional  code  I copied  and  pasted....like  I said  the app  works  great,  I just  need  some  way to  prevent  lock ups.

///////////////////////////
Private Sub Timer1_Timer()
Dim temp As String
Dim start As String
Dim data As String
If MSComm1.PortOpen = False Then
    lblstate.Caption = "Disconnected"
    Exit Sub
End If
data = MSComm1.Input
If Mid(data, 1, 1) <> "s" Then
    lblstate.Caption = "Sync Data"
MSComm1.InputLen = 1
Do While start <> "s"
    start = MSComm1.Input
Loop
lblstate.Caption = "Connected"
MSComm1.InputLen = 8
temp = Mid(data, 1, 4)
pres = Mid(data, 5, 4)

//////////////////////////////////

DaveMon
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Sage020999Commented:
If you are not polling the data, one thing you might want to try is to use the OnComm event to retrieve the data, it will also handle the errors.  Look at the attachment below and let me know if it satisfies your request.
0
Sage020999Commented:

            .RThreshold = 1
            .SThreshold = 1

by setting the above properties to any none zero, the comm port will fire the OnComm event when it receives data.  This will allow you to remove the timer in your app and use the code I sent you earlier to stop any of the errors from the comm port.  

I have written a few apps reading tools attached to the comm port.  I find it a little odd that your machine crashes with no tool.  You should be able to open a port with out any errors.  
0
DaveMonAuthor Commented:
Attachment?

Remember,  the   app  is  functioning fine  as is ...  Im  looking  for  a  way  to prevent  a crash  if  the  user  clicks  "connect"   and  the  serial  cable  is  not  connected.   There  is  already  a  trap  in place  that  ends  the program  if  Com1  is  already  open,  I need  one  that   ends  the program  if  COM1  doesnt  respond  in   1-2  seconds.

Davemon
0
DaveMonAuthor Commented:


we  were  posting  at  nearly  the same  time.....to give  you  more  background  on what  Im  doing....i  have  a microcontroller  called  a  Basic Stamp,  it  has  a  chip  that  measures  temperature  and  sends it  to the Stamp, which  outputs  serial  data  to the PC  via  a serial cable.  i borrowed  some  code  from a similar project on the web  and  hooked  it all  together  and  miraculously it  kindof  worked!!  ( Despite the huge  gaps  in my programming  knowlege ) Ive  got the thing  updating  the screen  in real time  ( a label control )  and  all  is  good  execept  if  I forget  to  plug  in the  microcontroller or something  similar...then  it  takes  1-2  minutes  to ctrl-alt-del  enough  to end  the offending  application  (mine )

DaveMon
0
Sage020999Commented:
Let me do some testing real quick to see if I can get an app to crash on me.  Then I will know what you need to do.
0
DaveMonAuthor Commented:
Ok  sure,  I appreciate  you  goin  to the trouble.  I will explore  some  of the suggestions  youve  already  made

DaveMon
0
KDivadCommented:
Don't know if it will help or not, but I wrote a short bit of code once to check all the possible ports on a computer. It went something like:

On Error Resume Next
    MSComm1.PortOpen = True
    Select Case Err.Number
    Case 0
        'Port opened fine
    Case ????
        'Port non-existent
    Case ????
        'Port already open
    Case Else
        MsgBox Err.Number
    End Select

I don't remember what the errors were, but it is simple enough to find them.
0
Sage020999Commented:
I haven't been able to crash an app.  Have you had any luck yet?  If you want you can email me your code and I can take a look at it.  Is it machine dependant?

0
DaveMonAuthor Commented:


sure  I ll send  you  the  .vbp   or  can  can  look  at  the program  I  borrowed  the  idea  from....its  at  http://geocities.com/SiliconValley/Orchard/6633/vbtemplog.html

the actual  code  I started  with ( and  basically  removed  alot of it )  is  located  here...http://geocities.com/SiliconValley/Orchard/6633/vbwealog.zip


but  even this  code  will  crash  my  machine  without  the sensor  attached  if  you  try  to connect


DaveMon
0
Sage020999Commented:
great, I will take a look at it.
0
Sage020999Commented:
Here is your problem.  Enter a DoEvents and a check to add a condtional statement to avoid trying to read the input when the port is closed.

In the timer1 loop...

MSComm1.InputLen = 1
Do While start <> "s"
    DoEvents
    If MSComm1.PortOpen = True Then
      start = MSComm1.Input
    End If
Loop
0
Sage020999Commented:
Here is your problem.  Enter a DoEvents and a check to add a condtional statement to avoid trying to read the input when the port is closed.

In the timer1 loop...

MSComm1.InputLen = 1
Do While start <> "s"
    DoEvents
    If MSComm1.PortOpen = True Then
      start = MSComm1.Input
    End If
Loop
0
DaveMonAuthor Commented:
I will try  that....ill need  a couple  of  hours  to get  home  and  try it

DaveMon
0
Sage020999Commented:
Ths is probally a little better, that way it gets out of the loop


Private Sub Timer1_Timer()
Dim temp As String
Dim start As String
Dim data As String
If MSComm1.PortOpen = False Then
    lblstate.Caption = "Disconnected"
    Exit Sub
End If
data = MSComm1.Input
If Mid(data, 1, 1) <> "s" Then
    lblstate.Caption = "Sync Data"
MSComm1.InputLen = 1
Do While start <> "s"
    DoEvents
    If MSComm1.PortOpen = True Then
      start = MSComm1.Input
    Else
     
      Exit Do
    End If
Loop
lblstate.Caption = "Connected"
MSComm1.InputLen = 8
temp = Mid(data, 1, 4)
pres = Mid(data, 5, 4)
Label7.Caption = temp
Label8.Caption = pres
temp = (Val(temp) / 10 - 273.15) - 0.55
tempc = (temp * 1.8) + 32
pres = ((((((Val(pres) / 2.86) + 3010) / 1000) / 5) + 0.095) / 0.009) + 0.5
presHg = pres / 3.3875

If temp < -30 Or temp > 30 Or pres < 97 Then
    Exit Sub
End If
m = Minute(Time)
s = Second(Time)

If s = "0" Then
y1 = -(temp * 51.38) + 1027.5
x1 = x + 1.5
Picture1.Line (x, y)-(x1, y1), RGB(0, 0, 255)
y = y1
x = x + 1.5
y1 = 0

py1 = (104 - pres) * 342.5
px1 = px + 1.5
Picture1.Line (px, py)-(px1, py1), RGB(255, 0, 0)
py = py1
px = px + 1.5
py1 = 0
Picture1.Line (0, 1027.5)-(2055, 1027.5)
Picture1.Line (0, 514)-(2055, 514)
Picture1.Line (0, 1541.25)-(2055, 1541.25)
End If

If Format(temp, "###.00") >= maxtemp Then
     maxtemp = Format(temp, "##0.00")
     lblmaxtime.Caption = Time
End If
If Format(temp, "###.00") < mintemp Then
    mintemp = Format(temp, "##0.00")
    lblmintime.Caption = Time
End If
If Format(pres, "###.##") >= maxpres Then
     maxpres = Format(pres, "###.00")
     lblmaxptime.Caption = Time
End If
If Format(pres, "###.##") < minpres Then
    minpres = Format(pres, "###.00")
    lblminptime.Caption = Time
End If
lblmin.Caption = mintemp & " C"
lblmax.Caption = maxtemp & " C"
lbltemp.Caption = Format(temp, "##0.00" & " C")
lbltempc.Caption = Format(tempc, "###.00" & " F")
lblmaxpre.Caption = maxpres & " Kpa"
lblminpre.Caption = minpres & " Kpa"
lblpress.Caption = Format(pres, "###.##" & " Kpa")
lblpressHg.Caption = Format(presHg, "###.##" & " inHg")

If Mid(Minute(Time), 2, 1) = "0" Or Mid(Minute(Time), 2, 1) = "5" Or m = "0" Or m = "5" Then
    If s = "0" Then
        Print #1, Format(temp, "##0.00"), Format(pres, "###.00"), Time
    Else
        Exit Sub
    End If
Else
    Exit Sub
End If
End If
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sage020999Commented:
No problem.  Good Luck.
0
Sage020999Commented:
Have you tried the above solution, or is something not working?
0
DaveMonAuthor Commented:
Im   sorry   I  hadnt  been  working  on that  problem  and  had  forgotten  to  come  and  accept  your  answer....i still  have  work  to  do on the  code  but  interestingly  enough  I was  just  tryin  some  things  and  I dropped  the  code  "DoEvents
    If MSComm1.PortOpen = True Then
      start = MSComm1.Input
    Else
       
      "
into  the proper  area  and  it  began  not  crashing...im  not  sure  if its  as  simple  as  that  or  I have  stumbled  across  yet  more  magical,  mystery  code  that  happens  to work,  but  I dont  understand  :)   anyway,  your  comments  have  given  me  plenty  to work  with for  awhile...i am  also  getting  ready  to ask  another  question  soon  so  maybe  I will  see you  there,   Thanks  again


DaveMon
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.