Consulting

Results 1 to 4 of 4

Thread: Modifying COMport Code

  1. #1
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    3
    Location

    Modifying COMport Code

    Hello All,

    I am trying to modify the code attached. Currently I can receive data from the COM port using this program. The problem is that all of the information goes into the A15 cell. I am taking torque readings from a calibration bench that outputs numbers 3-4 digits long. An example would be 95.63. After each torque reading I would like a new cell to populate with the data. For instance, the first reading would populate A15. The second would populate A16. The third would populate A17 and so forth. How would I go about modifying the code so that each reading is received in its own cell?

    Thanks for all the help this forum is awesome!

    Excel COMM interface without 3rd party .xlsm

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This is a guess since I can't test. I added to C15 and beyond since that was what the code said, not the ...

    For instance, the first reading would populate A15. The second would populate A16. The third would populate A17 and so forth
    A15 etc. and since col A had a lot of stuff in it


    Public Sub Connect()
    Dim strSettings As String, strData As String
    Dim intPortID As Integer
    Dim lngStatus As Long
    Dim rData As Range                  '   <<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        strData = ""
        strSettings = ""
        Sheet1.Range("C15").Value = ""
        
        With Sheet1
        
            Set rData = .Range("C15")   '   <<<<<<<<<<<<<<<<<<<<<<<<<<<
        
            If .Range("A63").Value = True Then
                strSettings = "baud=" & Left(.Range("D5").Value, Len(.Range("D5").Value) - 3) & " parity=" & Left(.Range("D7").Value, 1) & " data=" & .Range("D6").Value & " stop=" & .Range("D8").Value
                intPortID = Mid(.Range("D2").Value, 4, Len(.Range("D2").Value) - 3)
                
                lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), strSettings)
                
                If lngStatus = 0 Then
                    MsgBox "Connection to " & .Range("D2").Value & " Established!"
                    .Range("A64").Value = 1
                    Do While .Range("A64").Value = 1
                        lngStatus = CommRead(intPortID, strData, 1)
                        If lngStatus > 0 Then
    '                        .Range("C15").Value = .Range("C15").Value & strData
                            rData.Value = strData           '   <<<<<<<<<<<<<<<<<<<<<
                            Set rData = rData.offset(1, 0)  '   <<<<<<<<<<<<<<<<<<<<<
                        End If
                        'Sleep 500
                        DoEvents
                    Loop
                Else
                    lngStatus = CommGetError(strError)
                    MsgBox "Connection Failed. " & strError
                End If
            Else
                MsgBox "Please Ensure All COM Setting Are Set And COM Port Selected."
            End If
        End With
        
    End Sub
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    3
    Location
    Thank you very much this is working! How would I go about setting up a polling time per cell? Say I want to poll for 500ms before moving into a new cell, how would I do this?

  4. #4
    VBAX Newbie
    Joined
    Sep 2019
    Posts
    3
    Location
    I am trying to use your code to send command to serial port. however, when I send the command,
    I always get IngStatus = 0, that is <> Len(strData), and then get the message "can not send Data"
    How should I revise your code ?
    Thanks in advance






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •