User manual

Programming examples
67 © 2007-2010 Analytica GmbH
End If
Me.lblDeviceInfo.Caption = GetAnagateInfo(hHandle)
nBufferWriteLen = GetCommand(arrWrite)
2
nBufferReadLen = nBufferWriteLen
nRC = SPIDataReq(hHandle, VarPtr(arrWrite(1)), nBufferWriteLen, _
VarPtr(arrRead(1)), nBufferReadLen)
3
If nRC = 0 Then
For I = 1 To nBufferReadLen
sByteText = sByteText & "0x" & ToHex(arrRead(I)) & " "
Next I
Me.txtBufferRead = sByteText
sText = sText & "SPIDataReq OK: " & vbCrLf
Else
sText = sText & "Fehler bei SPIDataReq: " & GetErrorMsg(nRC) & vbCrLf
End If
nRC = SPICloseDevice(hHandle)
End If
End Sub
1
A call to SPISetGlobals sets the global settings on the device, the parameter values of the input
fields in the dialog form are used.
2
The GetCommand function converts the texual SPI command entered in the input field on the form
to a byte array structure.
3
To process the data in the read and receive buffers, a byte array is used as VB6 data type for both
buffers. For this to work, the real memory address of the array data has to be parsed to the DLL
function. This will be done by using the VarPtr function on the first byte array element.
.
7.3. Programming language VB.NET
Of course, it is also possible to use the functions of the AnaGate API with the .NET programming
languages. For these languages the functions have only to be declared correctly in one of the .NET
languages. Loading and unloading of the declared API functions is done automatically by the .NET
framework.
7.3.1. CAN Console application VB.NET
This programming example for VB.NET demonstrates how to connect to an AnaGate CAN and how to
process received CAN data via a callback function.
Declare Function CANOpenDevice Lib "AnaGateCAN" (ByRef Handle As Int32, _
ByVal ConfirmData As Int32, _
ByVal MonitorOn As Int32, _
ByVal PortNumber As Int32, _
ByVal TCPAddress As String, _
ByVal Timeout As Int32) As Int32
Declare Function CANCloseDevice Lib "AnaGateCAN" (ByVal Handle As Int32) As Int32
Public Delegate Sub CAN_CALLBACK(ByVal ID As Int32, ByVal Buffer As IntPtr, _
ByVal Bufferlen As Int32, ByVal Flags as Int32, _
ByVal Handle as Int32)
Declare Function CANSetCallback Lib "AnaGateCAN" (ByVal Handle As Int32, _
ByVal MyCB As CAN_CALLBACK) As Int32
Sub CANCallback( ByVal ID As Int32, ByVal Buffer As IntPtr, ByVal Bufferlen As Int32, _
ByVal Flags as Int32, ByVal Handle as Int32)
Dim Bytes as Byte(8)
System.Runtime.InteropServices.Marshal.Copy(Buffer, Bytes, 0, Bufferlen )
Console.Out.Write( "CAN-ID=" )
Console.Out.Write( ID )