Passing arrays for functions
Passing arrays for functions
Passing arrays for functions
Passing arrays for functions
Passing arrays for functions
Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions
Passing arrays for functions Passing arrays for functions
Passing arrays for functions
Go Back  Xtreme Visual Basic Talk > > > Passing arrays for functions


Reply
 
Thread Tools Display Modes
  #1  
Old 04-25-2018, 09:46 PM
gahelton gahelton is offline
Newcomer
 
Join Date: Sep 2012
Posts: 2
Default Passing arrays for functions


Sorry if this is a duplicate post. I look through several dozen posts and didn't find an answer.

For a communications function, I would like to pass a reference to a byte array that is the comm buffer. Should be simple, but I just can't make it work. The function input parameters have to make it through 3 functions.

For the CommWrite function, I am trying to pass ByRef ModBusXmitBuf() as Byte, but I get an error on this.


Code:
Private Sub cmdStart_Click()
Dim XmitLen As Long
Dim retval As Long

ModBusXmitBuf(0) = CByte(&H0)
ModBusXmitBuf(1) = CByte(&H0)
ModBusXmitBuf(2) = CByte(&H0)
ModBusXmitBuf(3) = CByte(&H0)
ModBusXmitBuf(4) = CByte(&H0)
ModBusXmitBuf(5) = CByte(&H0)
ModBusXmitBuf(6) = CByte(&H0)
XmitLen = 7

retval = CommWrite(1, ByRef ModBusXmitBuf() as Byte, XmitLen)
End Sub

Here is a snippet of the CommWrite function. I have also told this function that it is to accept an array ByRef.

Code:
Public Function CommWrite(intPortID As Integer, ByRef XmitBuf() As Byte, XmitLen As Long) 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, XmitBuf(), XmitLen, _
        lngWrSize, udtCommOverlap)

The final code snippet is the API function WriteFile. If have changed it's input parameter to accept an array ByRef.

Code:
Declare Function WriteFile Lib "kernel32" _
    (ByVal hFile As Long, ByRef lpBuffer() As Byte, _
    ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
    lpOverlapped As OVERLAPPED) As Long
I've seen some examples, and tried many, many variations of passing array data, but not much luck. Some variations won't create any errors, but the data sent does not match that in ModBusXmitBuf() - Don't know where the data is coming from.

Thanks in advance for the help.
Reply With Quote
  #2  
Old 04-26-2018, 02:19 AM
Flyguy's Avatar
FlyguyPassing arrays for functions Flyguy is offline
Lost Soul

Retired Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,175
Default

How the data is accepted by a function is defined in the function declaration.
So you don't (and can't) specify this in the call.

Code:
retval = CommWrite(1, ModBusXmitBuf(), XmitLen)
Reply With Quote
  #3  
Old 04-26-2018, 08:33 AM
gahelton gahelton is offline
Newcomer
 
Join Date: Sep 2012
Posts: 2
Default

Thanks for the reply.

This led me in the right direction (got rid of the error when CommWrite was called), but the problem of sending the wrong data was still there. I verified that CommWrite was getting the correct pointer to the array, and that the array data was visible in CommWrite. But it wasn't making it into WriteFile function correctly. So I changed the declaration for WriteFile "lpbuffer" parameter to "lpbuffer as Any" (ByRef is assumed if not explicitly declared).

Also, when WriteFile is called, I passed the parameter for the transmit buffer with the first element in the buffer "XmitBuf(0)" instead of XmitBuf(). I suppose that you could start anywhere in the array that you wanted, but I always want to start at the beginning.

Here are the snippets of code changes.

Code:
Declare Function WriteFile Lib "kernel32" _
    (ByVal hFile As Long, lpBuffer As Any, _
    ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
    lpOverlapped As OVERLAPPED) As Long

Code:
Private Sub cmdStart_Click()
Dim XmitLen As Long
Dim retval As Long

ModBusXmitBuf(0) = CByte(&H55)
ModBusXmitBuf(1) = CByte(&H0)
ModBusXmitBuf(2) = CByte(&H0)
ModBusXmitBuf(3) = CByte(&H0)
ModBusXmitBuf(4) = CByte(&H0)
ModBusXmitBuf(5) = CByte(&H0)
ModBusXmitBuf(6) = CByte(&H0)
XmitLen = 7

retval = CommWrite(1, ModBusXmitBuf(), XmitLen)
End Sub

Code:
'-------------------------------------------------------------------------------
' CommWrite - Output data to the serial port.
'
' Parameters:
'   intPortID   - Port ID used when port was opened.
'   XmitBuf(0)  - Data to be transmitted.
'   XmitLen     - Length of Data to transmit
'
' Returns:
'   Error Code  - 0 = No Error.
'-------------------------------------------------------------------------------
Public Function CommWrite(intPortID As Integer, ByRef XmitBuf() As Byte, XmitLen As Long) 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, XmitBuf(0), XmitLen, _
        lngWrSize, udtCommOverlap)
Reply With Quote
Reply

Tags
function, cbyte&h0, byref, xmitlen, data, byte, dim, array, modbusxmitbuf, retval, passing, error, commwrite, byval, snippet, lngsize, lngwrsize, lngwrstatus, xmitbuf, integer, functions, writefile, accept, variations, pass


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Passing arrays for functions
Passing arrays for functions
Passing arrays for functions Passing arrays for functions
Passing arrays for functions
Passing arrays for functions
Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions Passing arrays for functions
Passing arrays for functions
Passing arrays for functions
 
Passing arrays for functions
Passing arrays for functions
 
-->