WriteFile() Function in VBA Excel not work correctly, it returns zero.

CHEPEL SAPAG 1 Reputation point
2021-10-19T18:48:30.42+00:00

Hello! i'm using the updated library for 64bits modCOMM - Written by: David M. Hitchner but i can't send data for serial port. The writefile() function returns zero all the time.

Below I copy the entry to the function. The readfile function is very similar, but it works fine. Please if anyone can help me I would appreciate it.

------------------- here the code-------------

Public Function CommWrite(intPortID As Integer, strData As String) As Long

Dim i As Integer
Dim lngStatus As Long, lngSize As Long
Dim lngWrSize As Long, lngWrStatus As Long

On Error GoTo Routine_Error

' Get the length of the data.
lngSize = Len(strData)

' Output the data.
lngWrStatus = WriteFile(udtPorts(intPortID).lngHandle, strData, lngSize, _
    lngWrSize, udtCommOverlap)

' Note that normally the following code will not execute because the driver
' caches write operations. Small I/O requests (up to several thousand bytes)
' will normally be accepted immediately and WriteFile will return true even
' though an overlapped operation was specified.

DoEvents

If lngWrStatus = 0 Then
    lngStatus = GetLastError
{count} votes

2 answers

Sort by: Most helpful
  1. Tom van Stiphout 1,621 Reputation points MVP
    2021-10-20T00:33:56.093+00:00

    hEvent As LongPtr

    0 comments No comments

  2. Shafique Ahmed 0 Reputation points
    2023-02-08T21:36:41.3233333+00:00

    Hi, Everyone

    I am also Working on this modCOMM Code. I use this code to communicate with my weight scale. But can't figure out it which function or routine that i call on caommand button click Event.

    ----------------here is the code--------------

    Option Compare Database
    
    Private Sub Command157_Click()
        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
        lngStatus = commopen(intPortID, "COM3" & CStr(intPortID), _
            "baud=9600 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)
    
        ' Write data to serial port.
        lngSize = Len(strData)
        lngStatus = CommWrite(intPortID, strData)
        If lngStatus <> lngSize Then
        ' Handle error.
        End If
    
    
    
        ' Read maximum of 64 bytes from serial port.
        lngStatus = CommRead(intPortID, strData, 64)
        If lngStatus > 0 Then
            ' Process data.
        ElseIf lngStatus < 0 Then
            ' Handle error.
        End If
    
        ' Reset modem control lines.
        lngStatus = CommSetLine(intPortID, LINE_RTS, False)
        lngStatus = CommSetLine(intPortID, LINE_DTR, False)
    
    
    
        ' Close communications.
        Call CommClose(intPortID)
    End Sub
    
    
    0 comments No comments