Solved

Closing MsComm port

Posted on 1998-10-14
7
649 Views
Last Modified: 2012-06-27
I am reading sentences from a GPS unit using the following code

Private Sub Form_Load()
    ClosePort = False
    MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
 if ClosePort = true then exit sub

     Select Case MSComm1.CommEvent

       Case comEvReceive
          a = Empty
          b = Empty
   
   
       Do Until MSComm1.Input = Chr(36)
         DoEvents
       Loop
     
       
       Do
         a = MSComm1.Input
         b = b & a
       Loop Until a = vbCr
 
       Text1 = b
       
   Case Else
 End Select
End Sub

Private Sub Command1_Click()
  MSComm1.PortOpen = False
  ClosePort = True
End Sub


PROBLEM:  It doesn't work!! if Closeport = true it will still go into the select case and give an error. The code is quick and dirty but it SHOULD work. What I am trying to do is to close the port gracefully. If I unload the form with the MsComm control on it a get a GPF

Any ideas?
0
Comment
Question by:entretec4
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:entretec4
ID: 1439832
Edited text of question
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1439833
Could you try using an 'On Error' statement at the beginning of your subroutine.  If the error is a specific value, then close the port gracefully, otherwise handle the error as you see fit.  This may work for you.
Martin
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1439834
I guess I should give an example:

Private Sub MSComm1_OnComm()
 On Error Goto ErrorHandler
 if ClosePort = true then exit sub




ExitSub:
 Exit Sub

ErrorHandler:
 Select Case Err.Number
   Case 99 'Should be the error that is tripping
    Resume ExitSub
   Case Else
    ' Do something for unexpected errors!!
    Resume ExitSub
 End Select



0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Accepted Solution

by:
Dalin earned 150 total points
ID: 1439835
entretec4,
I don't think there is a closeport property for MSComm.

You need to use the portOpen property

If MSComm1.portopen = false then exit sub

You may also considering change the input statement.

Your first DO until read all the staff in the buffer, and discarded. So your a would not be what you should have.

The other thing is, unless your device send to your port cha by cha, you may not see the input = chr(36), so you stuck in a endless loop.

let me know if I can be further help.
regards
Dalin
0
 

Author Comment

by:entretec4
ID: 1439836
Sorry I didn't make myself clear.

ClosePort is a global boolean that I set to FALSE when opening the port and set to TRUE when I try and stop - close the port (but it doesn't). Using the debug stepping into the function it will com to the 'if Closeport = TRUE then exit sub' and continue past to the next line.
If I put a doevents anywhere I will get 'Out of Stackspace'

I am reading a char at a time. It works fine for reading the sentences but I cannot interrupt the flow e.g. I will a continuous readout of sentences but the only way to stop is to use a ctrl break in the IDE. If I try to use END I will get a GPF

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439837
You can't read a chr at a time like that. Once you issue a MSCom.Input it will clear the input buffer.  You you get all the chr in the buffer. If your device send to the port char by char,  and you want to read like that, you can't use the Oncom event. You need to know every time you receive a chr the ON_Comm event will triggle again. but your code is still in the first event loop, so you are probably have 100 OnCom event s their you are stacked to so you can't really get out.
In debug mode, you are OK because you excute line but line and the 2nd, 3rd,.. event can't be triggled.

In general, what you need to do, if you wnat to use the on_comm event to read the port, is  to read the stuff at once, and then check what you received.


This is my Reading function for you to consider:
To use it in a ON_Comm event, you may do something like that:

strRet = fReadCom(MSComm1, False)
You can also read the port after you send some thing to your device:
strReturn = fReadCom(MSComm1, True, vbCR)

Hope it help. Let me know if you have questions
Regards
Dalin

p.s. the function
Public Function fReadCOM(theComm As MSComm, WaitOrNot As Boolean, Optional EndCha As String) As String

'   Name:       fReadCOM
'   Author:     Dalin Nie
'   Written:    4/14/97
'   Purpose:

'       This function read one line of data from the RS-232 Com port;
'   Called procedures and functions:

'   Input:  theComm:  Name of the MSComm control which is connected to the port you want to read
'           Wait--Boolean
'                 True:  Keep reading the port until the EndCha is received or timed out
'                        Soutable for contineous recieveing until the endCha after a command is issued, and

'                 False: Just Read once, get whatever is in the put and then get out, suitable
                         ' for use with OnComm event
'            EndCha: the external devices ending characters, usually vbCR or vbCRLF, optional
'   Output: The string it read
'
Dim lInput As String
Dim lMessage As String
Dim curReading As Single
Dim lLastTwo As String
Dim I As Integer
Dim lTime As Long


On Error GoTo fReadCOMErrHandler

' Set up output of fReadCOM to be blank by default

'add number of lines you expect to read

lMessage = ""

'Get one Line of Data
If WaitOrNot = True Then
        lTime = Timer
        Do Until Right(lMessage, Len(lStRespEnd)) = lStRespEnd
                If theComm.InBufferCount > 0 Then
                        lInput = theComm.Input
                        lMessage = lMessage + lInput
                           
                    End If ' If frmDeckLayout.Comm1.InBufferCount = dataLen
                DoEvents
                'Time out, I use 5 seconds here, change accordingly
                If Timer - lTime > 5 Then Exit Do
            Loop
       
    Else
       
        lMessage = theComm.Input
    End If
               
               
     ' strip the EndCha, if what we received is ended with the End Cha
   If Right(lMessage, Len(EndCha)) = EndCha Then
         lMessage = Left(lMessage, Len(lMessage) - Len(EndCha))
     End If
     
  ' If Has a startcha, strip it too
  ' If Left(lMessage, Len(cStRespBegin)) = cStRespBegin Then
   '     lMessage = Right(lMessage, Len(lMessage) - Len(cStRespBegin))
   '   End If

    fReadCOM = lMessage
   
    'Put the response to some textbox
    'frmMain.txtCaroResp = lMessage
   
Exit Function

fReadCOMErrHandler:
   fReadCOM = "ERROR" & Err.Number & " " & Err.Description
   ' Oprional gui.

End Function


 
0
 

Author Comment

by:entretec4
ID: 1439838
That worked well after I figured what some of the variables meant
Thanks

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

10 Experts available now in Live!

Get 1:1 Help Now