VBA Example on Excel

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
Don
Posts: 7
Joined: Thu Nov 04, 2010 2:15 am

VBA Example on Excel

Post by Don »

Hi,

Is there any VBA example without .NET framework for Excel 2003?

Thanks and regards,

Don

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: VBA Example on Excel

Post by serg »

Hi Don,

Our VBA sample for MS Excel doesn't use the .NET framework directly. Instead, VBA is using the SUB-20 as a regular COM/ActiveX control. You have to generate the sub20dnc.tlb file for VBA/VB6 though. See the SUB-20 .NET document for details.
In general, if you know how to use a regular windows dll from your VBA-capable document you can directly call the sub20.dll - the SUB-20 main interface library

Regards

Don
Posts: 7
Joined: Thu Nov 04, 2010 2:15 am

Re: VBA Example on Excel

Post by Don »

Hi,

I try to follow user menu to define data type of SlavesArry in Excel VBA,but system show me the error message”Data Type not match”.I don't know how to define data type of SlavesArry in Excel VBA.Help!

Example in sub20net user menu as following

Private Sub I2CEnumSlaves()
Dim Dev As New Sub20
Dim SlavesArry(127) As Byte
Dim SlaveCntr As Integer
' Open SUB-20 device
If Not Dev.Open(0) Then
GoTo Done
End If
' Set I2C clock frequency to 400kHz
If Not Dev.I2C_SetFrequency(400000) Then
GoTo Done
End If
' Scan I2C bus for slaves
If Not Dev.I2C_Scan(SlavesArry, SlaveCntr) Then
GoTo Done
End If
If SlaveCntr > 0 Then
For i = 0 To SlaveCntr - 1
' Adding all the slaves to a list
AddToList(SlavesArry(i).ToString()
Next
End If
Done:
If Dev.GetLastError() > 0 Then
MsgBox(Dev.GetStrError(Dev.GetLastError()))
End If
' Close SUB-20 device
Dev.Close()
End Sub
End Sub

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: VBA Example on Excel

Post by serg »

The example in the document is in VB.NET. For the VBA it should be slightly modified

Private Sub I2CEnumSlaves()

Dim Dev As New Sub20
Dim SlaveCntr As Long
Dim SlavesArry As New SystemByteArray
Dim Success As Boolean

SlavesArry.CreateInstance (128)

' Open first available SUB-20 device
Success = Dev.Open(0)
If Not Success Then
GoTo Done
End If

' Set I2C clock frequency to 400kHz
Success = Dev.I2C_SetFrequency(400000)
If Not Success Then
GoTo Done
End If

' Scan I2C bus for slaves
Success = Dev.I2C_Scan(SlavesArry.GetSystemArray(), SlaveCntr)
If Not Success Then
GoTo Done
End If

If SlaveCntr > 0 Then
For i = 0 To SlaveCntr - 1
' Adding all the slaves to a list
Next
End If

Done:

If Dev.GetLastError() > 0 Then
MsgBox (Dev.GetStrError(Dev.GetLastError()))
End If

' Close SUB-20 device
Dev.Close

End Sub

Regards

Don
Posts: 7
Joined: Thu Nov 04, 2010 2:15 am

Re: VBA Example on Excel

Post by Don »

Hi,

I'd like to send data via I2C_Write command but the result is false.Why?

Source code as below.

Dim DataArry As New SystemByteArray
DataArry.CreateInstance (16)

'Protocol : S_0xBE_W_08_3A_P
Success = DataArry.SetValue(&H8, 0)
Success = DataArry.SetValue(&H3A, 1)

Success = Dev.I2C_Write(&H5F, &H0, 0, DataArry.Array)

Thanks and regards

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: VBA Example on Excel

Post by serg »

Hi,

You can get extended I2C status code by calling the I2C_GetStatus method.
See the "3.2.8 I2C Status" in the "User Manual" document for more details

Regards

Post Reply