VBA MSComm troubles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbrother
    New Member
    • Nov 2007
    • 19

    VBA MSComm troubles

    Hello,
    I have a program in Access that uses VBA to capture data being sent over the serial port from a Cosmo Flow Test Machine. This determines the air pressure's leak rate escaping from a muffler.

    I have written several programs that send data out serially, but I am new to "listening" to receive the data.

    I can setup an instance of HyperTerminal, provided the settings used in the given manual for the device and successfully retreive data results. When using the same settings in my VBA code, I never get any data; none. The data coming from HyperTerminal seems to be 'stream' based rather than packet, thus I used the code to keep adding the characters to the end until it finishes the loop.

    Here's the code:
    Code:
     
    Private Sub cmdHHP_Click()
    
    Dim strLeakRate As String
    Dim strUprLmt As String
    Dim strLwrLmt As String
    Dim strChar As String
    
    strUprLmt = 3.77
    strLwrLmt = 0
    
    lblStatus.Caption = "RETREIVING LEAK TEST DATA"
    
        MSComm0.CommPort = 1
        MSComm0.Settings = "1200,N,8,1"
        MSComm0.PortOpen = True
        
            Do Until strChar = ":"
                strChar = MSComm0.Input
                strLeakRate = strLeakRate & MSComm0.Input
            Loop
            
        MSComm0.PortOpen = False
        
    lblStatus.Caption = "DATA RETREIVED"
        
        strLeakRate = Mid(strLeakRate, 6, 11)
        lblLeakRate.Caption = strLeakRate
        
        
                   
            If strLeakRate > strUprLmt Then
                cmdFailed
                Exit Sub
            Else
                DoCmd.RunMacro "macHHP"
            End If
            
     lblStatus.Caption = "PRINTING LABEL"
    
    '''''''''''
    cmdReset
    '''''''''''
    
    End Sub
    
    Public Sub cmdFailed()
    
     MsgBox "Test Failed. Please try the leak test at most 3 times for any one DPF.", vbCritical + vbOKOnly, "LEAK TEST FAILED"
    End Sub
    
    Public Sub cmdReset()
    
     strChar = ""
     strLeakRate = ""
     lblLeakRate.Caption = "-----"
     lblStatus.Caption = "READY"
    
    End Sub
    Looking at the form versus the code, it doesn't seem like it's preforming the first working line. I have a label that changes status when they click a 'start' button. As soon as the button is clicked, I would imiagine that the display message should change to 'RETREIVING LEAK TEST DATA'.

    Another concern is that it doesn't seem like the port is actually ever opening. The code currently hangs in the loop waiting to receive the ":" char to denote the end of the string. I manually break the code and debug, then go back to the form to try again and I DO NOT receive a 'port already open' error as would be expected.

    One last question... The manual claims that the end of the string is denoted by carriage return, <CR>. It lists what I would think to be the hexadecimal code for that as (0DH). How would I go about using that as the delimiter to signify the end of the string?

    Any info anyone could share would be most appreciated.
    Thanks in advance,
    dbrother.
  • Bum
    New Member
    • Jan 2008
    • 19

    #2
    Hi dbrother,

    you have a loop where you're looking for a colon. Are you sure a colon is supposed to come through? Why don't you try to say

    code[vb]
    strchar = ""
    do until strChar <> ""

    this way, if anything comes through you can see it.

    also, the reason your program might seem like it's hanging up is b/c of this loop. You need to use DoEvents in the call to free up the interface so you can click a cancel button or something. Also, you need something in this loop to eventually get you out if nothing ever shows up. You know, like a counter.

    Also, some of these call like

    code[vb]
    strChar = MSComm0.Input
    strLeakRate = strLeakRate & MSComm0.Input

    /code

    could go inside your "ONComm" event for mscomm0

    Also, are you sure the baud settings are correct? Most machines I've ever talked to used 9600 or more.

    Hope this helps,

    B

    Comment

    Working...