UltimaSerial How to use ActiveX and Variant in VB.NET
 

Windaq add-ons
Windaq Add-ons


 

The following is a step-by-step lesson on how to use ActiveX and Variant in VB.NET

For other VB varieties, including Visual Basic 2008 Express Edition, please visit our classroom .

32 or 64-bit?

Most ActiveXs are 32-bit components, you must select 32-bit code option when using 64-bit compilers. Both 32-bit and 64-bit Windows runs 32-bit applications properly. 

In this lession, we will use Ultimaserial ActiveX to develop a data acquisition application with DATAQ's Starter kit. 

Click here to download a VB.NET sample project . You can also use VisualBASIC tutorial as reference

If you google using the terms of "use variant vb.net", you will probably find the statement "VB.NET no longer supports the Variant and Currency data types.". It sounds scary, doesn't it? Without variant, how can we get the data from UltimaSerial?! Do I have to use GetDataPt to retrieve a single data point at a time? (Yes, you could, if you don't want to deal with variant at all)

Well, Microsoft may try to drop the variant support in VB.NET, but it also keeps a back door opened for anyone who wants to use it.

To access the data returned by GetData, instead of using variant, declare it as short directly.

In the following example, we have one form, two buttons, start and stop, and UltimaSerial control.

Please pay attention to the area that is highlighted with red. sections highlighted with blue are added to the project, green area are generated by VB.NET

Public Class Form1
Inherits System.Windows.Forms.Form

Dim v(,) As Short
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents AxUltimaSerial1 As AxULTIMASERIALLib.AxUltimaSerial
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxUltimaSerial1 = New AxULTIMASERIALLib.AxUltimaSerial()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
CType(Me.AxUltimaSerial1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'AxUltimaSerial1
'
Me.AxUltimaSerial1.Enabled = True
Me.AxUltimaSerial1.Location = New System.Drawing.Point(104, 24)
Me.AxUltimaSerial1.Name = "AxUltimaSerial1"
Me.AxUltimaSerial1.OcxState = CType(resources.GetObject("AxUltimaSerial1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxUltimaSerial1.Size = New System.Drawing.Size(100, 50)
Me.AxUltimaSerial1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(32, 168)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 48)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Start"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(168, 168)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(56, 48)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Stop"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.AxUltimaSerial1})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxUltimaSerial1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region



Private Sub AxUltimaSerial1_NewData(ByVal sender As Object, ByVal e As AxULTIMASERIALLib._DUltimaSerialEvents_NewDataEvent) Handles AxUltimaSerial1.NewData
v = AxUltimaSerial1.GetData()
' the data can be accessed as v(chn, pt),
' where chn=0 for the first channel,
' pt =0 for the first pts

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AxUltimaSerial1.CommPort = 2

AxUltimaSerial1.Device =194

AxUltimaSerial1.ChannelCount = 1
AxUltimaSerial1.SampleRate = 20
AxUltimaSerial1.EventLevel = 20
AxUltimaSerial1.Start()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
AxUltimaSerial1.Stop()
End Sub
End Class

Some users report that NewData event may not fire under certain Windows configuration. If you suspect this is what happened to you, please use Timer function to retrieve the data instead: Add a timer to the form, set its resolution to 10, enable it when Start button is pushed and disable it when Stop button is pushed. Copy the codes in NewData event to Timer1_Tick event

Last update: 11/18/16

Copyright: 2000-2005  www.UltimaSerial.com