Solved

Closing MsComm port

Posted on 1998-10-14
7
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a task in Outlook from access 11 43
How to compare ms sql hashbytes results within vb6 5 106
Problem to line 23 80
Set WorkSheet  not Working 9 62
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

734 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