Closing MsComm port

Posted on 1998-10-14
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)
         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?
Question by:entretec4
  • 3
  • 2
  • 2

Author Comment

ID: 1439832
Edited text of question

Expert Comment

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.

Expert Comment

ID: 1439834
I guess I should give an example:

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

 Exit Sub

 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Accepted Solution

Dalin earned 150 total points
ID: 1439835
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.

Author Comment

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


Expert Comment

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

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
                'Time out, I use 5 seconds here, change accordingly
                If Timer - lTime > 5 Then Exit Do
        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

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

End Function


Author Comment

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


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…

830 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