Open and read a serial Com Port connected to a scanner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    Open and read a serial Com Port connected to a scanner

    I would like to open a serial com port (RS232) when a form (frmRtMain) open, collect the data when the scanner is activated and read data, and then add it to a textbox (text10) in a form. The port should stay open while the form (frmRtMain) is open.
    This is what i have tried so far
    Code:
    Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
        Dim lngStatus As Long
        Dim strError  As String
        Dim strData   As String
        ' Initialize Communications
    '(com 8)
        lngStatus = CommOpen(intPortID, "8" & CStr(intPortID), _
            "baud=57600 parity=N data=8 stop=1")
            If lngStatus <> 0 Then
        ' Handle error.
            lngStatus = CommGetError(strError)
        MsgBox "COM Error: " & strError
        End If
        
        ' Set modem control lines.
        lngStatus = CommSetLine(intPortID, LINE_RTS, True)
        lngStatus = CommSetLine(intPortID, LINE_DTR, True)
    
        ' Read maximum of 64 bytes from serial port.
        lngStatus = CommRead(intPortID, strData, 64)
        If lngStatus > 0 Then
            ' Process data.
        ElseIf lngStatus < 0 Then
        Me.Text10 = strData
            ' Handle error.
        End If
    
        ' Reset modem control lines.
        lngStatus = CommSetLine(intPortID, LINE_RTS, False)
        lngStatus = CommSetLine(intPortID, LINE_DTR, False)
    I will need a module for the "commopen" and "commget" and "commread" calls to get it to work
    I have seen this done with an TCP/IP address/network setup also where the scanner links to a wireless router. Would that be a better option? How is that done?
    Any suggestions will be welcomed pls i am rather desperate.
  • serialcomms
    New Member
    • Aug 2022
    • 1

    #2
    Serial COM Ports with VBA

    I've recently uploaded a new set of VBA functions for anyone interested in using Serial COM Ports with Office apps.



    Does that help ?
    Last edited by serialcomms; Aug 26 '22, 06:16 AM. Reason: Clarity

    Comment

    Working...