User manual

Programming examples
66 © 2007-2010 Analytica GmbH
1
A call to SPIOpenDevice establishes a network connection to the device. If the function fails, a
textual error description is returned via Funktion GetErrorMsg.
2
The connection to the device is closed with the SPICloseDevice function.
Reading the device settings and creation of the textual presentation of the data is done by the
GetAnagateInfo function.
Private Function GetAnagateInfo(hHandle As Long) As String
Dim nRC As Long, sText As String
Dim nBaudrate As Long, nSigLevel As Byte, nAuxVoltage As Byte, nClockMode As Byte
Dim nDigitalOutput As Long, nDigitalInput As Long
nRC = SPIGetGlobals(hHandle, nBaudrate, nSigLevel, nAuxVoltage, nClockMode)
If (nRC = 0) Then
sText = sText & "Baudrate=" & CStr(nBaudrate) & ", Siglevel="
Select Case nSigLevel
Case 1: sText = sText & "+5.0V"
Case 2: sText = sText & "+3.3V"
Case 3: sText = sText & "+2.5V"
Case Else: sText = sText & "High impedance"
End Select
sText = sText & vbCrLf & "AuxVoltage="
Select Case nAuxVoltage
Case 1: sText = sText & "+2.5V"
Case Else: sText = sText & "+3.3V"
End Select
sText = sText & ", ClockMode="
Select Case nClockMode
Case 1: sText = sText & "CPHA=0 und CPOL=1"
Case 2: sText = sText & "CPHA=1 und CPOL=0"
Case 3: sText = sText & "CPHA=1 und CPOL=1"
Case Else: sText = sText & "CPHA=0 und CPOL=0"
End Select
Else
sText = sText & "Fehler bei SPIGetGlobals: " & GetErrorMsg(nRC) & vbCrLf
End If
GetAnagateInfo = sText
End Function
7.2.1.3. Executing a command on the SPI bus
The AnaGate SPI can send arbitrary commands to the connected SPI bus. To write and read data by the
PC only the SPIDataReq function is required.
Public Declare Function SPIDataReq Lib "AnaGateSPIVB6" Alias "_SPIDataReq@20" (ByVal hHandle As Long, _
ByVal lpBufferWrite As Any, _
ByVal nBufferWriteLen As Long, _
ByVal lpBufferRead As Any, _
ByVal nBufferReadlLen As Long) As Long
The event procedure btnStart_Click is called on click of the Execute command button.
Private Sub btnStart_Click()
Dim nRC As Long, sText As String, I As Integer, sByteText As String
Dim nBaudrate As Long, nSigLevel As Byte, nAuxVoltage As Byte, nClockMode As Byte
Dim nBufferWriteLen As Long, nBufferReadLen As Long
Dim arrWrite(1 To 255) As Byte, arrRead(1 To 255) As Byte
nRC = SPIOpenDevice(hHandle, Me.IPAddresse.Text, 2000)
If nRC <> 0 Then
sText = "Fehler bei SPIOpenDevice: " & GetErrorMsg(nRC)
Else
nBaudrate = CLng(Me.txtBaudrate)
nSigLevel = CLng(Me.cmbSigLevel.ListIndex)
nAuxVoltage = CLng(Me.cmbAuxVoltage.ListIndex)
nClockMode = CLng(Me.cmbClockMode.ListIndex)
nRC = SPISetGlobals(hHandle, nBaudrate, nSigLevel, nAuxVoltage, nClockMode)
1
If nRC <> 0 Then
sText = sText & "Fehler bei SPISetGlobals: " & GetErrorMsg(nRC) & vbCrLf