Closing MsComm port

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?
entretec4Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
DalinConnect With a Mentor Commented:
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
 
entretec4Author Commented:
Edited text of question
0
 
mkmccrearyCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
mkmccrearyCommented:
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
 
entretec4Author Commented:
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
 
DalinCommented:
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
 
entretec4Author Commented:
That worked well after I figured what some of the variables meant
Thanks

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.