Solved

MScomm  error trap  needed

Posted on 2000-04-19
20
411 Views
Last Modified: 2010-01-13


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
0
Comment
Question by:DaveMon
  • 12
  • 7
20 Comments
 
LVL 2

Expert Comment

by:Sage020999
ID: 2732324
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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2732344
..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
 
LVL 2

Author Comment

by:DaveMon
ID: 2732355


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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2732384
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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2732396

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

Author Comment

by:DaveMon
ID: 2732399
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
 
LVL 2

Author Comment

by:DaveMon
ID: 2732427


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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2732440
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
 
LVL 2

Author Comment

by:DaveMon
ID: 2732459
Ok  sure,  I appreciate  you  goin  to the trouble.  I will explore  some  of the suggestions  youve  already  made

DaveMon
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2733096
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:Sage020999
ID: 2735432
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
 
LVL 2

Author Comment

by:DaveMon
ID: 2736163


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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2736251
great, I will take a look at it.
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2736281
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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2736282
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
 
LVL 2

Author Comment

by:DaveMon
ID: 2736287
I will try  that....ill need  a couple  of  hours  to get  home  and  try it

DaveMon
0
 
LVL 2

Accepted Solution

by:
Sage020999 earned 50 total points
ID: 2736310
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
 
LVL 2

Expert Comment

by:Sage020999
ID: 2736314
No problem.  Good Luck.
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2747904
Have you tried the above solution, or is something not working?
0
 
LVL 2

Author Comment

by:DaveMon
ID: 2749056
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now